In Objective-C, you can type @YES
instead of [NSNumber numberWithBOOL:YES]
. This makes for much tidier code.
In Swift, I'm having to write NSNumber.numberWithBool(true)
, which is kind of ugly.
Is there an equivalent to @YES
and @NO
in Swift?
Thanks in advance for your help!
it is true
and 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>
read this: https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/WorkingWithCocoaDataTypes.html#//apple_ref/doc/uid/TP40014216-CH6-XID_43
Swift automatically bridges certain native number types, such as Int and Float, to NSNumber. This bridging lets you create an NSNumber from one of these types
All of the following types are automatically bridged to NSNumber:
- Int
- UInt
- Float
- Double
- Bool
Swift automatically bridges certain native number types, such as Int and Float, to NSNumber
"Using Swift with Cocoa and Objective-C" (iBook).
let foo : NSNumber = true
let bar = NSNumber(value: false)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With