i declared two NSString and NSURL variable in interface of my controller
// project file url
NSURL * url_projectURL;
// temp string just for examine
NSString * tempString;
in my app action named:
- (IBAction) btnNewProject:(id)sender
// after running save panel and clicked Save
//
// set project url
url_projectURL = [savePanel URL];
// set temp string
tempString = [url_projectURL absoluteString];
//
NSLog(@"tempString is: %@", tempString);
NSLog(@"ProjectURL is: %@", [url_projectURL absoluteString]);
result is:
tempString is: file://localhost/Users/kosartofiq/Documents/project.ksf
ProjectURL is: file://localhost/Users/kosartofiq/Documents/project.ksf
but in other action for example:
– (IBAction) btn_test:(NSButton *)sender
NSLog(@"tempString is: %@", tempString);
NSLog(@"ProjectURL is: %@", [url_projectURL absoluteString]);
result:
for string variable is:
tempString is: file://localhost/Users/kosartofiq/Documents/project.ksf
but for url variable is nil or show error in running of coe
My question is why url variable changes it’s value to nil and it lost it’s value when use it in other action, but string variable keep it’s value?
i want use this url to save changes to file and in my project i use it some time.
Assuming you’re not using ARC, the problem is that you’re assigning objects you don’t own to instance variables. If the objects have no owners, they can be deallocated at any time. You should claim ownership of the objects, preferably by using accessors instead of directly setting the variables themselves.