2014-06-16 16 views
16

In Objective-C, è possibile digitare @YES anziché [NSNumber numberWithBOOL:YES]. Questo rende molto più ordinato il codice.Letterali Swift booleani (Obj-C @ YES equivalente NO)

In Swift, devo scrivere NSNumber.numberWithBool(true), che è una specie di brutto.

Esiste un valore equivalente a @YES e @NO in Swift?

Grazie in anticipo per il vostro aiuto!

+2

... perché stai usando NSNumbers a tutti? – nneonneo

+1

@nneoneo, per Core Data ... vale a dire [NSMigratePersistentStoresAutomaticallyOption: vero, NSInferMappingModelAutomaticallyOption: vero] che ha usato per essere NSDictionary * options = [ NSMigratePersistentStoresAutomaticallyOption: @YES, NSInferMappingModelAutomaticallyOption: @YES] – Timbo

risposta

26

è true e false

xcrun swift 
Welcome to Swift! Type :help for assistance. 
    1> import Foundation 
    2> var t : NSNumber = true 
t: __NSCFBoolean = {} 
    3> var f : NSObject = false 
f: __NSCFBoolean = {} 
    4> 

leggere questo: https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html#//apple_ref/doc/uid/TP40014216-CH6-XID_43

Swift colma automaticamente alcuni tipi di numero nativi, come Int e Float, a NSNumber. Questo ponte consente di creare un NSNumber da uno di questi tipi

Tutti i seguenti tipi sono ponticellati automaticamente NSNumber:

  • Int
  • UInt
  • Float
  • doppio
  • Bool
+0

Troppo facile, grazie! – Timbo

+0

Ottengo "Impossibile richiamare 'setValue' con un elenco di argomenti di tipo '(valore: BooleanLiteralConvertible, forKey: NSString!)' (Questo è Beta7) –

7

Swift colma automaticamente alcuni tipi di numero nativi, come Int e Float, per NSNumber

"Uso rapido con cacao e Objective-C" (iBook).

let foo : NSNumber = true 
let bar = NSNumber(value: false) 
0

Io non hai Sì o no in Swift, Bool vero falso che hai.

Se si utilizza objc-C e chiamare la funzione di ritorno Sì o No Puoi lanciare questa

example 
// isReady return Yes Or no of Objc-C 

if let isReady = object?.isReady { 
//is ready = true or false of your object 
} 
Problemi correlati