長文すみません。plist の文字列を 2 回目に使用すると、プログラムがクラッシュします。1 つのシーンと 1 つのオブジェクトを含む cocos2d プロジェクト ( attach ) があります。そして 1 つの .plist ファイル
HelloWorldLayer.h & .m (instantiate from CCLayer)
NodeObject.h & .m (instantiate from CCNode)
Test.plist
NodeObjectには、1 つのローカル文字列と 2 つのメソッドがあります
@interface NodeObject : CCNode
{
NSString *stringForPrint;
}
-(void)createObjWithString:(NSString *)string;
-(void)printString;
この両方のメソッドでは、パラメーター文字列で取得した文字列を出力します
-(void)createObjWithString:(NSString *)string
{
stringForPrint = string;
NSLog(@"NodeObject.createObjWithString stringForPrint >> %@", stringForPrint);
}
-(void)printString
{
NSLog(@"NodeObject.printString stringForPrint >> %@", stringForPrint);
}
Plis コンテンツは、項目タイプ文字列を含む 1 つのディクショナリを含む 1 つの配列です。
Root
-TestArray <Array>
--item 0 <Dictionary>
---type <String> == "This is string from plist"
テストのために、シーンにNodeObjectを作成し、plist からデータを取得します。そして、この文字列を印刷します。正しく動作しています。
if ([testDictionary objectForKey:@"TestArray"]) {
for (NSDictionary *tempItemData in [testDictionary objectForKey:@"TestArray"]) {
NSLog(@"type form plist in for loop > %@", [tempItemData objectForKey:@"type"]);//It's cool work. True string from plist.
}
}
NodeObjectをループに作成します。そしてまた仕事です。
if ([testDictionary objectForKey:@"TestArray"]) {
for (NSDictionary *tempItemData in [testDictionary objectForKey:@"TestArray"]) {
NodeObject *testObj = [NodeObject node];
//and send it string from plist
[testObj createObjWithString:[tempItemData objectForKey:@"type"]];
}
}
しかし!この文字列をprintStringメソッドフォームNodeObjectで使用しようとすると、ログなしでアプリがクラッシュします。[testObj printString]; // アプリをクラッシュさせる
繰り返します。手作業によるストリングワークによるオブジェクト作成。plist の文字列を使用するとクラッシュします。私は頭を壊しました。そして2番目の方法でのみ。createObjWithSrigで動作します。
//Work
NodeObject *testObj = [NodeObject node];
[testObj createObjWithString:@"manual string object"];
[testObj printString]; // Print correct string
//Crash
NodeObject *testObj = [NodeObject node];
[testObj createObjWithString:[tempItemData objectForKey:@"type"]];
[testObj printString]; //Crash here
プロジェクトファイルを添付します。これをテストできます