Keep showing error on Swift 2.2, but can build successfully.

Hi,


After upgraded to Xcode 7.3, Swift 2.2.


In the code,

func handleInfo(info: [NSObject: AnyObject]) {
     let aps = info["aps"]
     if let message = aps!["alert"] as? String {
          .....
     }
}


it will keep showing error for this line: if let message = aps!["alert"] as? String

( Downcast from 'String?!' to 'Sting' only unwraps optionals; did you meant to use '!!'?)


But if I build and run, it will run successfully without any issue.

However, it pretty annoying that it keep showing up when I coding.


Anyway to fix that?

Hi Kenny,


Oddly enough, I did not get that warning with the latest Xcode release 7.3 (7D175)


For the fun of it I further defined the types as indicated below, but did not have any issues with your code above.


    func testHandleInfo() {
        var info: [NSObject: AnyObject] = [NSObject: AnyObject]()
        var aps: [NSObject: AnyObject] = [NSObject: AnyObject]()
        aps["alert"] = "Alert"
        info["aps"] = aps
       
        self.handleInfo(info)
    }
   
    func handleInfo(info: [NSObject: AnyObject]) {
        let aps: [NSObject: AnyObject]? = info["aps"] as? [NSObject: AnyObject]
        if let message = aps!["alert"] as? String {
            NSLog("message: \(message)")
        }
    }

It seems Xcode 7.3 tend to keep (or re-activate) old (sometimes, very very old) warnings and errors which are not caused by the current codes.


Try clean build (Product > Clean or Option-Product > Clean Build Folder...) or you may need to wait until someone find which folder to be emptied.

Keep showing error on Swift 2.2, but can build successfully.
 
 
Q