Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-06-2010, 05:55 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 11
Default App crashes when calling a user defined function in a class

Hey everyone,

This one has been getting the best of me for some time now. I have a class that holds two instance variables looks a little like this

class .h
{
NSString *gameid;
NSString *gamename;
}
@property (nonatomic, retain) NSString *gameid;
@property (nonatomic, retain) NSString *gamename;

// I tried this as [variable gameid] would crash with no errors
- (NSString *)getGameID;

class .m

// This function works and will return the gameid
- (NSString *) description {
return gameid;
}

// If I run this the app crashes
- (NSString *) getGameID {
return gameid;
}


Let me know if you want more code or if you need more info to point me in the correct direction.
psycorpse is offline   Reply With Quote
Old 09-06-2010, 06:24 PM   #2 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 5
Default Don't add a "Get" prefix

To override the getter method for a property use just the name of the property without using a "get" prefix:

-(NSString *)gameID{
return gameID;
}

Hope it was that simple.
DrCronus is offline   Reply With Quote
Old 09-06-2010, 07:37 PM   #3 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 11
Default

Quote:
Originally Posted by DrCronus View Post
To override the getter method for a property use just the name of the property without using a "get" prefix:

-(NSString *)gameID{
return gameID;
}

Hope it was that simple.

I completely understand that. How is it though when I call the default gameID getter my app crashes.

This doesn't appear to happen with another on of my objects however I haven't been able to find the issue. they appear to look the same.

Mike
psycorpse is offline   Reply With Quote
Old 09-06-2010, 07:44 PM   #4 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,178
Default

Quote:
Originally Posted by psycorpse View Post
I completely understand that. How is it though when I call the default gameID getter my app crashes.

This doesn't appear to happen with another on of my objects however I haven't been able to find the issue. they appear to look the same.

Mike
My guess: your member/property name is "gameid" and you're sending the message "gameID" to try to get the member, which will cause a crash, as "gameid" != "gameID". If this is indeed what's happening, you should have gotten a "... may not respond to ..." compiler warning (which you should have addressed at the time).
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 09-06-2010, 09:25 PM   #5 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 5
Default

Kalimba is also correct. My bet is that your problem is a combination of using the prefix "get" and the not using a consistent label for the ivar and the property. (It's hard to pick up your own typos in this kind of error -- read carefully). I'm also assuming that you are using @synthesize in your .m file. If all you are doing is returning the value of the ivar, I'd let the synthesize command handle the getter and not specify the getter at all in your code. So, use either:

//Needs to claim the ivar with the same label -- gameid
@synthesize gameid;

or

//getter on the property gameid
-(NSString *)gameid{
return gameid; //This is the ivar gameid claimed by the property gameid
}

I think you will get a crash when calling a nonexistent getter/setter (either by synthesize or explicit definition). You get a warning when calling a function that hasn't been defined in the header. Best I can tell, these are treated differently by the compiler.

I've had times where deleting what is there and starting over solves the problem -- either I'm more careful the second time or some phantom bug resolves. I don't argue with the success when this works. Good luck!
DrCronus is offline   Reply With Quote
Old 09-07-2010, 12:22 AM   #6 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 11
Default

Quote:
Originally Posted by DrCronus View Post
Kalimba is also correct. My bet is that your problem is a combination of using the prefix "get" and the not using a consistent label for the ivar and the property. (It's hard to pick up your own typos in this kind of error -- read carefully). I'm also assuming that you are using @synthesize in your .m file. If all you are doing is returning the value of the ivar, I'd let the synthesize command handle the getter and not specify the getter at all in your code. So, use either:

//Needs to claim the ivar with the same label -- gameid
@synthesize gameid;

or

//getter on the property gameid
-(NSString *)gameid{
return gameid; //This is the ivar gameid claimed by the property gameid
}

I think you will get a crash when calling a nonexistent getter/setter (either by synthesize or explicit definition). You get a warning when calling a function that hasn't been defined in the header. Best I can tell, these are treated differently by the compiler.

I've had times where deleting what is there and starting over solves the problem -- either I'm more careful the second time or some phantom bug resolves. I don't argue with the success when this works. Good luck!

I had posted some pseudo code so its possible I had a typo. When trying to use @synthesize gameId I get nothing in the console and the simulator/device closes. The only thing that I am doing currently (and not fully understanding) is using the AppDelegate to hold a NSMutableArray that holds the games. I then call something like appDelegate = (ProgramNameAppDelegate *)[[UIApplication sharedApplication] delegate];

I then use that variable like [[appDelegate.games objectAtIndex:0] gameId]; I know that its possible that it thinks that its not type-casted however if I change gameId to description I get the output that I want.

I might delete the files and start over again with that class.

If anything else pops in your minds, please send them my way.

Thanks for your help. I will post any fixes found.

Mike
psycorpse is offline   Reply With Quote
Old 09-07-2010, 02:06 AM   #7 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 5
Default

When you want to pull info from the array in your app delegate, just message for the sharedApplication and not a second call to the delegate of that object. I would set a break point on the statement with the sharedApplication and step through until you get the crash. I wonder if your problem is that you are just not getting ahold of the object that you are then trying to get your property value from.

If it is a problem with the property, I would suggest that you use the dot syntax when working with properties. Besides being apparent in the code that you are working with properties, it keeps your mind in that space.

I'm not sure that you understand that gameid and gameID are different to the compiler (it is Case Sensitive). If you define the property as gameID and then call for it as gameid, you will get the kind of behavior that you mention.

Example: Let's go with gameID:

class.h
{
NSString *gameID;
}

@property (nonatomic, retain) NSString *gameID;

class.m

@synthesize gameID; //Don't do explicit methods unless you have a reason.

//If you have a reason, then do this
-(NSString *)gameID{
return gameID; //gameID and gameID match
}

//A sample function using the property you just set up
-(NSString *)idOfGame{

//retrieve the gameObject from your array

gameObject = ...;
return gameObject.gameID; //This should work

return gameObject.gameid; //This would crash

}

Set a breakpoint on the entry to the getter and step through to the crash. The first line you will see steps to the synthesize statement for gameID. The second it won't find an appropriate synthesize statement and it will crash.

Other than that, I'm out of ideas. Good luck.
DrCronus is offline   Reply With Quote
Old 09-07-2010, 09:22 AM   #8 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 11
Default

Quote:
Originally Posted by DrCronus View Post
When you want to pull info from the array in your app delegate, just message for the sharedApplication and not a second call to the delegate of that object. I would set a break point on the statement with the sharedApplication and step through until you get the crash. I wonder if your problem is that you are just not getting ahold of the object that you are then trying to get your property value from.

If it is a problem with the property, I would suggest that you use the dot syntax when working with properties. Besides being apparent in the code that you are working with properties, it keeps your mind in that space.

I'm not sure that you understand that gameid and gameID are different to the compiler (it is Case Sensitive). If you define the property as gameID and then call for it as gameid, you will get the kind of behavior that you mention.

Example: Let's go with gameID:

class.h
{
NSString *gameID;
}

@property (nonatomic, retain) NSString *gameID;

class.m

@synthesize gameID; //Don't do explicit methods unless you have a reason.

//If you have a reason, then do this
-(NSString *)gameID{
return gameID; //gameID and gameID match
}

//A sample function using the property you just set up
-(NSString *)idOfGame{

//retrieve the gameObject from your array

gameObject = ...;
return gameObject.gameID; //This should work

return gameObject.gameid; //This would crash

}

Set a breakpoint on the entry to the getter and step through to the crash. The first line you will see steps to the synthesize statement for gameID. The second it won't find an appropriate synthesize statement and it will crash.

Other than that, I'm out of ideas. Good luck.

Ok I found my issue (Well its working at least). In my classes initWithGameIdNSString *)_gameId GameNameNSString *)_gameName, i was assigning the values like this:
gameId = _gameId;
gameName = _gameName;

I then changed this to:
[self setGameId:_gameId];
[self setGameName:_gameName];

it appears to be working now.

Also you keep referring to the gameID vs gameid. I do know that to the compiler that they are different. I was trying not to use the synthesized getter as I thought it wasn't working.

I appreciate the help and ideas that you offered.

Thanks,
Mike
psycorpse is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 257
19 members and 238 guests
14DEV, @sandris, ADY, ArtieFufkin10, bookesp, ckgni, Dani77, DarkAn, HemiMG, iDifferent, IphoneSdk, jakerocheleau, JasonR, MACralik, NSeven, prchn4christ, Rudy
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,767
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:52 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0