我是Objective C的新手,我似乎无法找到如何做到这一点(如果可能的话)。
我有NSArray
填充了4张卡片(卡片=我自己的班级),我想将标签的文字设置为我的卡片对象所持有的NSString
。
在Java中我会做这样的事情: 如果我有一张带有4张卡的卡片阵列,而且标签是一个字符串。
theLabel = deck[2].getLabel();
这似乎不适用于目标C.到目前为止我在Objective C中的代码:
- (IBAction)nextCard:(id)sender {
theLabel.text = [deck objectAtIndex:j].getLabel;
theImage.image = [deck objectAtIndex:j].getImage;
}
每次我点击iPhone上的按钮时,nextCard都会调用。 j是一个普通的int,它将跟踪要显示的卡片。
我在创建数组时看起来像这样:
- (void)viewDidLoad
{
[super viewDidLoad];
ah = [[Card alloc]init];
[ah setLabel:@"label1"];
[ah setCardImage: [UIImage imageNamed:@"3.png" ]];
as = [[Card alloc]init];
[as setLabel:@"label2"];
[as setCardImage: [UIImage imageNamed:@"2.png" ]];
ac = [[Card alloc]init];
[ac setLabel:@"labbel3"];
[ac setCardImage: [UIImage imageNamed:@"1.png" ]];
ad = [[Card alloc]init];
[ad setLabel:@"label4"];
[ad setCardImage: [UIImage imageNamed:@"4.png" ]];
deck = [NSMutableArray arrayWithCapacity:25];
[deck addObject:ah];
[deck addObject:as];
[deck addObject:ac];
[deck addObject:ad];
}
所以我基本上希望能够在NSArray中使用ob对象存储方法。
非常感谢答案,提前谢谢!
答案 0 :(得分:1)
在objective-C中,生成的getter没有get前缀,因此您可以这样使用它:
theLabel = deck[2].label;
或者:
theLabel= [deck[2] label];
答案 1 :(得分:0)
检查一下:
[deck objectAtIndex:0].label;
或:
[[deck objectAtIndex:0] label];
或:
[[deck objectAtIndex:0] yourMethodWithParameter:@"foo"];
答案 2 :(得分:0)
在Objective-C中,在大多数情况下,访问器方法是自动生成的。
让我们说我们有一个名为“对象”的属性
getter方法的名称不是getObject
,其中“object”是您要访问的属性。
它只是通过像object
所以在你的情况下它应该是这样的:
NSString *theLabelString = [[deck objectAtIndex:j] label];
//or this:
NSString *theLabelString = [deck[j] label];
//or this:
NSString *theLabelString = deck[j].label;