xcode 如何将 NSDictionary 对象添加到 NSMutableArray?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26261376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add a NSDictionary object to NSMutableArray?
提问by eckama
I have looked at the following resources: NSDictionary to NSArray?https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.htmlhttps://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html
我查看了以下资源: 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
What I am attempting to do is insert an NSDictionary into an NSMutableArray.
我试图做的是将 NSDictionary 插入到 NSMutableArray 中。
The relevant code seems to be this to me:
相关代码对我来说似乎是这样的:
@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???
回答by Carlos Jiménez
you can add any object to NSMutableArray using the addObject method like this:
您可以使用 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];
Hope it helps you!
希望对你有帮助!
回答by BoilingLime
[self.curveList addObject:@{@"path":_curPath, @"color":_curColor}];