atpietr
atpietr

Reputation: 17

OOP In Objective-C

I am familiar with OOP as I have been coding in Java for a while now, but I am having (syntax?) trouble in Objective-C. I have been looking at other posts on here but nothing has helped so far.

I have a class named "Play_Name.m" that has a method that sets a players name at the touch of a button and another method that gets the name and returns a string, (NSString*)get_name. I also have another class named "Play_ChooseChar.m" which is supposed to display the name entered in by calling the get_name function.

get_name returns the correct name when I call it in "Play_Name" (it's owner), but when I call it in "Play_ChooseChar" it returns (null).

//Play_Name code below

#import "Play_Name.h"

@interface Play_Name ()

@end

@implementation Play_Name

@synthesize playerName;
@synthesize textName;


-(IBAction)set:(id)sender {

[self setPlayerName:(self.textName.text)];

if([self.textName.text length] <= 0) {
    playerName = @"Player";
    NSLog(@"YOUR NAME: %@", playerName);
}

NSLog(@"YOUR NAME: %@", playerName);

}

//...........

@end





//Play_ChooseChar code below

#import "Play_ChooseChar.h"
#import "Play_Name.h"

@interface Play_ChooseChar ()

@end

@implementation Play_ChooseChar

@synthesize display_name;
@synthesize playname;
@synthesize boy;
@synthesize girl;
@synthesize isGirl;
@synthesize isBoy;

bool isGirl = FALSE;
bool isBoy = FALSE;

-(void)theName {

Play_Name *pN = [[Play_Name alloc] init];
[pN setPlayerName: pN.playerName];
NSLog(@"NAME: %@", pN.playerName);

self.display_name.text = pN.playerName;
//display_name.text = @"test";

[pN release];

//............

@end

So when I run it and enter my name, the print statement from "Play_ChooseChar" returns 'NAME: (null)'

Upvotes: 0

Views: 238

Answers (4)

Tommy
Tommy

Reputation: 100652

Opening lecture: you seem to be throwing Objective-C conventions to the wind. Getters shouldn't refer to the act of getting — so you'd implement name, not get_name and almost the only verb you'll see is 'is', in the sense of isValid ala NSTimer. Objective-C also uses camel case, starting with a lower-case character so player_name should be playerName. Similarly your class should have a three-letter prefix (as Objective-C doesn't do namespaces) and also be camel case, e.g. ATPPlayName.

Lecture points aside, this:

Play_Name *play_name = [[Play_Name alloc] init];

creates a brand new instance of Play_Name. It's not the same as whatever instance you're using elsewhere. That instance doesn't have a name attached yet. So when you ask it for the name in the next line, it's nil.

Upvotes: 2

Sagi Mann
Sagi Mann

Reputation: 3620

putting design and code conventions aside: when you create a new Play_Name instance, its get_name will return nil, obviously, because nowhere in the code have you called set_name before calling get_name.

You should first do: [play_name set_name:@"john"];

And, assuming your set_name method is implemented correctly, [play_name get_name] should then return the correct value.

I second the others who recommend to use properties since it takes care of most memory mgmt nuances for you. You can read more about this here: Objective-C: Declared Properties

Upvotes: 0

aahsanali
aahsanali

Reputation: 3547

You should create properties, and xcode will automatically create getter & setter for you.

Have a look here:

Upvotes: 0

ColinE
ColinE

Reputation: 70160

You haven't really shown enough code to determine the problem. However, I would recommend using properties rather than methods which follow your own naming convention. For example:

@interface Player: NSObject
@property NSString* name;
@property NSString* character;
@end

The above defines a class Player with the properties name and character. Xcode will generate the appropriate accessor methods and instance variables that 'back' these properties. See the language reference for more details.

Upvotes: 0

Related Questions