2013-03-04 33 views
4

我正在爲iOS編程,並使用ARC。C數組作爲iOS中的財產

我想使用c數組作爲屬性,但它報告錯誤。

@property (strong, nonatomic)NSString *mappingTable[70][254]; 

錯誤是"Property cannot have array or function type NSString *[70][254]"。我怎麼解決這個問題?我怎樣才能將c-array聲明爲屬性?

注:
這是一個二維數組,我覺得這是很容易只使用C-陣列,所以我沒有使用的NSArray它。

+0

您不能將c-arrays聲明爲屬性。這就是信息所說的,而不是說謊。 – Fogmeister 2013-03-04 12:56:06

+0

您應該使用'NSArray'或聲明純C++數組 – nsgulliver 2013-03-04 12:56:07

回答

4

你不能以這種格式聲明它。由於錯誤消息指出您不能在屬性聲明中使用C風格的數組。

新的數組較短的語法使NSArray和NSMutableArray減少了痛苦。取而代之的

[array objectAtIndex:3] 

,你可以簡單地使用

array[3] 

我認爲從長遠來看,使用Objective-C的對象將超過使用C風格數組的舒適性的好處。

2

你不能聲明c/c++數組作爲屬性,你可以使用objective-c NSArray/NSMutableArray作爲屬性,或者你可以聲明C++數組。

@property (strong,nonatomic)NSArray *mappingTable; 

或聲明純C風格的字符數組這樣

char mappingTable[70][224]; 
+0

好的,謝謝!然後我將NSString數組聲明爲公共變量。它現在有效! – nan 2013-03-04 13:25:53

4

驚訝這還沒有提出已經但是你可以存儲在一個NSData對象的C數組。我只是用這種方法來存儲一個幀數組。

@property (nonatomic) NSData *framesArray; 


// Create and initialize your c-style frames array 
CGRect frames[numberOfFrames]; 
... 
self.framesArray = [NSData dataWithBytes:frames length:(sizeof(CGRect) * numberOfFrames)]; 


// To Access the property 
NSUInteger arraySize = [self.framesArray length]/sizeof(CGRect); 
CGRect *frames = (CGRect *) [self.framesArray bytes]; 
-1

如果您只打算將其作爲該類的私有屬性使用。然後保持簡單。 跳過YourClass.h文件。並將其直接寫入YourClass.m文件中。

//YourClass.m file 


#import "YourClass.h" 

@interface YourClass() 

@property (strong,nonatomic)NSArray *mappingTable; 

@end 

@implementation YourClass 
@synthesize mappingTable; 
@end