views:

91

answers:

2

This is my first app. It doesn't crash the simulator just brings you back to the home screen. This isn't my first cocoa program (I'm not a pro at cocoa either)just my first iPhone.

It crashes when you press the letsPlayButton, which runs the -(IBAction)letsPlay:(id)sender; method.

Help me! Im so lost with what's wrong!

.H

#import <UIKit/UIKit.h>

@interface BrainiacViewController : UIViewController {
    IBOutlet    UILabel     *theQuestion;
    IBOutlet    UILabel     *theScore;
    IBOutlet    UILabel     *theLives;
    IBOutlet    UIButton    *answerOne;
    IBOutlet    UIButton    *answerTwo;
    IBOutlet    UIButton    *answerThree;
    IBOutlet    UIButton    *answerFour;
    IBOutlet    UIButton    *letsPlayButton;
    NSString  *questionNumber;
    NSInteger myScore;
    NSInteger myLives;
    NSInteger usersAnswer;
    NSArray   *theQuiz;

    //quiz dictionarys
    NSMutableDictionary *question, *rightAnswer, *wrongOne, *wrongTwo, *wrongThree;
}

-(IBAction)buttonOne:(id)sender;
-(IBAction)buttonTwo:(id)sender;
-(IBAction)buttonThree:(id)sender;
-(IBAction)buttonFour:(id)sender;
-(IBAction)letsPlay:(id)sender;

-(void)checkAnswer;

-(void)askQuestion;

-(void)updateScore;

-(void)loadQuiz;
@end

.M

#import "BrainiacViewController.h"

@implementation BrainiacViewController

-(void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
- (void)dealloc {
    [super dealloc];
}

-(IBAction)buttonOne:(id)sender{
    usersAnswer = 1;
}
-(IBAction)buttonTwo:(id)sender{
    usersAnswer = 2;
}
-(IBAction)buttonThree:(id)sender{
    usersAnswer = 3;
}
-(IBAction)buttonFour:(id)sender{
    usersAnswer = 4;
}
-(IBAction)letsPlay:(id)sender{
    //un hide answer buttons
    [answerOne setHidden:NO];
    [answerTwo setHidden:NO];
    [answerThree setHidden:NO];
    [answerFour setHidden:NO];

    //hide the lets play button
    [letsPlayButton setHidden:YES]; 

    //assign values to variables
    myScore = 0;
    myLives = 3;
    questionNumber = 0; 
    //ask question and start game
    [self askQuestion];
}


-(void)checkAnswer{
    [question objectForKey:questionNumber];
    [rightAnswer objectForKey:questionNumber];
    [wrongOne objectForKey:questionNumber];
    [wrongTwo objectForKey:questionNumber];
    [wrongThree objectForKey:questionNumber];

}

-(void)askQuestion{
    //create array to mix up the possible answers
    NSMutableArray *possibleAnswers = [[NSMutableArray alloc] init];
    [possibleAnswers addObject:[rightAnswer objectForKey:questionNumber] ];
    [possibleAnswers addObject:[wrongOne objectForKey:questionNumber] ];
    [possibleAnswers addObject:[wrongTwo objectForKey:questionNumber] ];
    [possibleAnswers addObject:[wrongThree objectForKey:questionNumber] ];


    //set labels/buttons for question
    theQuestion.text = [question objectForKey:questionNumber];


    //user picks a answer button and sets a value to usersAnswer
    questionNumber = questionNumber + 1;
    [self checkAnswer];

}

-(void)updateScore{
}

-(void)loadQuiz{ //assigns dictionary values
    //*KEY* defines question number
    [question    setObject:@"Which is NOT a starwars movie?" forKey:@"1"];
    [rightAnswer setObject:@"Attack of the Jedi" forKey:@"1"];
    [wrongOne    setObject:@"A New Hope" forKey:@"1"];
    [wrongTwo    setObject:@"The Phantom Menace" forKey:@"1"];
    [wrongThree  setObject:@"Attack of the Clones" forKey:@"1"];

    [question    setObject:@"In what state is the majority of Yellow Stone National Park in?" forKey:@"2"];
    [rightAnswer setObject:@"Wyoming" forKey:@"2"];
    [wrongOne    setObject:@"California" forKey:@"2"];
    [wrongTwo    setObject:@"Ohio" forKey:@"2"];
    [wrongThree  setObject:@"Nebraska" forKey:@"2"];

    [question    setObject:@"In the US, what household pet is the most common?" forKey:@"2"];
    [rightAnswer setObject:@"Dogs" forKey:@"2"];
    [wrongOne    setObject:@"Cats" forKey:@"2"];
    [wrongTwo    setObject:@"Hamsters" forKey:@"2"];
    [wrongThree  setObject:@"Komodo Dragons" forKey:@"2"];

    [question    setObject:@"A plane is traveling 2675 miles every 5 hours. How fast is the plane going in Miles per Hour?" forKey:@"3"];
    [rightAnswer setObject:@"535 Mph" forKey:@"3"];
    [wrongOne    setObject:@"325 Mph" forKey:@"3"];
    [wrongTwo    setObject:@"535 Mph" forKey:@"3"];
    [wrongThree  setObject:@"420 Mph" forKey:@"3"];
}
@end

appdelagate.h

#import <UIKit/UIKit.h>

@class BrainiacViewController;

@interface BrainiacAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    BrainiacViewController *viewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet BrainiacViewController *viewController;

@end

appdelegate.m

#import "BrainiacAppDelegate.h"
#import "BrainiacViewController.h"

@implementation BrainiacAppDelegate

@synthesize window;
@synthesize viewController;


#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    // Add the view controller's view to the window and display.
    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
     */
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    /*
     Called as part of  transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
     */
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */
}


- (void)applicationWillTerminate:(UIApplication *)application {
    /*
     Called when the application is about to terminate.
     See also applicationDidEnterBackground:.
     */
}


#pragma mark -
#pragma mark Memory management

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
    /*
     Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
     */
}

- (void)dealloc {
    [viewController release];
    [window release];
    [super dealloc];
}


@end

I tried it again and checked the console and the error was this..I dont know what that means.

[Session started at 2010-07-11 12:24:40 -0400.]
2010-07-11 12:24:45.729 Brainiac[46764:207] -[NSCFString letsPlay:]: unrecognized selector sent to instance 0x5944f00
2010-07-11 12:24:45.733 Brainiac[46764:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString letsPlay:]: unrecognized selector sent to instance 0x5944f00'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x02395919 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x024e35de objc_exception_throw + 47
    2   CoreFoundation                      0x0239742b -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
    3   CoreFoundation                      0x02307116 ___forwarding___ + 966
    4   CoreFoundation                      0x02306cd2 _CF_forwarding_prep_0 + 50
    5   UIKit                               0x002b9e14 -[UIApplication sendAction:to:from:forEvent:] + 119
    6   UIKit                               0x003436c8 -[UIControl sendAction:to:forEvent:] + 67
    7   UIKit                               0x00345b4a -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527
    8   UIKit                               0x003446f7 -[UIControl touchesEnded:withEvent:] + 458
    9   UIKit                               0x002dd2ff -[UIWindow _sendTouchesForEvent:] + 567
    10  UIKit                               0x002bf1ec -[UIApplication sendEvent:] + 447
    11  UIKit                               0x002c3ac4 _UIApplicationHandleEvent + 7495
    12  GraphicsServices                    0x02bfbafa PurpleEventCallback + 1578
    13  CoreFoundation                      0x02376dc4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    14  CoreFoundation                      0x022d7737 __CFRunLoopDoSource1 + 215
    15  CoreFoundation                      0x022d49c3 __CFRunLoopRun + 979
    16  CoreFoundation                      0x022d4280 CFRunLoopRunSpecific + 208
    17  CoreFoundation                      0x022d41a1 CFRunLoopRunInMode + 97
    18  GraphicsServices                    0x02bfa2c8 GSEventRunModal + 217
    19  GraphicsServices                    0x02bfa38d GSEventRun + 115
    20  UIKit                               0x002c7b58 UIApplicationMain + 1160
    21  Brainiac                            0x00002790 main + 102
    22  Brainiac                            0x00002721 start + 53
)
terminate called after throwing an instance of 'NSException'
A: 

You need to 'box' the integer questionNumber in a NSNumber object, like this:

theQuestion.text = [question objectForKey:[NSNumber numberWithInt:questionNumber]];

Or, you can use the more appropriate NSArray indexer:

theQuestion.text = [question objectAtIndex:questionNumber];
Jason
but questionNumber is a string not an int....
I checked the console agian and saw there was an error. I edited my post and added the error in case that helps.
+3  A: 

I tried it again and checked the console and the error was this..I dont know what that means.

2010-07-11 12:24:45.729 Brainiac[46764:207] -[NSCFString letsPlay:]: unrecognized selector sent to instance 0x5944f00

You sent a message (letsPlay:) to an object that does not respond to that message (an NSString, shown here as NSCFString).

This sort of thing usually happens because you didn't properly hold on to the object that you meant to send letsPlay: to, so it got deallocated. An NSString later got allocated at the same address, so everything that had been holding the pointer to the old object (and thinks it still is) is now holding the pointer to the NSString. Same pointer, different object. Then, when one of those would-be owners tries to send the message to the object it thinks it still has… boom.

The fix is to check your ownerships and make sure you're retaining (or creating using alloc and not autoreleasing) what you mean to own.

On a related note:

NSMutableArray *possibleAnswers = [[NSMutableArray alloc] init];
[possibleAnswers addObject:[rightAnswer objectForKey:questionNumber] ];
[possibleAnswers addObject:[wrongOne objectForKey:questionNumber] ];
[possibleAnswers addObject:[wrongTwo objectForKey:questionNumber] ];
[possibleAnswers addObject:[wrongThree objectForKey:questionNumber] ];

If you mean for this to be a local variable, you must release it by the end of the method, or you will leak this array.

If you mean to own this object, you should move it to an instance variable, and drop the local variable, and continue creating the object as you are doing now.

Required reading: The Memory Management Programming Guide for Cocoa.

Peter Hosey
OK, I think i get the exception. I lost my allocated memory of the letsPLay method so it couldn't run and then it got reallocated when it wasent meant to? How do I fix this???Yeah, I'm aware that I needed to release my arrays memory. I'm still coding it I knew I needed to release it but got distracted by this error!Thanks for the reading link, I'll check it out. First I want to fix this exception
How do I check my ownerships and not auto releasing? The letsplay method doesnt have any of that..Sorry I'n new to cocoa..
There is no “allocated memory of the letsPlay method”. The thing that got deallocated was the object that you tried to send the `letsPlay:` message to. Autorelease isn't evil; you just need to make sure you release what you own (whether by `release` or by `autorelease`) when you want to no longer own it, and no earlier.
Peter Hosey
And don't skip the documentation. It will provide you with the understanding you need to hunt down this problem, both now and in the future.
Peter Hosey
Im confused where it lost the memory. Because all I see is the method hooked up to a button in interface builder..I did skim through that memory documentation. well worth it.
You are wasting your time by not reading that documentation first. It is quite explicit in exactly how memory should be managed.
bbum
I just read it and tried `[self retain]` in my letsPlay method. It did nothing. So I dont know what to try.
shorty876: As I said in my answer, the `letsPlay:` message went to an NSString object, because your view controller had already died by that point. Changing how your view controller would respond to the message if it were still alive to receive it will not fix anything. You need to keep the view controller alive as long as it needs to be.
Peter Hosey
my 14 year old brain just exploded. How do I keep the view controller alive?
As long as something owns it, it will stay alive. It dies because nothing owns it. So, you need something to own this view controller.
Peter Hosey
You may also want to look at this document: http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhone101/Articles/03_AddingViewController.html
Peter Hosey
can I create a view controller object in main.m? will that work?
You already have a view controller object; otherwise, how did its view get on the screen? The problem is either that it does not have anything owning it, or that one of its owners is releasing that ownership before it should. Trying to go in any direction that doesn't fix that problem will not get you anywhere. Again, read the Memory Management Programming Guide—and read it all the way through. Don't do anything to your code until you reach the end of the document and understand it.
Peter Hosey
The app dealegate seems to deal with the viewcontroller but I dont see anything..can you help me a little more..
Try building with “Build and Analyze”.
Peter Hosey
I did it said succeeded and nothing happend. What am I looking for?
I'd hoped the analysis would have told you what you were missing. At this point, you should edit your question to include the complete source code of the class of the object that creates the view controller.
Peter Hosey
ok i added the app delegate in my op
Do you have the `viewController` outlet connected to your view controller in the nib? (I presume that's where your view controller is, since you aren't creating it in code.)
Peter Hosey
yeah I think it is
Well, go look. Right-click the app delegate and see whether its `viewController` outlet is connected and what to.
Peter Hosey
no its not. Its not connected to anything how do I connect it to the nib?
You can't connect it to the nib. That doesn't make sense, as you can't have a nib as the value of an outlet. What you want to connect the outlet to is the view controller, so do that. (See the IB manual and/or the Cocoa Fundamentals Guide if you don't know how.)
Peter Hosey
I did it. now what?
Connected the outlet to the view controller? Now it should work.
Peter Hosey
yup. I somehow got it too work..not really sure how. but thanks