viewWillDisappear 中 NSString 的问题
我已经与这段代码作斗争好几天了,我真的很感激您的见解。所以这里是...
fileName
,它在我的 .h
文件中声明
NSString *fileName;
,后来
@property (nonatomic, copy) NSString *fileName;
在 viewWillDisappear
中无法访问。当我调试时,它只是显示为 nil 对象或一些奇怪的值。当我调试时,我的控制台显示此消息:
打印文件名的描述: FileNumber1
当我到达 viewWillDisappear
时,我的控制台显示:
正在打印 fileName 的描述:
是的,fileName 没有任何内容......该值就消失了。 。我只是不知道为什么!
更多细节:
在我的 init
方法中,我执行了以下操作,效果很好。
fileName = [[NSString alloc] initWithString:(NSString *)CFURLGetString(pdfURL)];
fileName = [[fileName lastPathComponent] stringByDeletingPathExtension];
fileName = [fileName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
在整个程序中,我可以很好地访问 fileName
,但在 viewWillDisappear
内,我尝试通过尝试以下操作来保存到我的 standardUserDefaults
:
NSUserDefaults *ud = [[NSUserDefaults standardUserDefaults] autorelease];
NSArray *tmp = [NSArray arrayWithArray:someMutableArray];
[ud setObject:tmp forKey:fileName]; // THE PROBLEM IS HERE WITH fileName AS AKEY
[ud synchronize];
[tmp release];
此时someMutableArray
可以访问,但 fileName
则不可访问。任何见解、想法、想法都将受到赞赏。
I am battling with this piece of code for days now, I really would appreciate your insight. So here it is...
fileName
which is declared in my .h
file as
NSString *fileName;
and later as
@property (nonatomic, copy) NSString *fileName;
is not accessible inside viewWillDisappear
. It just shows up as nil object, or some weird value when I debug. When I debug, my console show this msg:
Printing description of fileName:
FileNumber1
When I reach to viewWillDisappear
my console shows this:
Printing description of fileName:
Yeah, just nothing for fileName.. the value simply disappears.. I just don't know why!!!
MORE DETAILS:
Inside my init
method I do the following and it works fine.
fileName = [[NSString alloc] initWithString:(NSString *)CFURLGetString(pdfURL)];
fileName = [[fileName lastPathComponent] stringByDeletingPathExtension];
fileName = [fileName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Throughout the program I can access fileName
just fine, but inside viewWillDisappear
I am trying to save to my standardUserDefaults
by attempting the following:
NSUserDefaults *ud = [[NSUserDefaults standardUserDefaults] autorelease];
NSArray *tmp = [NSArray arrayWithArray:someMutableArray];
[ud setObject:tmp forKey:fileName]; // THE PROBLEM IS HERE WITH fileName AS AKEY
[ud synchronize];
[tmp release];
At this point someMutableArray
is accessible, but fileName
is not. Any insight, thoughts, ideas, will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的 init 方法中将其更改为 self.fileName =
您需要使用属性方法以便创建字符串的副本,目前因为您使用的是变量而不是正在分配自动释放字符串的属性。通过使用 self.fileName 字符串将被复制并且不会被自动释放。
in your init method change it to read self.fileName =
You need to use your property methods so that a copy of the string will be made, currently because you are using the variable rather than the property you are assigning an autoreleased string. By using self.fileName the string will be copied and will not be autoreleased.