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

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

【解决方法】在 Environment Variables中添加字段 name:OS_ACTIVITY_MODE,value:disable

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()(0)
            _ = make?.left.mas_equalTo()(20)
            _ = make?.height.mas_equalTo()(self.frame.size.height / 3 - 1)
            _ = make?.width.mas_equalTo()(self.frame.size.width / 2 - 30)
        }

相关参考

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值