문제

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?

도움이 되었습니까?

해결책

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)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top