How to Get All Keys and Values into Separate String Arrays from Nsdictionary in Swift

How to get all keys and values into separate String arrays from NSDictionary in Swift?

You can use:

let keys = jsonResult.flatMap(){ $0.0 as? String }  
let values = jsonResult.flatMap(){ $0.1 }

Get all keys exists in NSDictionary and put into Array

As @AechoLiu writes a recursive approach is a nice way to go about it. Something like the below:

func nestedKeys(in dictionary: [String: Any]) -> [String] {
guard let dictionaryValues = dictionary.values.filter({ $0 is [String: Any] }) as? [[String: Any]] else {
return [String]() + dictionary.keys
}
let subKeys = dictionaryValues.reduce([String]()) { result, dictionary in
return result + nestedKeys(in: dictionary)
}
return dictionary.keys + subKeys
}

The above expects your dictionaries to be of the format [String: Any] obviously.

Array from dictionary keys in swift

Swift 3 & Swift 4

componentArray = Array(dict.keys) // for Dictionary

componentArray = dict.allKeys // for NSDictionary

Split NSArray into sub-arrays based on NSDictionary key values

If the order of your splited Arrays is not important, i have a solution for you:

NSArray *arrOriginal;
NSMutableDictionary *grouped = [[NSMutableDictionary alloc] initWithCapacity:arrOriginal.count];
for (NSDictionary *dict in arrOriginal) {
id key = [dict valueForKey:@"roomType"];

NSMutableArray *tmp = [grouped objectForKey:key];
if (tmp == nil) {
tmp = [[NSMutableArray alloc] init];
[grouped setObject:tmp forKey:key];
}
[tmp addObject:dict];
}
NSMutableArray *marrApartmentsByRoomType = [grouped allValues];

Retrieve specific Array from JSON in swift in the form of Array of Keys and Values?

let keys=jsonResult["11/08/22"]?.allKeys as? [String];                       
let values=jsonResult["11/08/22"]?.allValues as? [String];

It was as simple as this

How can I get all values for specific key from each NSDictionary in an NSArray?

There's a neat trick with key-value coding that does it:

NSArray *matches = [myArray valueForKey: @"Match"];

Here is an example in action:

NSArray *anArray = @[
@{@"aWord" : @"I"},
@{@"aWord" : @"have"},
@{@"aWord" : @"an"},
@{@"aWord" : @"array"},
@{@"aWord" : @"which"},
@{@"aWord" : @"contains"},
@{@"aWord" : @"dictionary"},
@{@"aWord" : @"objects."},
@{@"aWord" : @"Etc."},
];

NSArray *aWordArray = [anArray valueForKey:@"aWord"];

After this last line, aWordArray will contain the words in the same order as in the original array of dictionaries.

How can I get all keys from each NSDictionary in an NSArray without loop

I am still worried about one situation of having similar keys in multiple Dictionaries inside your Array. The main thing for any Dictionary is that each of its content must have different keys. So, I recommend you to go with a solution that can generate an Array of Keys from the Dictionary having different keys.

Input :

(
{
x = 1;
y = 2;
},
{
y = 3;
z = 4;
},
{
x = 6;
z = 5;
}
)

Sample Code :

NSArray *uniqueKeys = [arrayWithKeys valueForKeyPath:@"@distinctUnionOfArrays.@allKeys"];

Output :

(
y,
z,
x
)


Related Topics



Leave a reply



Submit