简体   繁体   English

iphone sdk nsstring作为对象-获取文本

[英]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. 上面的代码会将serviceUserID字符串输出到日志窗口,但是当我尝试在该功能之外执行相同操作时,它会崩溃且没有错误消息。 I've no idea what's up. 我不知道怎么回事。 I have tried to set the string with initWithFormat and all sorts, but nothing's working!! 我试图用initWithFormat和所有类型设置字符串,但是没有任何效果! 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?? 我只需要设置serviceUserID变量,以便可以在其他函数中使用它...有没有办法设置我丢失的文本?

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. 您需要保留分配给iVar的字符串值-在您的示例中,您可能会分配自动发布的字符串,该字符串在当前范围之外变得无效。 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"; 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. 起作用的是您在这里分配字符串文字-它是在编译时创建的,根本不会被释放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM