ツリーのような構造から NSMutableDictionary をその場で作成して入力しようとするのに苦労しています。
ノードがあるとしましょう
node.attributes
NSArray
キーと値のペアの を取得します
と
node.children
NSArray
同じノード タイプからノードの を取得します
そのツリーをネストされた にどのように変換できますNSMutableDictionary
か?
私のアプローチは、NSMutableDictionary
for each ノードを作成し、その属性と子を設定して、新しいNSMutableDictionary
per child を作成し、それを再度反復することです...再帰のように聞こえますね。
次のコードは、1 レベルの深さ (親と子) では機能しますが、孫以降では SIGABRT をスローします。
[self parseElement:doc.rootElement svgObject:&svgData];
どこ
-(void) parseElement:(GDataXMLElement*)parent svgObject:(NSMutableDictionary**)svgObject
{
NSLog(@"%@", parent.name);
for (GDataXMLNode* attribute in parent.attributes)
{
[*svgObject setObject:attribute.stringValue forKey:attribute.name];
NSLog(@" %@ %@", attribute.name, attribute.stringValue);
}
NSLog(@" children %d", parent.childCount);
for (GDataXMLElement *child in parent.children) {
NSLog(@"%@", child.name);
NSMutableDictionary* element = [[[NSMutableDictionary alloc] initWithCapacity:0] retain];
NSString* key = [child attributeForName:@"id"].stringValue;
[*svgObject setObject:element forKey:key];
[self parseElement:child svgObject:&element];
}
}
アップデート:
あなたの答えをありがとう、私はなんとかコードを動作させることができました
どうやら GDataXMLElement は、属性がない場合に attributeForName に応答しないため、私のコードはいくつかの例外をスローしました。
私はあなたのすべての(ベストプラクティスに関連する)提案も考慮しています
よろしく