2014-06-20 101 views
7

在Objective-C能正常工作如何迅速比较枚举?

enter image description here

在IOS

enter image description here

或者

enter image description here

ALAuthorizationStatus定义不能编译这个斯威夫特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. 
} 
+1

请告诉我们您的枚举定义。从IOS SDK – Alexander

+1

AssetsLibrary – Charlie

回答

3

比较运算符==返回Bool而不是Boolean。 以下编译:

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

(就个人而言,我觉得从斯威夫特编译器的错误信息有时会混淆 在这种情况下,这个问题是不是==的论点,但不正确的返回类型。)


其实,下面还应该编译由于自动类型推断:

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

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

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

这可能是在目前雨燕的错误编译器(在Xcode 6的β1测试)。

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