Gurumoorthy Arumugam
Gurumoorthy Arumugam

Reputation: 2128

How can store the string in a variable on iPhone?

I pass my string value to one method but I can't get the stored variable value. Here is my code:

NSString username = @":ram";
NSString geder = @":gender";

[twoclass newDetails:username:gender]

My method is:

NSString *twoclassname;
NSString  *twoclassgender;

-(void)newDetails:(NSString *)name:(NSString *)gender{

//saved the string here;

 twoclassname = name;
 twoclassgender = gender;

//print name value here

NSLog(@"2classname: %@",twoclassname);
}

I get output like 2classname:ram, I will assign this to getter method like this:

-(NSString *)getTwoclassname{
    return twoclassname;
   }

... when I call this getter method.like this below:

[self details:getTwoclassname];

But, console output showing null. I can't seem to get string vale "ram".

What am I doing incorrectly?

Upvotes: 0

Views: 941

Answers (2)

Craig Mellon
Craig Mellon

Reputation: 5399

It might be that your strings arn't retained?

try

twoclassname = [name retain];
twoclassgender = [gender retain];

Upvotes: 0

jrturton
jrturton

Reputation: 119242

[self details:getTwoclassname];

This is calling a method on self, called details, with a parameter getTwoClassname.

It isn't clear where you're calling this code from or what self is in this case, but let's assume it is being called from the same class you set the details from in the first place.

The correct syntax would be:

NSString *myString = [twoClass getTwoClassname];

myString will then hold the returned value.

Upvotes: 1

Related Questions