Question

I have an object that contains the value 1 or 0 for true or false. For example, let's call my object Stuff and stuff has look like this.

@interface Stuff : NSObject {

NSString *_boolValue1;
NSString *_boolValue2;
}

When I populate my entity with values, for example: Stuff *stuff = [[Stuff alloc] init...] if I used NSLog to display the values stored in boolValue1 and boolValue2 they might output a 1 or a 0. I would like to use NSLog to output something like [stuff.boolValue1 == 1 ? @"true" : @"false"]. What am I missing? I am getting an error that the format specifies type 'id' but the argument has type 'BOOL'...

NSString *myStr = [NSString stringWithFormat:@"%@", [stuff.boolValue1 isEqualToString:@"1" ? @"true" : @"false"]];
Was it helpful?

Solution

You bracket is just in the wrong place, as the isEqualToString operator returns the bool.

NSString *myStr = [NSString stringWithFormat:@"%@", [stuff.boolValue1 isEqualToString:@"1"] ? @"true" : @"false"];

or, the simpler, more typical way, using suggestion from the comments below:

NSString *myStr = [NSString stringWithFormat:@"%@", [stuff.boolValue1 boolValue] ? @"true" : @"false"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top