如何在swift中比较Enum?
在Objective-C中,这工作正常
无法在Swift中进行编译
要么
IOS SDK中的ALAuthorizationStatus定义
enum ALAuthorizationStatus : Int {
case NotDetermined // User has not yet made a choice with regards to this application
case Restricted // This application is not authorized to access photo data.
// The user cannot change this application’s status, possibly due to active restrictions
// such as parental controls being in place.
case Denied // User has explicitly denied this application access to photos data.
case Authorized // User has authorized this application to access photos data.
}
比较运算符==
返回一个Bool
,而不是Boolean
。 以下编译:
func isAuthorized() -> Bool {
let status = ALAssetsLibrary.authorizationStatus()
return status == ALAuthorizationStatus.Authorized
}
(就我个人而言,我发现Swift编译器的错误消息有时会令人困惑,在这种情况下,问题不是==
的参数,而是不正确的返回类型。)
实际上,由于自动类型推断,下面也应该编译:
func isAuthorized() -> Bool {
let status = ALAssetsLibrary.authorizationStatus()
return status == .Authorized
}
但它会因编译器错误“Could not find member'Authorized'”而失败,除非您明确指定了status
变量的类型:
func isAuthorized() -> Bool {
let status:ALAuthorizationStatus = ALAssetsLibrary.authorizationStatus()
return status == .Authorized
}
这可能是当前Swift编译器(用Xcode 6 beta 1测试)中的一个错误。
更新:第一个版本现在在Xcode 6.1中编译。
链接地址: http://www.djcxy.com/p/21163.html