【发布时间】:2011-02-24 00:38:19
【问题描述】:
我遇到了数组问题,我想从数组中随机选取一个对象, 然后删除它并删除“if”语句中指定的其他对象。
我做了什么..
在.h中
NSMutableArray *squares;
int s;
NSString *randomN;
接下来,在.m中
创建一个新数组:
-(void) array{
squares = [[NSMutableArray alloc] arrayWithObjects: @"a", @"b", @"c", nil];
}
然后随机选择一个对象,如果满足“if”的属性,则将该对象从数组中移除,再做一次while循环。
-(void) start{
s=5;
while (s > 0){
//I even tried it without the next line..
randomN = nil;
//Also tried the next line without ' % [squares count'
randomN = [squares objectAtIndex:arc4random() % [squares count]];
if (randomN == @"a"){
[squares removeObject: @"a"];
[squares removeObject: @"b"];
s = s - 1;
}
if (randomN == @"b"){
[squares removeObject: @"b"];
[squares removeObject: @"c"];
s = s - 1;
}
if (randomN == @"c"){
[squares removeObject: @"c"];
s = s - 1;
}
else {
s=0;
}
}
}
当我运行应用程序时,应用程序停止并在循环开始后立即退出。
你能帮帮我吗?
【问题讨论】:
标签: iphone arrays object random sdk