调出一个数组,objectAtIndex
Call out of an Array, objectAtIndex
我有一个 Array
有 10 个对象。我拿第一个对象并用 String
.
将它放入我的 Label
然后我想要一个将 objectAtIndex
增加 1 的方法。
这是我的代码:
//.m
@interface GameViewController () {
NSInteger _labelIndex;
}
@property (nonatomic) NSArray *playerArray;
@property (nonatomic) NSString *playerLabel;
- (void)viewDidLoad {
[super viewDidLoad];
self.playerArray = [NSArray arrayWithObjects:@"FIRST", @"SECOND", @"THIRD", @"FOURTH", @"FIFTH", @"SIXT", @"SEVENTH", @"EIGTH", @"NINTH", @"TENTH", nil];
_labelIndex = 0;
[self updateTurnLabel];
self.turnLabel.text = [NSString stringWithFormat:@"YOUR %@ DRAW?", self.playerLabel];
}
这里我把这个方法称为另一个方法:
-(void) flipDraws {
self.boardView.userInteractionEnabled = NO;
[self updateTurnLabel];
CardView *cv1 = (CardView *) self.turnedDrawViews[0];
CardView *cv2 = (CardView *) self.turnedDrawViews[1];
}
这是我的方法:
-(void) updateTurnLabel {
self.playerLabel = [self.playerArray objectAtIndex:_labelIndex % self.playerArray.count]; _labelIndex++;
}
我用 for Loop
试过了,但没有任何反应。我尝试设置 objectAtIndex:1
但我的 Method
没有被调用。
我做错了什么?
你在 objectAtIndex:
.
方法中向什么添加 +1
你应该维护一个变量来跟踪当前使用的索引,然后在你的方法中使用这个:
-(void) updateTurnLabel {
self.playerLabel = [self.playerArray objectAtIndex:currentIndex+1];
}
{
int a;
}
- (void)viewDidLoad {
[super viewDidLoad];
a = 0;
self.playerArray = [NSArray arrayWithObjects:@"FIRST", @"SECOND", @"THIRD", @"FOURTH", @"FIFTH", @"SIXT", @"SEVENTH", @"EIGTH", @"NINTH", @"TENTH", nil];
self.playerLabel = [self.playerArray objectAtIndex:a];
self.turnLabel.text = [NSString stringWithFormat:@"YOUR %@ DRAW?", self.playerLabel];
}
-(void) updateTurnLabel {
a +=1;
if (!a<[playerArray count])
{
a = 0;
}
self.playerLabel = [self.playerArray objectAtIndex:a];
}
调用self.turnLabel.text = [NSString stringWithFormat:@"YOUR %@ DRAW?", self.playerLabel];在 [self updateTurnLabel] 之后;
我有一个 Array
有 10 个对象。我拿第一个对象并用 String
.
Label
然后我想要一个将 objectAtIndex
增加 1 的方法。
这是我的代码:
//.m
@interface GameViewController () {
NSInteger _labelIndex;
}
@property (nonatomic) NSArray *playerArray;
@property (nonatomic) NSString *playerLabel;
- (void)viewDidLoad {
[super viewDidLoad];
self.playerArray = [NSArray arrayWithObjects:@"FIRST", @"SECOND", @"THIRD", @"FOURTH", @"FIFTH", @"SIXT", @"SEVENTH", @"EIGTH", @"NINTH", @"TENTH", nil];
_labelIndex = 0;
[self updateTurnLabel];
self.turnLabel.text = [NSString stringWithFormat:@"YOUR %@ DRAW?", self.playerLabel];
}
这里我把这个方法称为另一个方法:
-(void) flipDraws {
self.boardView.userInteractionEnabled = NO;
[self updateTurnLabel];
CardView *cv1 = (CardView *) self.turnedDrawViews[0];
CardView *cv2 = (CardView *) self.turnedDrawViews[1];
}
这是我的方法:
-(void) updateTurnLabel {
self.playerLabel = [self.playerArray objectAtIndex:_labelIndex % self.playerArray.count]; _labelIndex++;
}
我用 for Loop
试过了,但没有任何反应。我尝试设置 objectAtIndex:1
但我的 Method
没有被调用。
我做错了什么?
你在 objectAtIndex:
.
你应该维护一个变量来跟踪当前使用的索引,然后在你的方法中使用这个:
-(void) updateTurnLabel {
self.playerLabel = [self.playerArray objectAtIndex:currentIndex+1];
}
{
int a;
}
- (void)viewDidLoad {
[super viewDidLoad];
a = 0;
self.playerArray = [NSArray arrayWithObjects:@"FIRST", @"SECOND", @"THIRD", @"FOURTH", @"FIFTH", @"SIXT", @"SEVENTH", @"EIGTH", @"NINTH", @"TENTH", nil];
self.playerLabel = [self.playerArray objectAtIndex:a];
self.turnLabel.text = [NSString stringWithFormat:@"YOUR %@ DRAW?", self.playerLabel];
}
-(void) updateTurnLabel {
a +=1;
if (!a<[playerArray count])
{
a = 0;
}
self.playerLabel = [self.playerArray objectAtIndex:a];
}
调用self.turnLabel.text = [NSString stringWithFormat:@"YOUR %@ DRAW?", self.playerLabel];在 [self updateTurnLabel] 之后;