从 2013 年冬季开始,我一直在观看 iOS 讲座(由 Paul Hegerty 撰写),我似乎无法理解为什么在 Matchisimo 程序中需要第二行代码。如果我把它注释掉,程序就会崩溃,但如果我把它留在里面,它就可以正常工作。
[cardButton setTitle:card.contents forState:UIControlStateSelected];
[cardButton setTitle:card.contents forState:UIControlStateSelected|UIControlStateDisabled];
在这一行失败:
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([CardGameAppDelegate class]));
}
如果第二行被注释掉,则会出现错误:
'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 47 beyond bounds [0 .. 46]'
卡片内容:
@property (strong, nonatomic) NSString *contents;
更新用户界面:
- (void)updateUI
{
for (UIButton *cardButton in self.cardButtons) {
Card *card = [self.game cardAtIndex:[self.cardButtons indexOfObject:cardButton]];
[cardButton setTitle:card.contents forState:UIControlStateSelected];
[cardButton setTitle:card.contents forState:UIControlStateSelected|UIControlStateDisabled];
cardButton.selected = card.isFaceUp;
cardButton.enabled = !card.isUnPlayable;
}
}