从数组中获取随机对象,然后将其从数组中删除。 iPhone
我遇到了数组问题,我想从数组中随机选取一个对象, 然后删除它并删除“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;
}
}
}
当我运行该应用程序时,该应用程序会在循环开始后立即停止并退出。
你能帮我吗?
I am having a problem with arrays, I want an object to be picked randomly from the array,
and then remove it and remove other objects that are specified in the "if" statement.
What I did..
in the .h
NSMutableArray *squares;
int s;
NSString *randomN;
Next, in the .m
Create a new array:
-(void) array{
squares = [[NSMutableArray alloc] arrayWithObjects: @"a", @"b", @"c", nil];
}
and then choose a random object, if properties of the "if" met, remove the object from the array, do the while loop again.
-(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;
}
}
}
When I run the app, the app stops and quits as soon as the loop starts.
Can you please help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有一些问题可能会困扰您:
您正在使用便捷构造函数初始化已分配的数组,您应该选择
alloc
/init
对之一或便利构造函数本身:或者:
您的删除行正在尝试删除字符串文字。虽然您的数组包含与您尝试删除的字符串包含相同值的字符串实例,但它们并不是完全相同的字符串实例。您需要使用
[stringVar isEqualToString:otherStringVar]
来比较值而不是它们的引用:而不是:
此外,您的
else
语句将每次触发出于与第二个问题类似的原因。即使您使用了正确的字符串比较,如果您尝试仅执行这 4 个代码块之一,您的逻辑也可能会出错。为了实现这一点,第一个之后的每个if
都需要一个else
,如下所示:而不是:
Their are a few issues that are probably tripping you up:
You're initializing an already allocated array with a convenience constructor, you should pick one of the
alloc
/init
pair or the convenience constructor by itself:or:
Your remove lines are attempting to remove string literals. While your array contains string instances that contain the same values as the strings you're trying to remove, they're not the same exact instances of the string. You need to use
[stringVar isEqualToString:otherStringVar]
to compare the values instead of their references:instead of:
Also, your
else
statement will fire every time for similar reasons as the second problem. Even if you use the correct string comparisons, your logic is probably off if you're trying to only execute one of those 4 blocks of code. To accomplish that, each of theif
s after the first needs anelse
, like so:instead of:
我认为问题是您在创建数组时应该使用
initWithObjects
(而不是arrayWithObjects
)。在使用
alloc
创建新对象后,您应该始终使用init*
方法。arrayWith*
方法是“便捷构造函数”,它自动释放
返回的对象。当您开始使用它时,该数组可能已被释放。I think the problem is that you should be using
initWithObjects
when creating the array (rather thanarrayWithObjects
).You should always use an
init*
method after usingalloc
to create a new object.The
arrayWith*
methods are 'convenience constructors' thatautorelease
the returned object. By the time you come to using it, the array has probably been freed.在 Objective-C 中,不能使用
==
比较字符串,必须使用isEqual:
方法。@"string"
表示法生成一个指向内存中其他位置定义的字符串的指针,这些指针可能不同,即使它们指向的数据相同。所以,而不是
尝试
In objective-c, you can't compare strings using
==
, you have to use theisEqual:
method. The@"string"
notation produces a pointer to a string defined elsewhere in memory, and these can be different, even though the data they point to is the same.So, instead of
try
正如亚历克斯所说,
这一行应该是
或者
此外,
如果方块为空,则此行上会发生 EXC_ARITHMETIC 异常(除以零)。
As Alex said,
This line should be
or
Also,
If squares is empty, EXC_ARITHMETIC exception (Division by Zero) occurs on this line.