试图在ViewController中显示字典值

时间:2018-07-26 18:43:10

标签: ios swift dictionary swift4

我可以正常返回字典值,但无法弄清楚如何在视图控制器中显示它们。如您所见,由于每个地方的子键都不同,因此返回的键/值有些不同。 我将整个子例程放在此处以提高清晰度。打印(待办事项)打印出来

> Got data
The todo is: ["mining": {
    1532638123799234661 =     {
        command = mining;
        siteid = "Platform Mining";
        ts = 1532638118480;
        user1 = 73276;
        value = 1;
    };
    153264129781238193 =     {

如您所见,每个连续键都是一个不同的数字。因此,首先我要如何在每次输入不同的数字或键名时引用该键?

现在,下一位,forKey,value会简单地打印出整个字典。

for (forKey,value) in todo {
    print("\(forKey) : \(value)")
}

这是30件物品的退货。

Got data
mining : {
1532583713635640435 =     {
    command = mining;
    siteid = "Platform Mining";
    ts = 1532583706411;
    user1 = 12345;
    value = 1;
};
1532585218556932431 =     {
    command = mining;
    siteid = "Platform Mining";
    ts = 1532585212726;
    user1 = 12345;
    value = 1;
};

从此处编辑:

可以看出,它几乎可以正常工作,但是现在我知道一个主要问题实际上是访问字典元素,并找到这些元素的路径,因为第一个键始终是一个不同的名称。即;

1532638123799234661 =
153264129781238193 =

那么,每次密钥名称不同时,如何告诉它密钥路径?我认为这是我最大的问题。

> ["mining": {
    1532638123799234661 =     {
        command = mining;
        siteid = "Platform Mining";
        ts = 1532638118480;
        user1 = 73276;
        value = 1;
    };
    153264129781238193 =     {
        command = mining;
        siteid = "Platform Mining";
        ts = 1532641293697;
        user1 = 73276;
        value = 1;
    };

完整代码

    func makeGetCall() {
    // Set up the URL request
    //let todoEndpoint: String = "https://api.jsecoin.com/v1.7/balance/auth/0/"
    //let todoEndpoint: String = "https://api.jsecoin.com/v1.7/ledger/auth/"
    //let todoEndpoint: String = "https://api.jsecoin.com/v1.7/checkuserid/xxxxxxxx/auth/"
    let todoEndpoint: String = "https://api.jsecoin.com/v1.7/mining/auth/"

    let apiKey = "xxxxxxxxxxxxxxxxxx"


    guard let url = URL(string: todoEndpoint) else {
        print("Error: cannot create URL")
        return
    }
    var urlRequest = URLRequest(url: url)

    urlRequest.setValue(apiKey, forHTTPHeaderField: "Authorization")
    urlRequest.setValue("application/json", forHTTPHeaderField: "Content- Type")
    // set up the session
    let config = URLSessionConfiguration.default
    let session = URLSession(configuration: config)

    // make the request
    let task = session.dataTask(with: urlRequest) {
        (data, response, error) in
        // check for any errors
        guard error == nil else {
            print("error calling GET on /todos/1")
            print(error!)
            return
        }


        // make sure we got data
        guard let responseData = data else {
            print("Error: did not receive data")
            return
        }
        print("Got data")
        // parse the result as JSON, since that's what the API provides
        do {
            guard let todo = try JSONSerialization.jsonObject(with: responseData, options: [])
                as? [String: Any] else {
                    print("error trying to convert data to JSON")
                    return
            }
            // now we have the todo
            // let's just print it to prove we can access it
           // this prints out the entire dictionary. 30 entries.
           print("The todo is: " + todo.description)

            for (forKey,value) in todo
            {
                print("\(forKey) : \(value)")

            }
            // the todo object is a dictionary
            // so we just access the title using the "title" key
            // so check for a title and print it if we have one
           // let index0 = todo.index(forKey: "title")
            let index1 = todo.index(forKey: "command")
            let index2 = todo.index(forKey: "siteid")
            let index3 = todo.index(forKey: "ts")
            let index4 = todo.index(forKey: "user1")

            print("JSECoin Stored Key/Value: \(todo[index1!].key): '\(todo[index1!].value)'.")

            let btn_ts = (todo[index3!].value)
            print("btn_ts: \(btn_ts)")

            let mySiteID = (todo[index2!].value)
            print("mySiteID: \(mySiteID)")

            let myUser = (todo[index4!].value)
            print("myUser: \(myUser)")

             DispatchQueue.main.async {
                if let displayMining = todo[index3!].value as? String {

                   self.displayMining.text = String(displayMining)

                } else {
                    // Ooops
                    self.displayMining.text = "?? unknown ??"
                }
            }



        // the todo object is a dictionary
        // so we just access the title using the "title" key
        // so check for a title and print it if we have one.

        guard let todoTitle = todo["mining"] as? String
            else {
                //print("Could not get title from JSON")
                return
                }
        print("The title is: " + todoTitle)

    } catch  {
        print("error trying to convert data to JSON")
        return
    }

    }
    task.resume()
}

0 个答案:

没有答案