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
How to Send Msmessage in Messages Extension
Manage a Uipickerview from an External Class - Using Swift
How to Keep the Header Cell Moving with the Tableview Cells in Swift 2.0
What Are the First Two Columns in Scnmatrix4
How to Monitor More Than 20 Regions
Swift Add Line Above to Control
Save Image with the Correct Orientation - Swift & Core Image
How to Handle Homophones in Speech Recognition
iOS Swift 2 Record Video Avcapturesession
Geolocation by Iphone's Ip Address
Auto-Sizing Uicollectionview Headers
How to Draw Geojson in Apple Maps as Overlay Using Swift 3
Shift Avplayer Captions When Subview Overlaps Them
Apple Push Notifications in Tvos
Save Depth Images from Truedepth Camera