天天看点

Swift2.3适配Swift3.0

升级Xcode8.0Beta版,看着公司Swift版本的项目200多个报红,那种酸爽也就苹果能给。慢慢改吧!改的东西很多,但基本都不难,记录几个花费较长时间的Bug。

NO.1

【报错】“Use Legacy Swift Language Version” (SWIFT_VERSION) is required to be configured correctly for targets which use Swift. Use the [Edit > Convert > To Current Swift Syntax…] menu to choose a Swift version or use the Build Settings editor to configure the build setting directly.

【解决方法】设置 Build Settings —-> Use Legacy Swift Language Version —-> YES/NO,改为NO或者YES

NO.2

【问题】控制台打印出大量无用的信息。

Swift2.3适配Swift3.0

【解决方法】在

Environment Variables

中添加字段 name:

OS_ACTIVITY_MODE

,value:

disable

Swift2.3适配Swift3.0

NO.3

【问题】整个项目能编译通过,但在运行时

启动页出现后就黑屏

【解决方法】问题在于,AppDelegate中

didFinishLaunchingWithOptions

这个方法没有走。虽然是利用系统提供的修复,自动从Swift2.3修复到Swift3,但修复完的这个方法也还是不对的。

private func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { }
           

修改为

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { }
           

NO.4

【报错】“ambiguous referenc to member datask(with: completionhandler:)”

【解决方法】这个是在

URLSessionDataTask

中遇到的,属于类型不明确的报错。

修改前的代码:

let task: URLSessionDataTask = session.dataTask(with: request) { (data, resp, err) in   }
           

修改后的代码:

let task: URLSessionDataTask = session.dataTask(with: request as URLRequest) { (data, resp, err) in }
// 由于request是NSMutableURLRequest类型,在request 后面添加了 as URLRequest
           

NO.5

【报错】”Cannot pass immutable value of type ‘NSDate?’ as inout argument”

【解决方法】

var beginDate: NSDate? = NSDate()

修改前的代码:

var beginDate:  Date?
var endDate: Date?
let calendar: Calendar = Calendar.current
let ok: Bool = calendar.range(of: Calendar.Unit.month, start: &beginDate, interval: &interval, for: newDate as Date)
           

修改后的代码:

var beginDate:  NSDate? = NSDate()
var endDate: NSDate? = NSDate()
let calendar: Calendar = Calendar.current
let ok: Bool = calendar.range(of: Calendar.Unit.month, start: &beginDate, interval: &interval, for: newDate as Date)
           

NO.6

【警告】”Expression of type “UIViewController?” is unused”.

【解决】Swift3之前,每个方法都有一个默认的可以废弃的结果。

相关解决实例:

// 例一:
    _ = navigationController?.popViewController(animated: true)

// 例二:
    t_principal.mas_makeConstraints { (make) in
            _ = make?.top.mas_equalTo()()
            _ = make?.left.mas_equalTo()()
            _ = make?.height.mas_equalTo()(self.frame.size.height /  - )
            _ = make?.width.mas_equalTo()(self.frame.size.width /  - )
        }
           

相关参考

http://adcdownload.apple.com/Developer_Tools/Xcode_8_beta_3/Release_Notes_for_Xcode_8_beta_3.pdf

https://forums.developer.apple.com/thread/49635

http://stackoverflow.com/questions/37812286/swift-3-urlsession-shared-ambiguous-reference-to-member-datataskwithcomplet

http://stackoverflow.com/questions/37946990/cgrectmake-cgpointmake-cgsizemake-cgrectzero-cgpointzero-is-unavailable-in

http://stackoverflow.com/questions/37843049/xcode-8-swift-3-expression-of-type-uiviewcontroller-is-unused-warning