如何在 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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)