[ACCEPTED]-Create an array of integers property in Objective-C-properties
This should work:
@interface MyClass
{
int _doubleDigits[10];
}
@property(readonly) int *doubleDigits;
@end
@implementation MyClass
- (int *)doubleDigits
{
return _doubleDigits;
}
@end
0
C arrays are not one of the supported data 13 types for properties. See "The Objective-C 12 Programming Language" in Xcode documentation, in 11 the Declared Properties page:
Supported Types
You can declare 10 a property for any Objective-C class, Core 9 Foundation data type, or “plain old data” (POD) type 8 (see C++ Language Note: POD Types). For 7 constraints on using Core Foundation types, however, see 6 “Core Foundation.”
POD does not include 5 C arrays. See http://www.fnal.gov/docs/working-groups/fpcltf/Pkg/ISOcxx/doc/POD.html
If you need an array, you 4 should use NSArray or NSData.
The workarounds, as 3 I see it, are like using (void *) to circumvent 2 type checking. You can do it, but it makes 1 your code less maintainable.
Like lucius said, it's not possible to have 6 a C array property. Using an NSArray
is the way 5 to go. An array only stores objects, so 4 you'd have to use NSNumber
s to store your ints. With 3 the new literal syntax, initialising it 2 is very easy and straight-forward:
NSArray *doubleDigits = @[ @1, @2, @3, @4, @5, @6, @7, @8, @9, @10 ];
Or:
NSMutableArray *doubleDigits = [NSMutableArray array];
for (int n = 1; n <= 10; n++)
[doubleDigits addObject:@(n)];
For 1 more information: NSArray Class Reference, NSNumber Class Reference, Literal Syntax
I'm just speculating:
I think that the variable 13 defined in the ivars allocates the space 12 right in the object. This prevents you from 11 creating accessors because you can't give 10 an array by value to a function but only 9 through a pointer. Therefore you have to 8 use a pointer in the ivars:
int *doubleDigits;
And then allocate 7 the space for it in the init-method:
@synthesize doubleDigits;
- (id)init {
if (self = [super init]) {
doubleDigits = malloc(sizeof(int) * 10);
/*
* This works, but is dangerous (forbidden) because bufferDoubleDigits
* gets deleted at the end of -(id)init because it's on the stack:
* int bufferDoubleDigits[] = {1,2,3,4,5,6,7,8,9,10};
* [self setDoubleDigits:bufferDoubleDigits];
*
* If you want to be on the safe side use memcpy() (needs #include <string.h>)
* doubleDigits = malloc(sizeof(int) * 10);
* int bufferDoubleDigits[] = {1,2,3,4,5,6,7,8,9,10};
* memcpy(doubleDigits, bufferDoubleDigits, sizeof(int) * 10);
*/
}
return self;
}
- (void)dealloc {
free(doubleDigits);
[super dealloc];
}
In this 6 case the interface looks like this:
@interface MyClass : NSObject {
int *doubleDigits;
}
@property int *doubleDigits;
Edit:
I'm really 5 unsure wether it's allowed to do this, are 4 those values really on the stack or are 3 they stored somewhere else? They are probably 2 stored on the stack and therefore not safe 1 to use in this context. (See the question on initializer lists)
int bufferDoubleDigits[] = {1,2,3,4,5,6,7,8,9,10};
[self setDoubleDigits:bufferDoubleDigits];
This works
@interface RGBComponents : NSObject {
float components[8];
}
@property(readonly) float * components;
- (float *) components {
return components;
}
0
You can put this in your .h file for your 2 class and define it as property, in XCode 1 7:
@property int (*stuffILike) [10];
I found all the previous answers too much 10 complicated. I had the need to store an 9 array of some ints as a property, and found 8 the ObjC requirement of using a NSArray 7 an unneeded complication of my software.
So 6 I used this:
typedef struct my10ints {
int arr[10];
} my10ints;
@interface myClasss : NSObject
@property my10ints doubleDigits;
@end
This compiles cleanly using 5 Xcode 6.2.
My intention was to use it like this:
myClass obj;
obj.doubleDigits.arr[0] = 4;
HOWEVER, this 4 does not work. This is what it produces:
int i = 4;
myClass obj;
obj.doubleDigits.arr[0] = i;
i = obj.doubleDigits.arr[0];
// i is now 0 !!!
The 3 only way to use this correctly is:
int i = 4;
myClass obj;
my10ints ints;
ints = obj.doubleDigits;
ints.arr[0] = i;
obj.doubleDigits = ints;
i = obj.doubleDigits.arr[0];
// i is now 4
and so, defeats 2 completely my point (avoiding the complication 1 of using a NSArray).
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.