【问题标题】:Copy C pointers to NSMutableDictionary将 C 指针复制到 NSMutableDictionary
【发布时间】:2013-01-08 20:35:16
【问题描述】:

我有一个在 iOS 中解析 XML 文件的类。

可以TBXMLElement*的形式获取元素的数据。

我想遍历 XML 并制作 TBXMLElements 的深层副本并将它们存储在 NSMutableDictionary 类变量中。

我该怎么做:

myClassDict addObject:(TBXMLElement*)element?

【问题讨论】:

  • 你必须创建一个封装类,它是 NSObject 的子类。

标签: ios xml tbxml


【解决方案1】:

您可以将指针放在 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];

【讨论】:

    【解决方案2】:

    就像在 cmets 中所说的那样,您必须将 TBXMLElement* 包装在 NSObject 的子类中。大概是这样的:

    @interface MyXMLElement {
    TBXMLElement* _xmlElement;
    }
    
    -(void)setXMElement:(TBXMLelement*)element;
    
    @end
    

    然后您可以填充元素:

    MyXMLElement *elmt = [[MyXMLElement alloc] init];
    
    [emlt setXMLElement:pointerToTBXMLElement];
    
    [someArray addObject:elmt];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-26
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      • 2021-07-13
      • 1970-01-01
      • 2010-09-27
      相关资源
      最近更新 更多