0

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?

Tim Post
  • 33,371
  • 15
  • 110
  • 174
Gurumoorthy Arumugam
  • 2,129
  • 1
  • 25
  • 40

2 Answers2

1
[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.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • ya your right i tried already like this when i call that myString in other method its showing null? example -(void)execute{ NSLog(@"myString: %@",myString);} console out put is (myString: (null)) – Gurumoorthy Arumugam Dec 22 '11 at 08:28
  • how can i access this this string in hole class? – Gurumoorthy Arumugam Dec 22 '11 at 08:31
  • You need to be clearer about what you mean. At the moment I don't know where each of your code snippets is, or what they are supposed to be doing. – jrturton Dec 22 '11 at 09:02
  • actually i have to use that stored string value other method when ever i try to call getTwoClassname method it need to return the value of mystring.in hole class. – Gurumoorthy Arumugam Dec 22 '11 at 09:23
0

It might be that your strings arn't retained?

try

twoclassname = [name retain];
twoclassgender = [gender retain];
Craig Mellon
  • 5,399
  • 2
  • 20
  • 25