Objective-C将NSArray中的字符串对象与字符串进行比较,以确保相等

Objective-C将NSArray中的字符串对象与字符串进行比较,以确保相等,objective-c,xcode,Objective C,Xcode,如何正确地将从NSArray检索的字符串与文字字符串进行比较?到目前为止,我已经在一个方法中用三个空格填充了一个NSArray,现在我尝试用字符串“C10”替换NSArray中的10个随机索引,但我不想替换其中的内容,除非它仍然是“” 在这里,我创建了大小为100的数组,并用3个空格填充每个点 -(void) initBoard{ _board = [board initWithCapacity: 100]; for(int i =0; i < 100; i++){

如何正确地将从NSArray检索的字符串与文字字符串进行比较?到目前为止,我已经在一个方法中用三个空格填充了一个NSArray,现在我尝试用字符串“C10”替换NSArray中的10个随机索引,但我不想替换其中的内容,除非它仍然是“”

在这里,我创建了大小为100的数组,并用3个空格填充每个点

-(void) initBoard{
    _board = [board initWithCapacity: 100];
    for(int i =0; i < 100; i++){
        [_board addObject:@"   "];
    }
}
-(无效)初始化板{
_线路板=[线路板初始容量:100];
对于(int i=0;i<100;i++){
[_boardaddobject:@”“;
}
}
这是我在等式比较中遇到问题的方法

-(void)makeChutes: (int) numOfChutes {
    //Make argument number of Chutes randomly across the board.
    for(int i = 0; i < numOfChutes || i>(-100);){
        int random = arc4random_uniform(101);
        if ([[_board objectAtIndex:random] isEqual:@"   "]) {
            NSString *fString = [NSString stringWithFormat:@"C%d", 10];
            [_board replaceObjectAtIndex:random withObject:fString];
            i++;//Only increments i if 3 blank spaces were at random index..
        }
        else{//Used to attempt to stop my accidental infinite loop.
            i--;
            NSLog(@"I, loop, ran %i times.", i);//Attempt failed:-(
        }
    }
}
-(void)makeChutes:(int)numfochutes{
//在整个电路板上随机设置滑槽的数量。
对于(int i=0;i(-100);){
int random=arc4random_均匀(101);
如果([[U board ObjectatinIndex:random]isEqual:@”“])){
NSString*fString=[NSString stringWithFormat:@“C%d”,10];
[_BoardReplaceObjectAtIndex:random with Object:fString];
i++;//仅当3个空格在随机索引中时增加i。。
}
else{//用于尝试停止意外的无限循环。
我--;
NSLog(@“I,循环,运行了%I次。”,I);//尝试失败:-(
}
}
}
我知道上面的代码乱七八糟。为了停止循环,每次它不满足for条件时,我都使I递减,然后在for循环中添加一个OR条件,使用| |,在100个循环后尝试停止它。出于某种原因,| |条件即使在-100的南部也无法阻止它循环

我的第一个问题是如何将存储在数组中索引为“random”的字符串与由3个空格组成的文本字符串进行正确比较?我还尝试了方法
isEqualToString
,但效果相同


其次,也是不太重要的一点,既然我现在不需要它,我如何正确地为循环编写双条件代码?我的for循环语法不会从Xcode中得到任何错误或警告,它会编译并运行,所以我真的不明白为什么它会忽略我的第二个条件并不断迭代,即使
I
-100
使用这个字符串比较方法

   [[_board objectAtIndex:random] isEqualToString:@"  "]
修改了你的代码。我想这就是你要找的

-(void)makeChutes: (int) numOfChutes
{
    for(int i = 0; i < numOfChutes ;i++){
        int random = arc4random_uniform(101);
        if ([[_board objectAtIndex:random] isEqualToString:@"  "])
        {
            [_board replaceObjectAtIndex:random withObject:@"C10"];
            i++;
        }
    }
}
-(void)makeChutes:(int)numfochutes
{
对于(int i=0;i
编辑: 根据您在评论中所说的解决方案

-(void)makeChutes: (int) numOfChutes
{
    int i=0;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] '  '"];
    NSArray *filteredArray = [_board filteredArrayUsingPredicate:predicate];
    int arrayCount=[filteredArray count];

    do {
        int random = arc4random_uniform(101);
        if ([[_board objectAtIndex:random] isEqualToString:@"  "])
        {
            [_board replaceObjectAtIndex:random withObject:@"C10"];
            i++;
            if (arrayCount == i) {
                i=numOfChutes;
            }
        }

    } while (i<numOfChutes);
 }
-(void)makeChutes:(int)numfochutes
{
int i=0;
NSPredicate*谓词=[NSPredicate谓词WithFormat:@“自身包含[cd]”];
NSArray*filteredArray=[[使用谓词的线路板filteredArray:谓词];
int arrayCount=[filteredArray计数];
做{
int random=arc4random_均匀(101);
如果([[[U board objectAtIndex:random]isEqualToString:@”“]))
{
[_BoardReplaceObjectAtIndex:random with Object:@“C10”];
i++;
if(arrayCount==i){
i=努莫夫切特;
}
}

}而(i要知道它的存在与否以及索引,只需在
NSArray上使用
ContainsObject
属性即可

[array containsObject:@"text"];
int indexOfObject = [array indexOfObject:@"text"];
我的“initBoard”方法已损坏,导致“makeChutes”方法无法按预期工作。以下是正确的initBoard:

-(void) initBoard{
    _board = [[NSMutableArray alloc] initWithCapacity:100];
    for(int i =0; i < 100; i++){
        [_board addObject:@"   "];
    }
}
-(无效)初始化板{
_board=[[NSMutableArray alloc]initWithCapacity:100];
对于(int i=0;i<100;i++){
[_boardaddobject:@”“;
}
}
以及修正后的MakeCoots:

-(void)makeChutes: (int) numOfChutes{
    //Make argument number of Chutes randomly across the board.
    for(int i = 0; i < numOfChutes;){
        int random = arc4random_uniform(100);//+1 of max index
        if ([[_board objectAtIndex:random] isEqualTo:@"   "]){
            NSString *fString = [NSString stringWithFormat:@"C%d", 10];
            [_board replaceObjectAtIndex:random withObject:fString];
            i++;//Only increments i if 3 blank spaces were at random index.
        }
    }
}
-(void)makeChutes:(int)numfochutes{
//在整个电路板上随机设置滑槽的数量。
对于(int i=0;i
你想从这个方法中得到什么?哦,我正试图将字符串“C10”插入数组中随机位置的参数编号中。在我的程序中,参数编号是10,因此它将在数组中查找10个“空白”点并插入“C10”。但这将在每次迭代中递增i。我希望它迭代的次数与找到一个随机索引位置所需的次数相同,该位置是
,尚未被另一个字符串占用。因此,我只希望在它找到一个尚未使用的空间时递增i。因此,您希望删除所有@“到@“C10”在迭代中?我想找到
的随机位置的方法参数号,并用
“C10”
替换它们。因此,如果我去一个随机索引,它不是
,我不想弄乱它。在这个程序中,它像这样调用这个方法:
[cl makeChutes:10];
,因此我需要找到十个空格来替换为
“C10”
。这是家庭作业,忘了提一下。此外,从数组中检索字符串的事实在这方面没有任何改变。所有字符串的相等性测试方法都是相同的。好的,谢谢。我想如果字符串来自数组,我可能必须显式将其转换为字符串。您可以这样做,但这不会改变任何内容关于运行时对象的事情,它只是增加了编译器对它的了解(显然是在编译时)。值得注意的是,
arc4random\u uniform()
返回一个
u int32\u t
(实际上应该是
uint32\u t
)而不是
int