How do you declare, set properties, synthesize, and implement an int array of size 5 in Objective C? I'm writing this code for an iphone app. Thanks.
views:
2878answers:
4
A:
You shouldn't use int in this case if you want to use an array. Use NSNumber and put those 5 NSNumbers into an NSMutableArray.
@interface testClass: NSObject {
NSMutableArray* numbers;
}
@property (nonatomic, retain) NSMutableArray* numbers;
-(id)initWithValue:(NSNumber *)initialValue;
@end
@implementation testClass
@synthesize numbers;
-(id)initWithValue:(NSNumber *)initialValue {
numbers = [[NSMutableArray alloc] initWithCapacity:5];
[numbers addObject:initialValue];
return self;
}
@end
Is the code for the interface and implementation with synthesize(not tested BTW). What are you trying to accomplish?
ryanday
2009-05-26 18:59:14
Thanks for the quick response. The reason I wanted to use an array of ints is that I am trying to store 5 random/unique numbers between 1:1000 and I wanted an easy structure to check if each subsequent int is unique. It seems like this will be much more difficult using NSMutableArray. What do you suggest?
2009-05-26 19:18:09
Ahh, so maybe in a for loop you check if intArray[i] == intArray[i-1] then fail? It is certainly more overhead using the objc objects, it may just be better to use int[]. Can you keep the integer array as a local variable in the method you are generating the numbers? Or do you need it to be an accessible property of the class?
ryanday
2009-05-26 19:33:36
I want it to be an instance variable because the class is part of the model in a MVC. However, would the extra overhead be significant if the size is only 5?
2009-05-26 19:36:58
+7
A:
I think the "Cocoa-y" thing to do is hide the int array even if you use it internally. Something like:
@interface Lottery : NSObject {
int numbers[5];
}
- (int)numberAtIndex:(int)index;
- (void)setNumber:(int)number atIndex:(int)index;
@end
@implementation Lottery
- (int)numberAtIndex:(int)index {
if (index > 4)
[[NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"Index %d is out of range", index] userInfo:nil] raise];
return numbers[index];
}
- (void)setNumber:(int)number atIndex:(int)index {
if (index > 4)
[[NSException exceptionWithName:NSRangeException reason:[NSString stringWithFormat:@"Index %d is out of range", index] userInfo:nil] raise];
numbers[index] = number;
}
Chuck
2009-05-26 19:41:56
How would I do that? I think that is what I've been meaning to do originally with the int array.
2009-05-26 22:14:09
That *is* using a C array, Roger. But it removes all the hassle of memcpy and low-level details that don't really belong in a Cocoa interface.
Chuck
2009-05-26 23:37:17
doh, I'll wear the idiot hat today. Look before you postwould also be a good thing to write on my hat.
Roger Nolan
2009-05-28 18:44:24
A:
I have a class variable:
NSInteger myInt[5];
So that I could use the normal myInt[1]=0 syntax in my code, I created a property that returns an integer pointer:
@property (nonatomic, readonly) NSInteger *myInt;
Then created the following getter method:
-(NSInteger *) myInt {
return myInt
}
Now I can use something like class.myInt[1]=0;
Well, I don't know for sure this works, but it seems to. I just thought I'd put this out there if someone else wants to try it.
Ernie Thomason
2009-10-04 23:02:35