简体   繁体   中英

Swift 2.0 String try keyword

In Swift 1.2 my code was

let examplePhoneNumber =  phoneNumberUtil.getExampleNumberForType(countryCode, type: NBEPhoneNumberTypeMOBILE);

But the migration tool in XCode 7 Beta changed it to

let examplePhoneNumber = try? phoneNumberUtil.getExampleNumberForType(countryCode, type: NBEPhoneNumberTypeMOBILE);

But there is a compile error asking me to add a ; after try?

Any thoughts on how to fix this?

屏幕截图

Xcode 7 beta 6 added a new try? keyword, which attempts to call a method that may throw an error and wraps the result in an optional if it succeeds, otherwise returns nil if it throws. It allows one to handle calling methods that throw an error if you only care if it succeeded or failed and don't care what the actual error was.

If you're using try? and not try , then it doesn't need to be in a do block with a catch block. Although that doesn't appear to be why it's complaining.

The error it's giving is what Xcode 6 would give if you opened your code that was already migrated to Xcode 7 in Xcode 6. So check which version of Xcode you have it opened in. If you have it open in Xcode 7, try performing a clean and then rebuild it to see if the error goes away. If you have it open in Xcode 6, then that's your problem right there. Once you migrate to Swift 2 syntax, you can't open it again in older versions of Xcode or you'll get compile errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM