In my AppDelegate, near the end of the init method, I call a [self setup] method. This method gets a string from a URL, trims it, and assigns the string to a property called _songDirectory.
Here’s how it looks in the header file:
@property (retain) NSString *_songDirectory;
And here is how it is assigned in the [setup] method:
//set a URL string
NSString *urlString = [NSString stringWithFormat:@"http://www.blahblahblah.com/php/dev/blah.php?routine=blah"];
NSMutableString *infoString = [self getInfoStringFromURL: urlString];
//get the song directory as a string from the infostring
NSRange startIndex = [infoString rangeOfString:@"song_directory=="];
NSRange endIndex = [infoString rangeOfString:@"end_of_data=="];
_songDirectory = [NSString stringWithString: [infoString substringWithRange: NSMakeRange(startIndex.location + startIndex.length, endIndex.location - startIndex.location - startIndex.length)]];
NSLog(@"STRING IN APP DELEGATE: %@", _songDirectory);
NSLog prints the correct string when called in the app delegate. However, after I push a new scene I cannot access _songDirectory from it. The following code in the pushed scene yields EXC_BAD_ACCESS:
NSLog(@"STRING IN PUSHED SCENE: %@", [[[UIApplication sharedApplication] delegate] _songDirectory]);
I can use the above statement to get ints from the app delegate but not strings. I would appreciate some insight!
You’re assigning the string directly to the instance variable and not to the property, therefore the string is not retained. It should be
self._songDirectory = ...instead of_songDirectory(and you should probably call the propertysongDirectory, the leading underscore is usually only used for private instance variables).