开发者

How to wrap BOOL in NSValue

开发者 https://www.devze.com 2023-03-26 03:46 出处:网络
Having trouble wrapping a BOOL value in NSValue. I tried this: [NS开发者_开发百科Value valueWithPointer:[NSNumber numberWithBool:YES]]

Having trouble wrapping a BOOL value in NSValue.

I tried this:

[NS开发者_开发百科Value valueWithPointer:[NSNumber numberWithBool:YES]]


An NSNumber is an NSValue subclass. As such,[NSNumber numberWithBool:YES] is already wrapping it in an NSValue for you.


Actually NSNumber is a heavier-weight descendant of NSValue. If you actually want to use an NSValue here's how you'd do that:

BOOL bool_value = YES;
[NSValue valueWithBytes:&bool_value objCType:@encode(BOOL)]


Here's some example code to expand on answer by Joshua Weinberg.

➤ To convert a primitive BOOL to a pseudo-Boolean object:

NSNumber* isWinning = [NSNumber numberWithBool:YES];

➤ To convert a pseudo-Boolean object to a primitive BOOL:

BOOL winner = [isWinning boolValue];

See NSNumber class reference.


Use an Objective-C literal:

@(YES)
0

精彩评论

暂无评论...
验证码 换一张
取 消