Warnings during migration from Swift 2 to Swift 3

Compile errors part

Warning: Result of call is unused

  • One way to resolve this warning is adding @discardableResult to the declaration of the function
//Swift 2
extension UIView {
func addHighlightedAction(_ handler:()->Void) -> HighlightedAction {
return HighlightedAction(view: self, handler: handler)}
}
buttonView.addHighlightedAction {…}
---------------------------------------------------------------
//Swift 3
extension UIView {
@discardableResult func addHighlightedAction(_ handler: @escaping ()->Void) -> HighlightedAction {
return HighlightedAction(view: self, handler: handler)}
}
buttonView.addHighlightedAction {…}
  • The other way is to use ‘_=’ in front of the function
_ = imageBadge.autoAlignAxisToSuperviewAxis(...)

Warning: String interpolation produces a debug description for an optional value; did you mean to make this explicit?

Adding a default value in case the String is nil

//Swift 2
print(“Could not add \(tag.label) to empty row.”)
//Swift 3
print(“Could not add \(tag.label ?? “”) to empty row.”)

Warning: ‘init(nilLiteral:)’ is deprecated: use JSON.null instead. Will be removed in future versions

//Swift 2
let json = try JSON(data: download.receivedData!)
if json == nil {..}
--------------------------------------------------------------------
//Swift 3
let json = try JSON(data: download.receivedData!)
if json == JSON.null {..}

Warning: ‘M_PI’ is deprecated: Please use ‘Double.pi’ or ‘.pi’ to get the value of correct type and avoid casting.

//Swift 2
transform = transform.rotated(by: CGFloat(M_PI))
//Swift 3
transform = transform.rotated(by: CGFloat(Double.pi))

Warning: Comparing non-optional value of type ‘Calendar’ to nil always returns false

//Swift 2
let calendar = Calendar(identifier: Calendar.Identifier.gregorian)
if calendar == nil { return nil } //this line should be removed
//Swift 3
let calendar = Calendar(identifier: Calendar.Identifier.gregorian)

Warning: Non-optional expression of type ‘String’ used in a check for optionals

//Xcode conversion
if let type : String = item[“type”].stringValue, type == “activity” { ...}
//Swift 3
let type = item["type"].stringValue
if type == "activity" {...}

Warning: Non-optional expression of type ‘NSDictionary’ used in a check for optionals

//Xcode conversion
if let info: NSDictionary = notif.userInfo! as NSDictionary {...}
//Swift 3
if let info = notif.userInfo as NSDictionary?,

Warning: Non-optional expression of type ‘CGSize’ used in a check for optionals

//Xcode conversion
if let keyboardSize: CGSize = value.cgRectValue.size {...}
//Swift 3
let keyboardSize: CGSize = value.cgRectValue.size

Warning: Variable ‘self’ was written to, but never read

//Swift 2
dispatch_async_main { [weak self]
handler(feedItemManager)
}
//Swift 3
dispatch_async_main { [weak self]
if self == nil {return}
handler(feedItemManager)
}

Warning: Implicit conversion of out of range value from ‘const CGFloat’ (aka ‘const double’) to ‘bool’ changes value from 2 to true

//Obj-C
if (self.shouldBlurBackground && __blurRadius) {..}
//Obj-C in Xcode 8
if (self.shouldBlurBackground && __blurRadius > 0) {..}

Warning: Implicit conversion loses floating-point precision: ‘CGFloat’ (aka ‘double’) to ‘float’

In this case the function is receiving a Double value, when is expecting a Float value. Fortunately, the library math provides two functions

  • extern double sin(double);
  • extern float sinf(float);
//Obj-C
CGFloat angularVelocity = fabs((fabs(pushVelocity) * sinf(angle)) / fabs(radius));
//Obj-C in Xcode 8
CGFloat angularVelocity = fabs((fabs(pushVelocity) * sin(angle)) / fabs(radius));

Warning: Implicit conversion turns floating-point number into integer: ‘CGFloat’ (aka ‘double’) to ‘uint32_t’ (aka ‘unsigned int’)

//Obj-C
vImageBoxConvolve_ARGB8888(&effectInBuffer, &effectOutBuffer, NULL, 0, 0, radius, radius, 0, kvImageEdgeExtend);
//Obj-C in Xcode 8
vImageBoxConvolve_ARGB8888(&effectInBuffer, &effectOutBuffer, NULL, 0, 0, (uint32_t)radius,(uint32_t) radius, 0, kvImageEdgeExtend);

Warning: Implicit conversion from nullable pointer ‘CGContextRef _Nullable’ (aka ‘struct CGContext *’) to non-nullable pointer type ‘CGContextRef _Nonnull’ (aka ‘struct CGContext *’)

//Obj-C
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
//Obj-C in Xcode 8
CGContextRef context = UIGraphicsGetCurrentContext();
if(context != nil){
[self.layer renderInContext:context];
}

Warning: ‘+=’ is deprecated: Mixed-type addition is deprecated. Please use explicit type conversion.

In this case “numBytesReceived” is Int64 type and data.count is type Int

//Swift 2
networkRequest.numBytesReceived += data.count
//Swift 3
networkRequest.numBytesReceived += Int64(data.count)

Warning: ‘global(priority:)’ was deprecated in iOS 8.0

//Swift 2
DispatchQueue.global(priority: DispatchQueue.GlobalQueuePriority.default).async(execute: {…}
//Swift 3
DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async(execute: {…}
A single golf clap? Or a long standing ovation?

By clapping more or less, you can signal to us which stories really stand out.