viewWillDisappear中的NSString问题 [英] Problems with NSString inside viewWillDisappear
问题描述
我现在正在努力处理这段代码,非常感谢您的见解.所以这里是...
I am battling with this piece of code for days now, I really would appreciate your insight. So here it is...
fileName
在我的.h
文件中声明为
NSString *fileName;
,后来又是
@property (nonatomic, copy) NSString *fileName;
在viewWillDisappear
内部不可访问.当我调试时,它只是显示为nil对象或一些怪异的值.调试时,控制台显示以下消息:
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:
文件名的打印说明: FileNumber1
当我到达viewWillDisappear
时,我的控制台将显示以下内容:
When I reach to viewWillDisappear
my console shows this:
文件名的打印说明:
是的,对于fileName来说什么也没有..该值只是消失了..我只是不知道为什么!
Yeah, just nothing for fileName.. the value simply disappears.. I just don't know why!!!
更多详细信息:
在我的init
方法中,我执行以下操作,并且效果很好.
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];
在整个程序中,我都可以访问fileName
,但是在viewWillDisappear
内部,我尝试通过以下操作将其保存到standardUserDefaults
中:
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];
此时,可以访问someMutableArray
,但不能访问fileName
.任何见解,想法,想法将不胜感激.
At this point someMutableArray
is accessible, but fileName
is not. Any insight, thoughts, ideas, will be appreciated.
推荐答案
在您的init方法中,将其更改为self.fileName =
in your init method change it to read self.fileName =
您需要使用属性方法来创建字符串的副本,当前是因为您使用的是变量而不是属性,而是要分配自动释放的字符串.通过使用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.
这篇关于viewWillDisappear中的NSString问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!