Boolean as a property in Cocoa

I'm sure this has been asked MANY times, but it still causes me problems. I define a property of the Boolean class as follows:

@property(readwrite,assign) BOOL namesVisible;

And it does not give compiler errors, but NSLogs is like (null). Obviously, I am doing something wrong here, but I do not understand what it is.

+3
source share
2 answers

BOOLs are just characters, either 0 or 1. Thus, you do not need to use the storage keyword in the property declaration, so it should be:

@property (readwrite) BOOL namesVisible;

Secondly, when registering BOOL, use an int format string,% d, or pass a string:

NSLog(@"My Boolean: %d, or %@", object.namesVisible, object.namesVisible ? @"Yes" : @"No");
+8
source

%@, BOOL , NO .

, BOOL NSLog, , , , . , BOOL, , . , / ( NO ); , description nil, nil , "(null)" .

/ YES, -, nil, , , . , , , EXC_BAD_ACCESS. , , , , BOOL .

:

NSLog(@"My Boolean property: %d", (int)[myObject myBooleanProperty]);

NSLog(@"My Boolean property: %@", [myObject myBooleanProperty] ? @"YES" : @"NO");

int ( , 0 1), @"YES", @"NO" . NSString (NSString), %@ .

+2

Source: https://habr.com/ru/post/1765461/


All Articles