从数组中获取随机对象,然后将其从数组中删除。 iPhone

发布于 2024-10-19 05:33:59 字数 1101 浏览 2 评论 0原文

我遇到了数组问题,我想从数组中随机选取一个对象, 然后删除它并删除“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

微暖i 2024-10-26 05:33:59

有一些问题可能会困扰您:

您正在使用便捷构造函数初始化已分配的数组,您应该选择 alloc/init 对之一或便利构造函数本身:

[[NSMutableArray alloc] initWithObjects:...]

或者:

[NSMutableArray arrayWithObjects:...]

您的删除行正在尝试删除字符串文字。虽然您的数组包含与您尝试删除的字符串包含相同的字符串实例,但它们并不是完全相同的字符串实例。您需要使用[stringVar isEqualToString:otherStringVar]来比较值而不是它们的引用:

if ([randomN isEqualToString:@"a"])

而不是:

if (randomN == @"a")

此外,您的else语句将每次触发出于与第二个问题类似的原因。即使您使用了正确的字符串比较,如果您尝试仅执行这 4 个代码块之一,您的逻辑也可能会出错。为了实现这一点,第一个之后的每个 if 都需要一个 else,如下所示:

if (/* test 1 */) {
}
else if (/* test 2 */) {
}
else if (/* test 3 */) {
}
else {
    // chained else's make only one block able to execute
}

而不是:

if (/* test 1 */) {
}
if (/* test 2 */) {
}
if (/* test 3 */) {
}
else {
    // this else only applies to the LAST if!
}

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:

[[NSMutableArray alloc] initWithObjects:...]

or:

[NSMutableArray arrayWithObjects:...]

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:

if ([randomN isEqualToString:@"a"])

instead of:

if (randomN == @"a")

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 the ifs after the first needs an else, like so:

if (/* test 1 */) {
}
else if (/* test 2 */) {
}
else if (/* test 3 */) {
}
else {
    // chained else's make only one block able to execute
}

instead of:

if (/* test 1 */) {
}
if (/* test 2 */) {
}
if (/* test 3 */) {
}
else {
    // this else only applies to the LAST if!
}
愿与i 2024-10-26 05:33:59

我认为问题是您在创建数组时应该使用 initWithObjects (而不是 arrayWithObjects)。

在使用 alloc 创建新对象后,您应该始终使用 init* 方法。
arrayWith* 方法是“便捷构造函数”,它自动释放返回的对象。当您开始使用它时,该数组可能已被释放。

I think the problem is that you should be using initWithObjects when creating the array (rather than arrayWithObjects).

You should always use an init* method after using alloc to create a new object.
The arrayWith* methods are 'convenience constructors' that autorelease the returned object. By the time you come to using it, the array has probably been freed.

送你一个梦 2024-10-26 05:33:59

在 Objective-C 中,不能使用 == 比较字符串,必须使用 isEqual: 方法。 @"string" 表示法生成一个指向内存中其他位置定义的字符串的指针,这些指针可能不同,即使它们指向的数据相同。

所以,而不是

if (randomN == @"a"){

尝试

if ([randomN isEqual:@"a"]){

In objective-c, you can't compare strings using ==, you have to use the isEqual: 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

if (randomN == @"a"){

try

if ([randomN isEqual:@"a"]){
画离情绘悲伤 2024-10-26 05:33:59

正如亚历克斯所说,

squares = [[NSMutableArray alloc] arrayWithObjects: @"a", @"b", @"c", nil];

这一行应该是

squares = [[NSMutableArray alloc] initWithObjects: @"a", @"b", @"c", nil];

或者

squares = [[NSMutableArray arrayWithObjects: @"a", @"b", @"c", nil] retain];

此外,

randomN = [squares objectAtIndex:arc4random() % [squares count]];

如果方块为空,则此行上会发生 EXC_ARITHMETIC 异常(除以零)。

As Alex said,

squares = [[NSMutableArray alloc] arrayWithObjects: @"a", @"b", @"c", nil];

This line should be

squares = [[NSMutableArray alloc] initWithObjects: @"a", @"b", @"c", nil];

or

squares = [[NSMutableArray arrayWithObjects: @"a", @"b", @"c", nil] retain];

Also,

randomN = [squares objectAtIndex:arc4random() % [squares count]];

If squares is empty, EXC_ARITHMETIC exception (Division by Zero) occurs on this line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文