简体   繁体   中英

Making an Integer Array in Objective-C

I want to have an internal int array for my class, but I can't seem to get XCode to let me. The array size needs to be set on initialization so I can't put the size directly into the interface.

At the moment I've been trying:

@interface TestClass : NSObject {
  int test[];
}

But it tells me that I'm not allowed. How to I refer to it in my interface, and then how do I allocate it when I create the implementation?

Sorry for a somewhat standard sounding question, but I can't seem to find the answer I need from searching.

edit: I want to use an array because it's apparently much faster than using an NSArray

You can use a number of methods to overcome this problem, but the easiest is to simply make the instance variable a pointer, like this:

@interface TestClass : NSObject {
    int *test;
}

@property int *test;

@end

Synthesizing the property will give it getter and setter methods which you can use to set its contents:

@implementation TestClass
@synthesize test;

//contents of class

@end

You can then use it like this:

TestClass *pointerTest = [[TestClass alloc] init];

int *array = (int *)malloc(sizeof(int) * count);
//set values

[pointerTest setTest:array];
[pointerTest doSomething];

However, using objects like NSNumber in an NSArray is a better way to go, perhaps you could do something like this:

@interface TestClass : NSObject {
    NSArray *objectArray;
}

@property (nonatomic, strong) NSArray *objectArray;

@end

@implementation TestClass
@synthesize objectArray;

//contents of class

@end

You can then set its contents with a pointer to an NSArray object:

NSArray *items = [NSArray arrayWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:2], nil];

TestClass *arrayClass = [[TestClass alloc] init];
[arrayClass setItems:items];

[arrayClass doSomething];

When retaining objects upon setting them (like the previous example), always make sure you deallocate the object in the classes dealloc method.

AC array is just a sufficiently sized raw memory buffer. Foundation has a nice wrapper around raw memory that frees you from all the manual memory management: NSMutableData

The following approach gives you automatic memory management plus proper encapsulation.

@interface TestClass : NSObject

@property (nonatomic, readonly) int *testArray;
@property (nonatomic, readonly) NSUInteger testArraySize;

@end


@implementation TestClass
{
    NSMutableData *_testData;
}

- (id)initWithSize:(NSUInteger)size
{
    self = [self init];
    if (self != nil) {
        _testData = [NSMutableData dataWithLength:size];
    }
}

- (int *)testArray
{
    return [_testData mutableBytes];
}

- (NSUInteger)testArraySize
{
    return [_testData length];
}

@end

As you see, the ivar does not have to be declared in the @interface.

Try something like this:

@interface TestClass : NSObject
{
    int *_test; 
}

@property (assign) int *test;

@end

@implementation TestClass

- (instancetype)init
{
    if (self = [super init])
    {
        _test = malloc(sizeof(int) * 20);
    }
    return self;
}

- (int *)test
{
    return _test;
}

- (void)setTest:(int*)test
{
    memcpy(&_test, &test, sizeof(_test));
}

- (void)dealloc
{
    free(_test);
}

@end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM