0

在我的 AppDelegate 中,靠近 init 方法的末尾,我调用了一个 [self setup] 方法。此方法从 URL 中获取字符串,对其进行修剪,并将字符串分配给名为 _songDirectory 的属性。

下面是它在头文件中的样子:

@property (retain) NSString *_songDirectory;

以下是它在 [setup] 方法中的分配方式:

//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 在应用程序委托中调用时打印正确的字符串。但是,在我推送新场景后,我无法从中访问 _songDirectory。推送场景中的以下代码产生 EXC_BAD_ACCESS:

NSLog(@"STRING IN PUSHED SCENE: %@", [[[UIApplication sharedApplication] delegate] _songDirectory]);

我可以使用上面的语句从应用程序委托中获取整数,而不是字符串。我会很感激一些见解!

4

2 回答 2

2

您将字符串直接分配给实例变量而不是属性,因此不会保留字符串。它应该self._songDirectory = ...代替_songDirectory(并且您可能应该调用 property songDirectory,前导下划线通常仅用于私有实例变量)。

于 2012-05-26T20:10:55.650 回答
1

你需要使用:

 self._songDirectory = [NSString stringWithString: [infoString substringWithRange: NSMakeRange(startIndex.location + startIndex.length, endIndex.location - startIndex.location - startIndex.length)]];

为了让物业工作。否则,您只是直接设置 ivar 并且不会发生保留。

于 2012-05-26T20:11:41.153 回答