声明,属性,合成,并实现INT []目标C数组 [英] Declaring, Properties, Synthesizing, and Implementing int[] array in Objective C

查看:175
本文介绍了声明,属性,合成,并实现INT []目标C数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你怎么申报,设置属性,合成,并实现规模5的目标C int数组?我写这篇code为iPhone应用程序。谢谢你。

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.

推荐答案

我觉得可可-Y的事情做的是隐藏,即使你使用它内部的int数组。是这样的:

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;
}

这篇关于声明,属性,合成,并实现INT []目标C数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆