How to cast AnyObject? to String in Swift 1.2

2020-08-03 05:12发布

问题:

Everything good until, I've downloaded the new version of Xcode 6.3 and Swift 1.2.

But when I compile my app again, a lots of error came out. I've dealt with most of them but there is a specific error that I couldn't figure it out the solutions

The error is "AnyObject? is not convertible to String?"

Before updating Xcode this was absolutely fine :

let firstName = self.currentUser["firstName"] as? String

回答1:

i've figured out the solution

let firstName = self.currentUser?["firstName"] as? String
if let firstname = firstName {
     // Continue
} else {
    // Handling the error.
}