8

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.

2
  • 1
    if you can't get past this step, I heartily recommend that you REALLY MUST get Aaron Hillegas' book on Cocoa programming. Commented May 26, 2009 at 18:39
  • The thing is that I know I could get away with using an NSMutableArray or just have 5 separate ints since the size is fixed at 5, but having an array of ints would be much more elegant. I would also like to know how for the future. I've written in several other languages but I just can't seem to find a definitive answer for how to do it in objective c for the iphone. Commented May 26, 2009 at 18:46

5 Answers 5

9

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

That's overkill. Use a C array.
How would I do that? I think that is what I've been meaning to do originally with the int array.
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.
doh, I'll wear the idiot hat today. Look before you postwould also be a good thing to write on my hat.
2

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.

Comments

0

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

3 Comments

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?
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?
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?
0

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.

Comments

0

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.

Comments

Your Answer

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