2014-06-20 10 views
7

in Objective-C das funktioniert gutWie kann man Enum in swift vergleichen?

enter image description here

Kann dies nicht

enter image description here

Oder

enter image description here

ALAuthorizationStatus Definition in IOS in Swift kompilieren 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

Bitte zeigen Sie uns Ihre Enum-Definition. – Alexander

+1

AssetsLibrary von IOS SDK – Charlie

Antwort

3

Der Vergleichsoperator == ein Bool zurückgibt, nicht Boolean. Folgende compiliert:

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

(. Persönlich finde ich die Fehlermeldungen aus den Swift-Compiler manchmal verwirrend In diesem Fall war das Problem nicht die Argumente von ==, aber der falschen Rückgabetyp.)


Eigentlich sollte die folgende auch aufgrund der automatischen Typinferenz kompilieren:

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

aber es funktioniert nicht mit dem Compiler-Fehler „kann nicht gefunden wird Mitglied‚Authorized‘“, es sei denn, Sie ausdrücklich auf die Art des status Variable angeben :

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

Dies ist ein Fehler in der aktuellen Swift sein könnte Compiler (getestet mit Xcode 6 Beta 1).

Aktualisierung: Die erste Version kompiliert jetzt in Xcode 6.1.