Question

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.

Was it helpful?

Solution

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;
}

OTHER TIPS

Here's something to try. In the .h file:

@property int* myIntArray;

Then, in the .m file:

@synthesize myIntArray;

If you use @synthesize, you'll need to malloc/calloc the array yourself, probably during init().

Alternatively, you could write your own accessor functions and have them malloc/calloc if called before the array has been allocated.

Either way, you'll want to free the array in the dealloc() method.

This is probably naive and wrong, as I'm just ramping up on Objective C myself, but so far it seems to be working for me.

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?

Good quick intro

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.

Whatever you do, you must be aware of the consequences.

An integer array is not reference counted. You don't know how many people are accessing it. You don't know who should deallocate it and when. So you can quite easily have a property of type int*. The setter will take a pointer and store it into the instance variable. The getter will return the contents of the instance variable. It just works.

HOWEVER you don't know when the array should be allocated or deallocated. If you have some static arrays (for example four different tables containing numbers), no problem. But if you create an array of integers with malloc (), someone should free () it. So when is this going to happen?

Because having to handle the lifetime of an array manually is rubbish, I'd recommend that you either just use an NSArray of NSNumber, or you look at NSPointerArray which can be abused to store arrays of integers with reference counting, or you create your own class like the Lottery class in a previous answer, just a bit more flexible.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top