12

First time seeing this error, can't i put '!' after 'as' in swift? Or just bug after updating my Xcode to 6.2?

let url = notification.userInfo![CallbackNotification.optionsURLKey] as! NSURL

It shows error : Expected type after 'as'

P.S: You can try download OAuthSwift from github to test this error. https://github.com/dongri/OAuthSwift

enter image description here

Chris Suen
  • 312
  • 3
  • 8

2 Answers2

22

The as! notation was not introduced until Xcode 6.3. You have Xcode 6.2, so you have to say simple as. Xcode 6.2 does not understand your as!; that is the cause of the compiler error you are getting.

(Note that if you take those ! away, then when you eventually switch to Xcode 6.3 you have will have to bring them all back again! It really is best not to change Xcode versions backwards and forwards like this. If your code was written originally with Xcode 6.3, you should stay with Xcode 6.3. The only problem is that in that case you cannot submit an app to the App Store until it goes final; right now it is still in beta.)

matt
  • 515,959
  • 87
  • 875
  • 1,141
5

You don't have to add ! to as to unwrap your optional variable in XCode 6.2

Duyen-Hoa
  • 15,384
  • 5
  • 35
  • 44
  • I using just downloading it from github to ready to import to my project. Should i remove them manually? It show nearly 50++ error inside it. – Chris Suen Mar 13 '15 at 21:27
  • If you need to work in Xcode 6.2 your only choice is to remove them. Since `as!` is not a valid combination in Swift 1.1 (the actual Swift version that is shipped with Xcode 6.2), you can mass replace it with `as` – Matthias Bauch Mar 13 '15 at 21:47
  • 1
    Excuse-me, it's Xcode 6.3 that uses Swift 1.2. In Swift 1.2, you have 2 possibilities to use as with or without !. Swift 1.2 separate the use of ! https://developer.apple.com/swift/blog/ – Duyen-Hoa Mar 13 '15 at 21:56