C array as property in iOS

I program for iOS and use ARC.

I am trying to use the c-array property as a property, but it reports an error.

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

Error "Property cannot have array or function type NSString *[70][254]" . "Property cannot have array or function type NSString *[70][254]" How can I solve this problem? How to declare a c-array property as a property?

Note:
This is a two-dimensional array, I think it is much easier to use a c-array, so I did not use NSArray for it.

+4
source share
4 answers

You cannot declare it in this format. As indicated in the error message, you cannot use C-style arrays in property declarations.

The new shorter syntax for arrays makes NSArray and NSMutableArray less painful. Instead

 [array objectAtIndex:3] 

you can just use

 array[3] 

I think that ultimately the advantage of using Objective-C objects outweighs the comfort of using C-style arrays.

+4
source

Surprisedly this has not already been suggested, but you can save the c-array in an NSData object. I just used this method to store an array of frames.

 @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]; 
+4
source

you cannot declare c/c++ arrays as properties, you can either use the objective-c NSArray/NSMutableArray for the property, or declare a C ++ array.

 @property (strong,nonatomic)NSArray *mappingTable; 

or declare an array of c style characters like this

 char mappingTable[70][224]; 
+2
source

If you are going to use it only as a private property of the class. Then keep it simple. skip the file YourClass.h. And write it directly in the YourClass.m file like this.

 //YourClass.m file #import "YourClass.h" @interface YourClass() @property (strong,nonatomic)NSArray *mappingTable; @end @implementation YourClass @synthesize mappingTable; @end 
-1
source

All Articles