Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Boolean Literals (Obj-C @YES @NO equivalent)

Tags:

swift

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!

like image 821
Timbo Avatar asked Jun 16 '14 22:06

Timbo


2 Answers

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
like image 194
Bryan Chen Avatar answered Oct 15 '22 06:10

Bryan Chen


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)
like image 44
Daniel Avatar answered Oct 15 '22 07:10

Daniel