Objective c 用布尔填充数组时发出警告
我有以下代码:Objective c 用布尔填充数组时发出警告,objective-c,ios,nsmutablearray,boolean,Objective C,Ios,Nsmutablearray,Boolean,我有以下代码: BOOL booleanValue = TRUE; [arrayZone replaceObjectAtIndex:indexZone withObject:booleanValue]; 这段代码给了我一个警告,上面写着: incompatible integer to pointer conversion: sending BOOL to parameter of type 'id' 不兼容的整数到指针转换: 正在将BOOL发送到“id”类型的参数 为什么?您只能在NSAr
BOOL booleanValue = TRUE;
[arrayZone replaceObjectAtIndex:indexZone withObject:booleanValue];
这段代码给了我一个警告,上面写着:
incompatible integer to pointer conversion:
sending BOOL to parameter of type 'id'
不兼容的整数到指针转换:
正在将BOOL发送到“id”类型的参数
为什么?您只能在
NSArray
中存储对象,而不能存储基本类型
干杯你需要用一个
NSNUmber
框住你的BOOL
,如下所示:
BOOL booleanValue = TRUE;
[arrayZone replaceObjectAtIndex:indexZone withObject:[NSNumber numberWithBool:booleanValue]];
然后,要检索BOOL
值,请使用boolValue
取消装箱:
BOOL b = [[arrayZone objectAtIndex:index] boolValue];
这个问题似乎是重复的
本质上,BOOL是一种基本类型-您必须将其包装在对象中以消除警告。BOOL是一种基本类型,您的数组需要一个对象。这就是为什么你需要把它写进一个数字里。但对于较新的xcode,您只需键入@YES或@NO,xcode就会将其视为一个数字Withbool。 因此,不是:
BOOL booleanValue = TRUE;
[arrayZone replaceObjectAtIndex:indexZone withObject:[NSNumber numberWithBool:booleanValue]];
您现在可以使用
[arrayZone replaceObjectAtIndex:indexZone withObject:@YES];