Iphone PushScene后AppDelegate中保留的NSString丢失

Iphone PushScene后AppDelegate中保留的NSString丢失,iphone,objective-c,xcode,nsstring,cocos2d-iphone,Iphone,Objective C,Xcode,Nsstring,Cocos2d Iphone,在我的AppDelegate中,在init方法的末尾,我调用了一个[self-setup]方法。此方法从URL获取字符串,对其进行修剪,并将该字符串分配给名为_songDirectory的属性 下面是它在头文件中的外观: @property (retain) NSString *_songDirectory; 以下是在[setup]方法中分配的方式: //set a URL string NSString *urlString = [NSString stringWithFormat:@"ht

在我的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_访问:

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

我可以使用上面的语句从应用程序委托获取int,但不能获取字符串。我希望能有一些见解

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

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

您需要使用:

 self._songDirectory = [NSString stringWithString: [infoString substringWithRange: NSMakeRange(startIndex.location + startIndex.length, endIndex.location - startIndex.location - startIndex.length)]];
为了让财产运转起来。否则,您只需直接设置ivar,不会发生保留。

您需要使用:

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

为了让财产运转起来。否则,您只需直接设置ivar,就不会发生保留。

这就成功了!我想投你一票,但我没有足够的声誉。这就成功了!我想投你一票,但我没有足够的声誉。