user4485 user4485 - 3 months ago 53
Swift Question

Error when using .capitalized (Swift 3)

I have a very simple question on the property .capitalized in Swift 3.
Here is my piece of code:

var pinkman = "yo yo yo mr white"
var capitalizeIt = pinkman.capitalized
print(capitalizeIt)


It gives me the following error:


value of type 'String' has no member 'capitalized'


Any help?

Thanks in advance!

Edit: looks like it works, i have no idea why I was getting an error in the beginning.

Answer

Assuming that you're using Xcode 8..

In your build settings, is your use Legacy Swift Version set to Yes? In which case, it'd be compiling to swift 2.3. If no, then it'll be compiling Swift 3.

Swift 2.3 doesn't have a .capitalized, it's .capitalizedString, from 3.0 onwards it's the former.

Furthermore, are you importing at least Foundation? If not, then UIKit, AppKit, Cocoa would be fine too as they automatically import Foundation.

Finally, is the swift file you've created part of an Objective-C project? Or is it part of a Swift project?