Get List of Values ​​for NSArray NSDictionary

I have the following NSArray:

NSArray myArray = @[@{@300:@"5 min"}, @{@900:@"15 min"}, @{@1800:@"30 min"}, @{@3600:@"1 hour"}]; 

I need a list of the meanings of my dictionaries:

 @[@"5 min",@"15 min",@"30 min",@"1 hour"] 

And a list of the keys of my dictionaries:

 @[@300, @900, @1800, @3600] 

What is the best way to do this? I was thinking about a predicate, but I don’t know how to use it?

+4
source share
4 answers

Without any code to show how you want to do this, it is difficult to make sure that you are after this, and there is a bit of confusion in the question.

First, a predicate is exactly what can be proven true or false. Therefore, predicates are used in logical expressions, including those that are used implicitly in database queries, such as Core Data.

This is not what you want if I read your question correctly. You want to reduce the complexity of your data model by removing some redundancy (hopefully) in the process. A kind of smoothing an array of dictionaries.

Fair enough.

I also see how confusion with predicates arose - they are most often created using Key-Value Coding. KVC is known to be a very powerful method that can accomplish what you need. It just has nothing to do with a logical expression.

Having clarified this, with KVC you can do what you want, and with minimal fuss. This happens as follows:

 NSArray *values = [myArray valueForKeyPath: @"@ unionOfArrays.@allValues "]; NSArray *keys = [myArray valueForKeyPath: @"@ unionOfArrays.@allKeys "]; 

A brief explanation may be in order:

We want results to be

  • All values ​​(or keys) of each dictionary, getting an array of arrays of values ​​(or keys)
  • Then we want to smooth these arrays into one array.

To get all values ​​(or keys) from the dictionary using KVC, the special key is @allValues or @allKeys , respectively.

The @unionOfArrays operator does the union of arrays obtained from the subsequent expression, i.e. aligns it in the desired array.

The price you pay for this coding simplicity is that you need to use KVC key paths with collection operators, which are just lines in your code. Therefore, you lose any help from the compiler with the syntax and do not check that the keys you entered exist in the objects. Similarly, the debugger and error messages are useless if you make a mistake or use the wrong operator, for example.

+8
source

You can use the dictionary property allValues ​​to get all the dictionary values.

Try this code in your case.

 NSArray *myArray = @[@{@300:@"5 min"}, @{@900:@"15 min"}, @{@1800:@"30 min"}, @{@3600:@"1 hour"}]; NSMutableArray *arr = [NSMutableArray array]; for (NSDictionary *dict in myArray) { [arr addObject:[[dict allValues] objectAtIndex:0]]; } NSLog(@"%@",arr); 

Note. Make sure that each dictionary has only one meaning. he will return

[

5 minutes,

15 minutes,

30 min,

1 hour

]

+1
source

@Johnyu's answers are technically correct, but I see no reason to include a secondary loop, especially if the data structure remains the same.

 NSArray *myArray = @[@{@300:@"5 min"}, @{@900:@"15 min"}, @{@1800:@"30 min"}, @{@3600:@"1 hour"}]; NSMutableArray *arrayOfValues = [NSMutableArray new]; NSMutableArray *arrayOfKeys = [NSMutableArray new]; for (NSDictionary *dictionary in myArray) { [arrayOfValues addObject:dictionary.allValues[0]]; [arrayOfKeys addObject:dictionary.allKeys[0]]; } NSLog(@"%@",arrayOfKeys); NSLog(@"%@",arrayOfValues); 
+1
source

Try the following:

 NSArray *myArray = @[@{@300:@"5 min"}, @{@900:@"15 min"}, @{@1800:@"30 min"}, @{@3600:@"1 hour"}]; NSMutableArray *keyArray = [[NSMutableArray alloc] init]; NSMutableArray *valueArray = [[NSMutableArray alloc] init]; for (NSDictionary *dictionary in myArray) { for (NSString *key in dictionary) { [keyArray addObject:key]; [valueArray addObject:[dictionary objectForKey:key]]; } } 
0
source

Source: https://habr.com/ru/post/1498938/


All Articles