NSArray 填充布尔值 [英] NSArray filled with bool
本文介绍了NSArray 填充布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个布尔值的 NSArray.请问有多少人这样做?
I am trying to create an NSArray of bool values. How many I do this please?
NSArray *array = [[NSArray alloc] init];
array[0] = YES;
这对我不起作用.
谢谢
推荐答案
NSArrays 不是 c 数组.您无法使用 array[foo];
访问 NSArray 的值但是您可以在 Objective-C 中使用 c 类型数组而不会出现问题.
NSArrays are not c-arrays. You cant access the values of an NSArray with array[foo];
But you can use c type arrays inside objective-C without problems.
Objective-C 方法是:
The Objective-C approach would be:
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:[NSNumber numberWithBool:YES]];
//or
[array addObject:@(NO)];
...
BOOL b = [[array objectAtIndex:0] boolValue];
....
[array release];
<小时>
clang 的新版本,现在是 Objective-c 的标准编译器,理解 对象下标.当您使用新版本的 clang 时,您将能够使用 array[0] = @YES
New versions of clang, the now standard compiler for objective-c, understand Object subscripting. When you use a new version of clang you will be able to use array[0] = @YES
这篇关于NSArray 填充布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文