【问题标题】:How to add a NSDictionary object to NSMutableArray? [closed]如何将 NSDictionary 对象添加到 NSMutableArray? [关闭]
【发布时间】:2014-10-08 16:01:54
【问题描述】:

我查看了以下资源: NSDictionary to NSArray? https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html

我正在尝试将 NSDictionary 插入 NSMutableArray。

相关代码在我看来是这样的:

@property (strong,nonatomic) NSMutableArray * curveList;
@property (strong,nonatomic) UIBezierPath * curPath;
@property (strong, nonatomic) UIColor * curColor;
// some code to set curPath and curColor
@{@"path":_curPath, @"color":_curColor}
//how to insert this into self.curveList???

【问题讨论】:

  • 查看NSMutableArray 的文档。添加对象有一个非常明显的方法。
  • 您可以像添加任何其他对象一样添加 NSDictionary。
  • 用 setObject:forKey: 方法?
  • 我实际上指出我已经阅读了该资源......我发现它们不像最明显的那样有用。
  • 我想这也应该被否决...stackoverflow.com/questions/1760371/…我不确定有些人明白为什么这个网站是必要的。如果文档足够多,这个网站就不会像今天这样存在了。

标签: ios objective-c xcode nsmutablearray nsdictionary


【解决方案1】:

您可以使用 addObject 方法将任何对象添加到 NSMutableArray,如下所示:

NSDictionary *aDic= [NSDictionary dictionaryWithObjectsAndKeys:
                              @"Carlos", @"name",
                              @"Jimenez", @"lastName", nil];

//On initialization  

NSMutableArray *aArr = [NSMutableArray arrayWithObjects:aDic,nil];

//After initialization  

NSMutableArray *aArr2 = [NSMutableArray array]; [aArr2 addObject:aDic];

希望对你有帮助!

【讨论】:

  • 谢谢,我会看看这个并决定是否可以将其用作答案。
  • 如果您认为这很有用,如果您将其作为正确答案,我将不胜感激:)
  • 我选择了你的,因为它更完整地解释了使用 NSMutableArray,尽管@BoilingLime 有确切的答案,所以我投了赞成票。非常感谢理解每个人的人从某个地方开始。
【解决方案2】:
[self.curveList  addObject:@{@"path":_curPath, @"color":_curColor}];

Read Apple Class Reference

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-25
    相关资源
    最近更新 更多