1

I have multiple arrays and i wish to check if the string selcted by random from one array is in another array and if so show an image

I have globaly set plistarray1 and plistarray2 in the h file and in the m file i have the following

The last block of code is what im strugling with i cant get an image to display based on the random string generated from 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;


        }
1
  • 1
    For better performance use NSSet instead of NSArray. containsObject: or member: can be used to check a object is present or not. Commented Apr 3, 2014 at 12:50

2 Answers 2

1

Your code checks if the plistArray contains plistArray1 object, which it does not (initially, it contains every single object from plistArray1, but not the plistArray1 object itself).

Your code should be checking for the presence of the text, like this:

// 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;
}

This code can be made more efficient if you replace plistArray1 with a set, which would let you check containment in amortized constant, rather than linear, time.

EDIT : (in response to this comment: "The plistArray1 is being brought back as nil")

This is because you have declared plistArray1 as a local variable in your method, in addition to having an instance variable by the same name. Replace this line

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

with this

plistArray1 = plistDict1[@"truth"];

to fix the problem.

Sign up to request clarification or add additional context in comments.

4 Comments

I can see how this works and have implimented it but the if statment dose not get called ?
@user3246508 What do you mean "the if statement dose not get called" - the code does not reach the if statement, or the code reaches the if statement, but does not enter its body because its condition evaluates to NO?
The pslistarry1 is being brought back as nul hmm??
This must have somthing to do with the arrayByAddingObjectsFromArray call when i add the two arrays together.
1

Judging from what I think your code is saying you are checking to see if one plistArray is within the other plistArray. You just want to check if a string is contained in the array. You are close. Try this:

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

It's hard to follow what you're doing because of some of the variable names. Consider choosing more descriptive names so it's easier to pick up on what is going on.

1 Comment

The plistarray1 is being returned as null so this wont fire. This has confused me now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.