在Objective C中声明,属性,合成和实现int []数组

7 arrays iphone primitive objective-c

如何在Objective C中声明,设置属性,合成和实现大小为5的int数组?我正在为iphone应用程序编写此代码.谢谢.

Chu*_*uck 9

我认为"Cocoa-y"要做的就是隐藏int数组,即使你在内部使用它也是如此.就像是:

@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;
}
Run Code Online (Sandbox Code Playgroud)