Question

I need an array in a plist which holds some null values (i.e. not every selection will have audio), which I've created as follows:

<dict>
<key>WalkingAudio</key>
  <array>
     <string>NULL</string>
     <string>classical.mp3</string>
     <string>NULL</string>
  </array>

I'm getting the information from the selection made like this:

//awakeFromNib
NSString *myPlist = [[NSBundle mainBundle] pathForResource:@"MyPlist" ofType:@"plist"];
NSDictionary *rootDictionary = [[NSDictionary alloc] initWithContentsOfFile:myPlist];    
self.walkingAudio = [rootDictionary objectForKey:@"WalkingAudio"];

//didSlectItemAtIndex
walkingAudioChosen = [NSString stringWithFormat:@"%@", [self.walkingAudio objectAtIndex:self.carousel.currentItemIndex]];

What I need is for a button to be visible on the next view controller only when there is audio available and remain hidden if no audio is available. What makes the most sense to me is an if-else statement in the prepareForSegue method as follows:

if (walkingAudio == NULL) {
        dvc.playButton.hidden=YES;
    } else {
        dvc.playButton.hidden=NO;
    }

But it always shows the button and plays part of the audio and then crashes. What am I missing?

Was it helpful?

Solution

Your array already contains strings, so replace:

walkingAudioChosen = [NSString stringWithFormat:@"%@", [self.walkingAudio objectAtIndex:self.carousel.currentItemIndex]];

with

walkingAudioChosen = [self.walkingAudio objectAtIndex:self.carousel.currentItemIndex];

because the format doesn't do anything to help you.

Change the if statement to:

if ([walkingAudioChosen isEqualToString:@"NULL"]) {

because you want to compare the contents of 2 objects, not one pointer to NULL.

(Corrected a typo in the if as well, need to compare the string, not the array)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top