Check是数组中的字符串

时间:2014-04-03 12:20:18

标签: ios objective-c

我有多个数组,我想检查一个数组中随机选择的字符串是否在另一个数组中,如果是,则显示图像

我在h文件中有globaly设置plistarray1和plistarray2,在m文件中我有以下

最后一段代码就是我无法获取基于plistarray生成的随机字符串显示的图像。

NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];






NSString *path = [[NSBundle mainBundle] pathForResource:
                  @"data" ofType:@"plist"];







if ([[defaults objectForKey:@"truthonoff"] isEqualToString:@"YES"] && [[defaults objectForKey:@"dareonoff"] isEqualToString:@"YES"]  ) {

        self.text.text =@"Are you ready for this?";
    [Animations fadeIn:self.text andAnimationDuration:1.0 andWait:YES];





        NSDictionary *plistDict1 = [[NSDictionary alloc] initWithContentsOfFile:path];
        NSArray * plistArray1 = plistDict1[@"truth"];



        NSDictionary *plistDict2 = [[NSDictionary alloc] initWithContentsOfFile:path];
        NSArray *plistArray2 = plistDict2[@"dare"];

        self.plistArray = [[plistArray1 arrayByAddingObjectsFromArray:plistArray2] mutableCopy];

    }

    else if ([[defaults objectForKey:@"truthonoff"] isEqualToString:@"YES"] ) {

        self.text.text =@"I hope you are feeling brave!";
        [Animations fadeIn:self.text andAnimationDuration:1.0 andWait:YES];




        NSDictionary *plistDict3 = [[NSDictionary alloc] initWithContentsOfFile:path];

        NSArray *plistArray3 = plistDict3[@"truth"] ;

        self.plistArray = [plistArray3 mutableCopy];


        NSLog(@"%@", plistArray);


    }

    else if ([[defaults objectForKey:@"dareonoff"] isEqualToString:@"YES"] ) {

        self.text.text =@"This could be interesting!";
        [Animations fadeIn:self.text andAnimationDuration:1.0 andWait:YES];



        NSDictionary *plistDict4 = [[NSDictionary alloc] initWithContentsOfFile:path];

        NSMutableArray *plistArray4 = plistDict4[@"dare"];

        self.plistArray = [plistArray4 mutableCopy];


        NSLog(@"%@", plistArray);


    }
    else {
        self.text.text =@"Please turn on Truth or Dare";
        [Animations fadeIn:self.text andAnimationDuration:1.0 andWait:YES];



    }



        ////display random quote from array

    int randV = arc4random() % self.plistArray.count;
        NSLog(@"%@", plistArray);


        self.text.text = self.plistArray[randV];
        [self.plistArray removeObjectAtIndex:randV];
        [Animations fadeIn:self.text andAnimationDuration:1.0 andWait:YES];

        //display truth or dare image
        if ([plistArray containsObject:plistArray1]) {
            // selectedString is from the truth array
            self.truthimage.hidden = NO;


        }

2 个答案:

答案 0 :(得分:1)

根据我认为你的代码说你正在检查一个plistArray是否在另一个plistArray中。您只想检查数组中是否包含字符串。你很亲密试试这个:

if ([plistArray1 containsObject:self.text.text]) {
            // selectedString is from the truth array
            self.truthimage.hidden = NO;
}

由于某些变量名称,很难跟踪您正在做的事情。考虑选择更具描述性的名称,以便更容易了解正在发生的事情。

答案 1 :(得分:1)

您的代码检查plistArray是否包含plistArray1对象,但它不包含{最初,它包含plistArray1中的每个对象,但不包含plistArray1对象本身)。

您的代码应检查是否存在文本,如下所示:

// Obtain the random quote that you plan to display
NSString* toDisplay = self.plistArray[randV];
// Display the quote
self.text.text = toDisplay;
// Remove that quote from the plistArray
[self.plistArray removeObjectAtIndex:randV];
[Animations fadeIn:self.text andAnimationDuration:1.0 andWait:YES];
// Now's the fix: check plistArray1 for the toDisplay object
if ([plistArray1 containsObject:toDisplay]) {
    // selectedString is from the truth array
    self.truthimage.hidden = NO;
}

如果用一个集合替换plistArray1,可以提高此代码的效率,这样可以让您以分摊的常量而非线性时间检查包含。

编辑(响应此评论:" plistArray1被带回nil")

这是因为除了具有相同名称的实例变量之外,您已在方法中声明plistArray1作为局部变量。替换此行

NSArray * plistArray1 = plistDict1[@"truth"];

用这个

plistArray1 = plistDict1[@"truth"];

解决问题。