简体   繁体   English

如何将NSDictionary值存储到NSArray?

[英]How to store NSDictionary value to an NSArray?

In a dictionary I have Key & values like below. 在字典中,我有如下键和值。 How can I get "title" values from the below and store it in a NSArray *data? 如何从下面获取“ title”值并将其存储在NSArray * data中?

{
    changed = 1414164684;
    city = Stockholm;
    class = "3. Mycket god klass";
    coordinates = "POINT(59.3246206 18.0686084)";
    id = 37510;
    title = "19 Glas Bar & Matsal";
    total = 70;`enter code here`
},
{
    changed = 1413991969;
    city = "G\U00f6teborg";
    class = "2. M\U00e4starklass";
    coordinates = "POINT(57.697944330446234 11.974067687988281)";
    id = 34865;
    title = "28+";
    total = 77;
},

The above mentioned snippet is an array, lets name it "result_Array". 上面提到的代码片段是一个数组,我们将其命名为“result_Array”。 In "result_Array" you are showing two objects. 在“result_Array”中,您将显示两个对象。 And each object is further a dictionary. 每个对象进一步是字典。 And from each Dictionary you want to fetch title and save it in your array named "data". 从每个词典中,您需要获取标题并将其保存在名为“data”的数组中。 Here we go 开始了

NSMutableArray *data=[NSMutableArray new];

for(int i=0; i<result_Array.count ; i++)
{
    NSDictionary *dict = [result_Array objectAtIndex:i];
    [data addObject:[dict objectForKey:@"title"]];
}

Hope it helps. 希望能帮助到你。 Feel free to ask any query 随意询问任何查询

If you have an array of NSDictionary, then you can get all values of a key using method "valueForKey" without any iteration: 如果你有一个NSDictionary数组,那么你可以使用方法“valueForKey”获得一个键的所有值,而不需要任何迭代:

NSArray *list = @[

@{@"changed":@"1414164684",
@"title":@"19 Glas Bar & Matsal"},

@{@"changed":@"1413991969",
@"title":@"28+"}];

NSArray *titles = [list valueForKey:"title"];

In the array titles you get only titles from list. 在数组标题中,您只能从列表中获取标题。

The example code you provided is actually an Array of Dictionaries. 您提供的示例代码实际上是一个字典数组。 So to answer your question with that small change 这么小的钱就能回答你的问题

NSArray *originalDict;
NSMutableArray *data = [NSMutableArray new];
for (NSUInteger i = 0; i < originalDict.count; i++) {
    NSDictionary *currentDictionaryPointer = [originalDict objectAtIndex:i];
    NSString *title = [currentDictionaryPointer objectForKey:@"title"];
    [data addObject:title];
 }

Where originalDict is the object that points to your provided sample code. 其中originalDict是指向您提供的示例代码的对象。

Thw following example will do the same with the least amount of code. 以下示例将使用最少量的代码执行相同操作。

NSMutableArray *arr = [NSMutableArray new];

for (NSDictionary* dict in myArray)
{
    [arr addObject:[dict valueForKey:@"title"]];
}

The arr object will have all values for the key 'title'. arr对象将具有键'title'的所有值。

Please do the following steps for your answer 请按照以下步骤获取答案

 1.First of all You should Create NSMutableArray
 2.After copy the dictionary to NSMutableArray
    NSMutableArray *array = [yourDictionary copy];
 3.After that set or create the for loop.
 4.Create NSString *strTitle in for loop.
 5.Finally strTitle = [NSString stringWithFormat:@"%@",[[array objectAtIndex:i]valueForKey:@"title"]]];

Easy steps 简单的步骤

 NSMutableArray *array = [yourDict copy];[If it is your dict is NSMutableDictionary]
     or
 NSMutableArray *array = [yourDict mutableCopy]; [If it is your dict is NSDictionary];

 for(int i=0;i<[array count];i++)
 {

   NSString *strTitle = [NSString stringWithFormat:@"%@",[[array objectAtIndex:i]valueForKey:@"title"]];
   NSLog(@"the title is==%@",strTitle);

 }

If you are getting "an array of stuff", I would not store it in a Dict I would store it to an NSArray and then iterate over it like: 如果你得到“一堆东西”,我不会将它存储在Dict中我会将它存储到NSArray然后迭代它,如:

var myObjects:[Object]

var myCollection = yourJSON as NSArray (assuming that you always get the same.

func myStuff (myCollection) {
    for item in myCollection {
    change = myCollection["changed"] as NSString (assuming a String here)
    city = myCollection["city"] as NSString
    ...
    ...

var Foo:Object = Object()
Foo.change = change
Foo.city = city
...
...


myObjects.append(Foo)
}

later on you can now easily iterate the myObjects Array to present stuff in a TableView 稍后您可以轻松地迭代myObjects数组以在TableView中显示内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM