简体   繁体   中英

Copying a dictionary into an array in Swift

I am attempting to create an array of dictionaries using [[String:AnyObject]]. I have used SwiftyJSON to convert JSON into a JSON object and am able to loop through each key and value. The code below will not create an individual business for each business, but an individual business for each key and value in the businesses. Currently the JSON has sections for id, name, latitude, and longitude and I want each of these values in the dictionary for each business in the array.

var businesses = [[String:AnyObject]]()

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

How can I adjust this code to create an individual business for each business, instead of an individual business in the array for each key and value.

Currently:

businesses[:] = ["id" = 1, "name" = "asdf",...,"id" = 2, "name" = "asdf2"] 

Instead of what I want which is

businesses[0] = ["id" = 1, "name" = "asdf"]
businesses[1] = ["id" = 2, "name" = "asdf2"]

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.

Run this in Playground.

//: Playground - noun: a place where people can play
import UIKit

// Simulating JSON
class sim {
    let s: String
    init(_ s:String) {self.s = s}
    var stringValue:String {return s}
}
let simulatingJSONThings = [
    ["id":sim("3"), "name":sim("Alice"), "lat":sim("123.45"), "lng":sim("567.89")],
    ["id":sim("4"), "name":sim("Bob"), "lat":sim("123.45"), "lng":sim("567.89")],
    ["id":sim("5"), "name":sim("Conny"), "lat":sim("123.45"), "lng":sim("567.89")],
]

// #1 Yours
do {
    var businesses = [[String:AnyObject]]()
    for business in simulatingJSONThings {
        for (key,value) in business {
            let value1 = value.stringValue
            businesses.append([key:value1])
        }
    }
    businesses
}

// #2 Answer
do {
    var businesses = [[String:AnyObject]]()
    for business in simulatingJSONThings {
        var b = [String:String]()
        for (key,value) in business {
            let value1 = value.stringValue
            b[key] = value1
        }
        businesses.append(b)
    }
    businesses
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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