Swift 3 Migration Issues

As you already know that Swift 3 is released, I guess you guys are busy migrating your project to Swift 3 now. So this post I just list some issues I came across during migrating to Swift 3.

1.Instance method 'application(:didFinishLaunchingWithOptions:)' nearly matches optional requirement 'application(:didFinishLaunchingWithOptions:)' of protocol 'UIApplicationDelegate'

Xcode 8 gives this warning because now the parameter launchOptions of the delegate method is changed from [NSObject: AnyObject]? to [UIApplicationLaunchOptionsKey : Any]? as below,

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

Xcode's fix-it suggestions would not fix the problem, and the method never get called if you use the suggestions.

2.Objective-C method 'tableView:estimatedHeightForRowAt:' provided by method 'tableView(_:estimatedHeightForRowAt:)' does not match the requirement's selector ('tableView:estimatedHeightForRowAtIndexPath:')

The method tableView:estimatedHeightForRowAt: is part of the UITableViewDelegate protocol. But this method was put in the extension that adopts the UITableViewDataSource protocol.

Also Xcode's suggestion "Insert @objc(tableView:estimatedHeightForRowAtIndexPath:)" is not correct.

3.Nil is not compatible with expected argument type 'UnsafePointer'

Core Graphics now has more Swifty API in Swift 3, so you just need to replace the C functions with new one.

For example, the following code snippet just draw border around one image,

let path = CGMutablePath()
let imageFrame = imageView.bounds
CGPathMoveToPoint(path, nil, 0.0, 0.0)
CGPathAddLineToPoint(path, nil, 0, imageFrame.size.height)
CGPathAddLineToPoint(path, nil, imageFrame.size.width, imageFrame.size.height)
CGPathAddLineToPoint(path, nil, imageFrame.size.width, 0.0)
CGPathAddLineToPoint(path, nil, 0.0, 0.0)

I replaced the code with following one.

let path = CGMutablePath()
let imageFrame = imageView.bounds
path.move(to: CGPoint(x: 0.0, y: 0.0))
path.addLine(to: CGPoint(x: 0.0, y: imageFrame.size.height))
path.addLine(to: CGPoint(x: imageFrame.size.width, y: imageFrame.size.height))
path.addLine(to: CGPoint(x: imageFrame.size.width, y: 0.0))
path.addLine(to: CGPoint(x: 0.0, y: 0.0))

4.This app has crashed because it attempted to access privacy-sensitive data without a usage description.

In iOS 10 you need to describe the reason when the app accesses the camera, contacts, calendar etc.
Take camera for example, what you need to do is include the NSCameraUsageDescription key in your app’s Info.plist file and provide a purpose string for this key like below.
Camera Usage Description

Hope this helps,
Michael

DigitalOcean Referral Badge