ios – 如何在迅捷中比较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,而不是布尔值.
以下编译:
func isAuthorized() -> Bool {
    let status = ALAssetsLibrary.authorizationStatus()
    return status == ALAuthorizationStatus.Authorized
}

(就个人而言,我发现Swift编译器的错误消息有时令人困惑.
在这种情况下,问题不是==的参数,而是错误的返回类型.)

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

func isAuthorized() -> Bool {
    let status = ALAssetsLibrary.authorizationStatus()
    return status == .Authorized
}

但它失败了编译器错误“无法找到成员’授权’”,除非你
显式指定状态变量的类型:

func isAuthorized() -> Bool {
    let status:ALAuthorizationStatus = ALAssetsLibrary.authorizationStatus()
    return status == .Authorized
}

这可能是当前Swift编译器中的一个错误(使用Xcode 6 beta 1测试).

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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...