如何将NSDictionary对象添加到NSMutableArray?

时间:2014-10-08 16:01:54

标签: ios objective-c xcode nsmutablearray nsdictionary

我查看了以下资源: 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???

2 个答案:

答案 0 :(得分:5)

您可以使用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];

希望它可以帮到你!

答案 1 :(得分:1)

[self.curveList  addObject:@{@"path":_curPath, @"color":_curColor}];

Read Apple Class Reference