views:

106

answers:

1

I have followed directions in several books and suggestions on some forums but I have issues with my app crashing when I try and set user preferences. I have the following lines on my "done" method in my flipscreenViewController:


    - (IBAction)done 
   {
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setBool:musicOnOff.on forKey:kMusicPreference];
    [userDefaults setObject:trackSelection forKey:kTrackPreference];
    [self.delegate flipsideViewControllerDidFinish:self];   
   }

And the following methods in my mainViewController:


    -(void)initialDefaults
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setBool:YES forKey:kMusicPreference];
    [userDefaults setObject:@"Infinity" forKey:kTrackPreference];
}

-(void) setvaluesFromPreferences
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];   
    BOOL musicSelection = [userDefaults boolForKey:kMusicPreference];
    NSString *trackSelection = [userDefaults objectForKey:kTrackPreference];
    if(musicSelection == YES)
    {
        if([trackSelection isEqualToString:@"Infinity"])
        song = [[BGMusic alloc]initWithPath:
                       [[NSBundle mainBundle] pathForResource:
                       @"Infinity" ofType:@"m4a"]];
        else if([trackSelection isEqualToString:@"Energy"])
        song = [[BGMusic alloc]initWithPath:
                       [[NSBundle mainBundle] pathForResource:
                       @"Energy" ofType:@"m4a"]];       
        else if([trackSelection isEqualToString: @"Enforcer"])
        song = [[BGMusic alloc]initWithPath:
                       [[NSBundle mainBundle] pathForResource:
                       @"Enforcer" ofType:@"m4a"]];     
        else if([trackSelection isEqualToString: @"Continuum"])     
        song = [[BGMusic alloc]initWithPath:
                       [[NSBundle mainBundle] pathForResource:
                       @"Continuum" ofType:@"m4a"]];        
        else if([trackSelection isEqualToString: @"Pursuit"])
        song = [[BGMusic alloc]initWithPath:
                       [[NSBundle mainBundle] pathForResource:
                       @"Pursuit" ofType:@"m4a"]];

        [song setRepeat:YES];
        counter = 0;
        }
        else 
        [song close];
    }

If anyone out there could please help me see what I am doing wrong it would be much appreciated.

Chuck

+1  A: 

You should save your preferences whenever it makes sense to do so in the context of your application. If your code is crashing then you need to acquaint yourself with the debugger and find out why. If you want help with that, then you need to provide a stack crawl at the very least.

Azeem.Butt
+1 and I second that BIG TIME ... it is MUCH easier for you to simply use the debugger than for anyone else to find the needle in that haystack you provided with your question Greywolf210
Till
When using the debugger the only issue I get returned is "GDB: Program received signal: 'EXC_BAD_INSTRUCTION'" I have looked up this error on stackoverflow as well and it seems to happen in so many different situations that I am not sure where the connection lies. I am a noob with the SDK but I am trying to learn and I appreciate all your input. Is there somewhere else I should be looking? Something else I should be trying?
Greywolf210
The debugger is obviously stopping the execution - it should display the callstack for you (usually on the top-left side of XCode). Look into the top most displayed method that you wrote yourself while the debugger is still active.
Till
That's the thing, its not stopping until after the app loads, all the methods in the stack show green and the app actually starts in the simulator. I switch to the flipside view, then switch back to the mainview and it crashes right away and the debugger tells me in the very bottom left corner it is because of a "exc bad instruction" received.
Greywolf210
It doesn't sound like you're actually looking at a debugger window.
Azeem.Butt