4

I am programming for iOS, and using ARC.

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

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

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

Note:
This is a two dimensional array, I think it is much easier to just use c-array, so I didn't use NSArray for it.

2
  • You can't declare c-arrays as properties. That's what the message says and it isn't lying. Commented Mar 4, 2013 at 12:56
  • you should use NSArray or declare pure c++ array Commented Mar 4, 2013 at 12:56

4 Answers 4

6

Surprised this hasn't been suggested already but you can store 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];
Sign up to request clarification or add additional context in comments.

Comments

4

You can't declare it in that format. As the error message states you can't use C-style arrays in property declarations.

The new shorter syntax for arrays makes NSArray and NSMutableArray less of a pain. Instead of

[array objectAtIndex:3]

you can simply use

array[3]

I think in the long run the benefit of using Objective-C objects will outweigh the comfort of using C-style arrays.

Comments

2

you can not declare c/c++ arrays as properties, you could either use objective-c NSArray/NSMutableArray for property or you could declare c++ array.

@property (strong,nonatomic)NSArray *mappingTable;

or declare pure c style character array like this

char mappingTable[70][224];

1 Comment

OK, thanks! Then I declare the NSString array as public variable. It works now!
-1

If you are only going to use it as a private property of the class. Then keep it simple. skip the YourClass.h file. 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.