Compile errors after conversion to Swift 4

I followed the instructions for converting some existing code to Swift 4. I chose the "Match Swift 3 Behavior" option because the code I am trying to convert is not my code, and I didn't want to have to try to fix anything manually.


In spite of this, there were more than a dozen compiler errors after the conversion process ran.


Did I do something wrong, or is this just normal?

Answered by QuinceyMorris in 290965022

Yes, this is one a few things that don't convert properly. It's more about the SDK changing, rather than Swift.


You "attributes" dictionary probably looks something like this:


["someKey": someValue, ...]


For the key, you should either use one of the predefined static variables from here:


https://developer.apple.com/documentation/foundation/nsattributedstringkey


or if the predefined constant doesn't exist, you should use

NSAttributedStringKey ("someKey")

instead.

Xcode 9.2 has Swift 4, which has two modes: version 4 mode and version 3.2 mode. If your conversion put the project into 3.2 mode (check the build settings in the "Swift Compiler — Language" section), your old version 3.1 code is mostly compatible with 3.2, but there were some some small differences. So, yes, there might be some errors and warnings.


It's also possible that one error prevented Xcode from fully converting its line of code, leading to secondary errors.


Are the errors you're getting strange, or is it obvious what to fix?

Header Charts-Swift.h not found



framework module Charts {

umbrella header "Charts.h"



export *

module * { export * }

}



module Charts.Swift {

header "Charts-Swift.h"

requires objc

}

Probably my whole module isn't compiling.


I found an error on this line:


text.size(withAttributes: attributes).width / 2.0


The complaint is "Cannot convert value of type [String : AnyObject]? to expected argument type [NSAttributedStringKey : Any]?


'attributes' is defined as [String : AnyObject]? in this context. 'text' is a String.


I didn't write this code, so I don't know what it's actually trying to do, but it compiled before.

This sounds more like a project configuration issue, where the conversion to use modules has left the build settings in an inconsistent state.


Note that the generated header file (<whatever>-Swift.h) is used only by Obj-C code that refers to Swift declarations in the same app target. If Charts is a framework, then neither the framework target or the app target should require it. The gory details are here:


https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html


The trick is to find the section of that document that exactly matches the project structure that's giving you trouble.

Accepted Answer

Yes, this is one a few things that don't convert properly. It's more about the SDK changing, rather than Swift.


You "attributes" dictionary probably looks something like this:


["someKey": someValue, ...]


For the key, you should either use one of the predefined static variables from here:


https://developer.apple.com/documentation/foundation/nsattributedstringkey


or if the predefined constant doesn't exist, you should use

NSAttributedStringKey ("someKey")

instead.

Compile errors after conversion to Swift 4
 
 
Q