iphone sdk nsstring as object - get text
I'm having an issue with a string which is being returned from a webservice. I am setting the string up in the header file, simple with
NSString *serviceUserID;
@property (nonatomic, retain) NSString *serviceUserID;
then I'm synthesizing it as normal. I can set it using
serviceUserID = @"4fffrdscfbg-44-06dfgf-dfgdfg-32eer456134";
but when it's set using the service return, it seems to save an object which I cannot access later on. So the code below doesn't work...
NSLog(@"result = %@", result);
serviceUserID = result;
The code above will output the serviceUserID string to the log window, but when I try to do the same outside of that function it crashes with no error messages. I've no idea what's up. I have tried to set th开发者_StackOverflow社区e string with initWithFormat and all sorts, but nothing's working!! I just need to set the serviceUserID Variable so I can use it in other functions... is there a way to set the text that I'm missing??
Thanks for any help.
You need to retain string value you assign to your iVar - in your example you're likely assigning autoreleased string which becomes invalid outside current scope. As you have declared property for it the correct way to do that will be:
self.serviceUserID = result;
The reason why serviceUserID = @"4fffrdscfbg-44-06dfgf-dfgdfg-32eer456134";
works is that you're assigning string literal here - it is created at compile-time and doesn't get released at all.
精彩评论