【问题标题】:Getting a random object from an array, then remove it from array. iphone从数组中获取随机对象,然后将其从数组中删除。苹果手机
【发布时间】: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


    【解决方案1】:

    其中有一些问题可能会让您感到困惑:

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

    [[NSMutableArray alloc] initWithObjects:...]
    

    或:

    [NSMutableArray arrayWithObjects:...]
    

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

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

    代替:

    if (randomN == @"a")
    

    此外,您的else 语句将每次触发,原因与第二个问题类似。即使您使用正确的字符串比较,如果您尝试仅执行这 4 个代码块之一,您的逻辑也可能是错误的。为此,第一个之后的每个ifs 都需要一个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!
    }
    

    【讨论】:

    • 谢谢大家的回答,我做了你告诉我的每件事,现在它正在工作:) 非常感谢
    【解决方案2】:

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

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

    【讨论】:

    • 谢谢大家的回答,我做了你告诉我的每件事,现在它正在工作:) 非常感谢
    【解决方案3】:

    在objective-c中,你不能使用==比较字符串,你必须使用isEqual:方法。 @"string" 表示法产生一个指向内存中其他地方定义的字符串的指针,它们可以不同,即使它们指向的数据相同。

    所以,而不是

    if (randomN == @"a"){
    

    试试

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

    【讨论】:

    • 谢谢大家的回答,我做了你告诉我的每件事,现在它正在工作:) 非常感谢你
    【解决方案4】:

    正如亚历克斯所说,

    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]];
    

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

    【讨论】:

    • 谢谢大家的回答,我做了你告诉我的每件事,现在它正在工作:) 非常感谢
    猜你喜欢
    • 2016-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多