2011-04-26 58 views
4

數組我有這樣的代碼:警告時,我充滿BOOL

BOOL booleanValue = TRUE; 
[arrayZone replaceObjectAtIndex:indexZone withObject:booleanValue]; 

此代碼給了我,說一個警告:

incompatible integer to pointer conversion: 
sending BOOL to parameter of type 'id'

爲什麼?

回答

14

你需要打你BOOLNSNUmber這樣的:

BOOL booleanValue = TRUE; 
[arrayZone replaceObjectAtIndex:indexZone withObject:[NSNumber numberWithBool:booleanValue]]; 

然後,檢索您的BOOL值,你拆箱它使用boolValue

BOOL b = [[arrayZone objectAtIndex:index] boolValue]; 
+0

aaaaaah好的.....謝謝 – CrazyDev 2011-04-26 14:51:16

0

只能在NSArray中存儲對象,而不是基本類型。

乾杯

+0

我不能有一個數組或BOOL mutablearray ??? – CrazyDev 2011-04-26 14:48:32

0

這問題似乎是重複的Objective C Boolean Array

實質上,BOOL是一種基本類型 - 您必須將其包裝在一個Object中才能擺脫警告。

0

BOOL是一種原始類型,您的數組需要一個對象。這就是爲什麼你需要把它包裝在NSNumber中。但是對於更新的xcode,您只需鍵入@YES或@NO,xcode將它視爲numberWithBool。 因此,而不是:

BOOL booleanValue = TRUE; 
    [arrayZone replaceObjectAtIndex:indexZone withObject:[NSNumber numberWithBool:booleanValue]]; 

現在可以使用

[arrayZone replaceObjectAtIndex:indexZone withObject:@YES];