将C指针复制到NSMutableDictionary
问题描述:
我有一个类可以解析iOS中的XML文件。将C指针复制到NSMutableDictionary
您可以以TBXMLElement*
的形式获取元素的数据。
我想遍历XML并制作TBXMLElements
的深层副本,并将它们存储在NSMutableDictionary
类变量中。
我怎能:
myClassDict addObject:(TBXMLElement*)element?
答
你可以把指针在NSValue。你打算用什么键?
// Save the TBXMLElement*
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setValue:[NSValue valueWithPointer:element] forKey:@"whatKey"];
…
// Get the TBXMLElement*
TBXMLElement *el = (TBXMLElement *)[[dict valueForKey:@"whatKey"] pointerValue];
答
像说的意见,你将不得不在NSObject
子类来包装TBXMLElement*
。大概是这样的:
@interface MyXMLElement {
TBXMLElement* _xmlElement;
}
-(void)setXMElement:(TBXMLelement*)element;
@end
然后,您可以填充元素:
MyXMLElement *elmt = [[MyXMLElement alloc] init];
[emlt setXMLElement:pointerToTBXMLElement];
[someArray addObject:elmt];
您必须创建一个包装类,它是NSObject的一个子类。 –