Question

I have a class that parses through an XML file in iOS.

You can get the data of an element in the form of an TBXMLElement*.

I want to iterate through the XML and make deep copies of the TBXMLElements and store them in an NSMutableDictionary class variable.

How can I:

myClassDict addObject:(TBXMLElement*)element?

Was it helpful?

Solution

You can put the pointers in an NSValue. What key are you going to use?

// Save the TBXMLElement*
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setValue:[NSValue valueWithPointer:element] forKey:@"whatKey"];

…

// Get the TBXMLElement*
TBXMLElement *el = (TBXMLElement *)[[dict valueForKey:@"whatKey"] pointerValue];

OTHER TIPS

Like said in the comments, you will have to wrap TBXMLElement* in a subclass of NSObject. Probably something like:

@interface MyXMLElement {
TBXMLElement* _xmlElement;
}

-(void)setXMElement:(TBXMLelement*)element;

@end

You can then populate the element:

MyXMLElement *elmt = [[MyXMLElement alloc] init];

[emlt setXMLElement:pointerToTBXMLElement];

[someArray addObject:elmt];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top