Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Using if statements with defaults?

Hi! I have just watched Amit's tutorial on how to setup defaults and was wondering about the part when he outputs the values to the text. My question is: can I use a default in an if statement. I tried with this:

    -(IBAction) click4 {
        playbtn.hidden = 0;
        if ([defaults stringForKey:kMusic]) == YES {
            CFBundleRef mainBundle = CFBundleGetMainBundle();
        CFURLRef soundFileURLRef;
        soundFileURLRef =CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"click",                     CFSTR ("wav"), NULL);
        UInt32 soundID;
        AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
        AudioServicesPlaySystemSound(soundID); }
    }

But it didn't work. It said "Use of undeclaired identifier 'defaults'" and 'expected expression' I tried moving the code to below the declaration of 'defaults' yet that made no difference. I hope someone can reply!

Thanks -Raph

1 Answer

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

Firstly, where is defaults defined? Is it an instance variable declared elsewhere? If not, you need to declare it locally within the method click4.

-(IBAction) click4 {
    playbtn.hidden = 0;

    // declare defaults it is not declared elsewhere within your view controller   
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

   // The stringForKey method returns a String and not a Boolean value
   // However, if you just want to check if a value exists then it is implied you are checking for existence
   // If there is no value then it will return nil and statement will fail
   // You had "== YES" outside of the parenthesis which is an illegal statement   
    if ([defaults stringForKey:kMusic])  {
        CFBundleRef mainBundle = CFBundleGetMainBundle();
        CFURLRef soundFileURLRef;
        soundFileURLRef =CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"click",                     CFSTR ("wav"), NULL);
        UInt32 soundID;
        AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
        AudioServicesPlaySystemSound(soundID); }
}

Thanks alot also in the Change Notification tutorial view I have an error on the line:

[self setupDefaults];

When in coding view it seems fine but wen I run the app, it crashes on that line with a SIGABRT error. Any idea how i can fix this?

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

Not sure but if you turn on breakpoints on exception then it will halt execution on the line the problem occurs. See this screenshot on how to turn on exception breakpoints:

I DISCOVERED THE SORCE OF THE PROBLEM!

I spelt setupDefaults as setupDefults but somehow spelt it right in the reply. Thanks for all your help -Raph.