7 arrays iphone primitive objective-c
如何在Objective C中声明,设置属性,合成和实现大小为5的int数组?我正在为iphone应用程序编写此代码.谢谢.
我认为"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)
归档时间: |
|
查看次数: |
12532 次 |
最近记录: |