Ios 如何在swift中比较枚举?

Ios 如何在swift中比较枚举?,ios,swift,Ios,Swift,在Objective-C中,这很好 无法在Swift中编译此文件 或 IOS SDK中的授权状态定义 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 pho

在Objective-C中,这很好

无法在Swift中编译此文件

IOS SDK中的授权状态定义

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编译器的错误消息有时令人困惑。 在本例中,问题不是
==
的参数,而是返回类型不正确。)


实际上,由于自动类型推断,还应编译以下内容:

但它失败,编译器错误为“找不到成员‘授权’”,除非您 明确指定
status
变量的类型:

func isAuthorized() -> Bool {
    let status:ALAuthorizationStatus = ALAssetsLibrary.authorizationStatus()
    return status == .Authorized
}
这可能是当前Swift编译器中的一个bug(使用Xcode 6 beta 1测试)


更新:第一个版本现在在Xcode 6.1中编译。

请向我们展示IOS SDK中的enum definition.AssetsLibrary
func isAuthorized() -> Bool {
    let status:ALAuthorizationStatus = ALAssetsLibrary.authorizationStatus()
    return status == .Authorized
}