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

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!

Swift Boolean Literals (Obj-C @YES @NO equivalent , In Objective-C, you can type @YES instead of [NSNumber numberWithBOOL:​YES] . This makes for much tidier code. In Swift, I'm having to  Whereas YES and NO are simply BOOL's and they are defined as- Call method after asynchronous request obj-c. // Swift self.showViewController

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)

BOOL / bool / Boolean / NSCFBoolean, Objective-C defines the BOOL type to encode truth values. and instance variables and use YES and NO when representing literal Boolean values. Because Indeed, two integers are equal if and only if their difference is 0 . Bool represents Boolean values in Swift. Create instances of Bool by using one of the Boolean literals true or false, or by assigning the result of a Boolean method or operation to a variable or constant.

I you don't have Yes Or no in Swift, Bool true false you have.

If you use Objc-C and call function return Yes or No you may cast this

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

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

Swift Boolean Literals (Obj-C @ YES @NO equivalent), es true y false xcrun swift Welcome to Swift! Type :help for assistance. 1> import Foundation 2> var t : NSNumber = true t: __NSCFBoolean = {} 3> var f  Boolean Literals. There are two boolean literals in swift. They are true and false.. Example 11: How to use Boolean literals in Swift? let result:Bool = false. In the above program, false is a Boolean literal which is assigned to the constant result.

Objective-C Literals, In Objective-C, any character, numeric or boolean literal prefixed with the '@' character will NSNumber *yesNumber = @YES; // equivalent to [NSNumber To support @YES and @NO expressions, these macros are now defined using new  The difference is that by using @ you are creating an NSNumber instance, thus an object.Yes and No are simply primitive Boolean values not objects.. The @ is a literal a sort of shortcut to create an object you have it also in strings @"something", dictionaries @{"key": object}, arrays: @[object,] and numbers: @0,@1@345 or expressions @(3*2).

Bool, Bool. A value type whose instances are either true or false . SDK. Xcode 6.0.1+ in Swift. Create instances of Bool by using one of the Boolean literals true or false , or by Returns a Boolean value indicating whether two values are not equal. Unofficial mirror of the WebKit SVN repository. Contribute to WebKit/webkit development by creating an account on GitHub.

bool(forKey:), Swift; Objective-C Returns the Boolean value associated with the specified key. SDKs. iOS 2.0+; macOS 10.0+; Mac Catalyst 13.0+; tvOS 9.0+; watchOS 2.0+ strings "true", "YES", and "1", and the numbers 1 and 1.0—to the Boolean value true . The same is true for certain ”falsy” values—such as the strings "false", "NO",​  "This is a Swift string literal" #"This is also a Swift string literal"# ####"So is this"#### This proposal comes as a natural extension of the new multi-line string literals added in Swift 4 ( SE-0165 ), and would make it even easier to do work with data formats like JSON and XML.

Comments
  • ...why are you using NSNumbers at all?
  • @nneoneo, for Core Data ... i.e. [NSMigratePersistentStoresAutomaticallyOption:true, NSInferMappingModelAutomaticallyOption:true] which used to be NSDictionary *options = [ NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES]
  • I get "Cannot invoke 'setValue' with an argument list of type '(value: BooleanLiteralConvertible, forKey: NSString!)' (this is Beta7)