I am having some problems with NSString retaining. My problem is that on the second function (runItem) it doesn’t seem to picking up on the value of the item1. No matter what I set it to, it seems that it is just set to nil. I am programming for Cocoa (desktop, versus iPhone), and I haven’t had this type of problem with NSString before. I’m not sure what I’m doing to cause it, so if anyone can help me on this I would really appreciate it! My code in my AppController.h file:
@interface AppController : NSObject {
NSString *item1;
}
@property (retain) NSString *item1;
- (IBAction)runItem:(id)sender;
@end
And AppController.m:
@synthesize item1;
- (void)awakeFromNib: {
NSDictionary *savedFile = [NSDictionary dictionaryWithContentsOfFile:@"Users/me/Desktop/Testing.plist"];
if (savedFile != nil) {
item1 = [savedFile objectForKey:@"Item Title"];
AppController *runFunction = [[AppController alloc] init];
[runFunction runItem:self];
}
else {
item1 = nil;
}
}
- (IBAction)runItem:(id)sender
NSLog(@"%@", item1);
}
Umm, although you set
item1in the firstAppControllerobject (the one that is created by the application because it was associated with a NIB file), you are creating a secondAppControllerobject (directly, through the default “init” constructor), whoseitem1was never set. And then you ask for that second object’sitem1, so of course it isnil.Perhaps this is because you think that
awakeFromNibis called whenever an object is initialized? But this is completely not true.awakeFromNibis only called for objects that are created when a NIB file is loaded.