How to Convert Data Dictionary into an Array Swift

How do I convert data dictionary into an array Swift?

About using map, I guess this page is helpful and visual.

let names = data.map { return $0.key }
print(names) // prints ["café-veritas", "cafe-myriade"]

How to convert dictionary to array

You can use a for loop to iterate through the dictionary key/value pairs to construct your array:

var myDict: [String : Int] = ["attack" : 1, "defend" : 5, "block" : 12]

var arr = [String]()

for (key, value) in myDict {
arr.append("\(key) \(value)")
}

Note: Dictionaries are unordered, so the order of your array might not be what you expect.


In Swift 2 and later, this also can be done with map:

let arr = myDict.map { "\($0) \($1)" }

This can also be written as:

let arr = myDict.map { "\($0.key) \($0.value)" }

which is clearer if not as short.

Can't convert dictionary to the array. Swift

Just delete the arrayLiteral: thing and it will work!

var months_keys = Array(array!.keys) //that does not work

The arrayLiteral initializer should be used like this:

var month_keys = Array(arrayLiteral: 1, 2, 3, 4) 
// will produce an array with items: 1, 2, 3 and 4

What you should call instead is the (_: SequenceType) initializer, since LazyMapCollection<[String : Int], String> conforms to that protocol.

A few more tips for your code:

  • If a variable's value is not going to change, declare it with let, instead of var.

  • You can simplify this:

-

if (customerViewModel.customer._dynamicMonthCount != nil) {
var array = customerViewModel.customer._dynamicMonthCount
var months_keys = Array(array!.keys)
}

to this:

if let dictionary = customerViewModel.customer._dynamicMonthCount {
var months_keys = Array(dictionay!.keys)
}

Copying a dictionary into an array in Swift

Basiclly, you just need to add the key, value pair for each busines to a dictionary and then add that dictionary to your array.

Something like this:

var businesses = [[String: AnyObject]]
for business in json["businesses"]!.array! {
var dic = [String: AnyObject]
for (key,value) in business {
let value1 = value.stringValue
dic [key] = value1
}
businesses.append(dic)
}

Well, I couldn't test the code 100% since you didn't provide your JSON data, but this is the general idea.

How to store dictionary values into an array from array of dictionaries in swift

Hope this can help you. swift codes

    var dic_categorys : NSDictionary! // original Dictionary
//
var array_item : NSArray! = dic_categorys.valueForKey("categorys") as NSArray
if let array = array_item {

var array_list : NSMutableArray! = NSMutableArray(array:array)
var array_list_category_name : NSMutableArray = NSMutableArray()
//
for item in array_list {
var dic_item : NSDictionary! = item as NSDictionary
if let dic = dic_item {
array_list_category_name.addObject(dic.valueForKey("category_name")!)
}
}

}

How to convert DictionaryString, Any.Values? into String-array SWIFT

You can unwrap data as well and then map the values from Any to String:

docRef.getDocument { document, error in
if let document = document, document.exists,
let data = document.data()?.values {
let values = data.values.compactMap{$0 as? String}
print(values)
} else {
print("Document does not exist")
}
}
}

Array from dictionary keys in swift

Swift 3 & Swift 4

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

componentArray = dict.allKeys // for NSDictionary

Convert an array of dictionaries into a set Swift 4

You don't want a Set of [[Double:Double]]. You want a Set of [Double:Double], because those are the objects in the array and you want them to be the objects in the Set.

Thus the right thing will happen if you simply say

let array1:[[Double:Double]] = [[4.5:3.678], [6.7:9.2867], [7.3: 8.7564]]
let set1 = Set(array1)

and so on.

This might require you to update to a newer version of Swift. It works in Swift 4.2.



Related Topics



Leave a reply



Submit