开发者

How to wrap BOOL in NSValue

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜