ios - 试图在 ViewController 中显示字典值
问题描述
我的字典值返回正常,但不知道如何在视图控制器中显示它们。如您所见,返回的键/值略有不同,因为每个位置的子键都在变化。为了更清楚起见,我将整个子程序放在这里。print(todo) 打印出来
> 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;
};
从这里开始编辑:
可以看出,它几乎可以工作了,但现在我知道一个主要问题实际上是访问字典元素,并找到元素的路径,因为第一个键总是不同的名称。IE;
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()
}
解决方案
推荐阅读
- python - 从末尾剪切一个子字符串,直到某个字符第一次出现
- excel - 带计数的计算字段(字段 1)/计数(字段 2)
- javascript - 未捕获的 TypeError:Ajax 调用中未定义数据
- assembly - 汇编什么是数据、.main 和 arr?
- flutter - Flutter:如何在页面转换时为背景颜色设置动画
- python - 如何在 python 3.86 OS Windows 10 (64-bit) 中安装 tensorflow
- html - 如何从我的 WordPress 主题中删除或修改此 H2 标签?
- shopify - Shopify - 忘记密码/重置密码不起作用
- javascript - 如何自动将工具提示文本内容包装到多行?
- python - 组织文件时忽略文件夹