So, you downloaded the shiny new Xcode 9 and migrated your project to Swift 4.

As hinted, you disabled @objc inference right away:

Then, subtle issues may have popped up. For example, this KVO-based code – which ran beautifully under Swift 3 – suddenly stopped working:

for (key, value) in jsonDict {

if self.value(forKey: key) != nil {

self.setValue(value, forKey: key)

}

}

According to my best knowledge, here are the options to fix the problem:

  • Re-enable “Swift 3 @objc Inference” (only works if you migrate an existing project from Swift 3)
  •  Mark the affected methods and properties as @objc
  • Re-enable ObjC inference for the entire class using @objcMembers

Re-enabling @objc inference leaves you with some warnings, but it’s the quickest solution. Note that it’s only available for projects migrated from an earlier Swift version.

The other two options are more tedious and require some code-digging and extensive testing.

See also https://github.com/apple/swift-evolution/blob/master/proposals/0160-objc-inference.md

Interested in Swift programming?
Check out my video courses:

Leave A Comment