Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何从Dict中检索值_Swift_Swift4 - Fatal编程技术网

Swift 如何从Dict中检索值

Swift 如何从Dict中检索值,swift,swift4,Swift,Swift4,我从服务器获取JSON格式的数据,然后将数据格式转换为[String:Any] JSON--> { integer = 1; length = "<null>"; object = ( "692b663b-b7d5-43-287ddaadc2ff" ); string = "SANJEEV TREHAN"; } 将数据转换为[

我从服务器获取JSON格式的数据,然后将数据格式转换为[String:Any]

JSON--> {
          integer = 1;
          length = "<null>";
           object =     (
           "692b663b-b7d5-43-287ddaadc2ff"
            );
          string = "SANJEEV TREHAN";
        }
将数据转换为[字符串:任意]后:

json = `["length": <null>, "integer": 1, "string": SANJEEV TREHAN, "object": <__NSSingleObjectArrayI 0x2806acb10>(
692b663b-b7d5-43d5-daadc2ff) ]`
json=`[“长度”:“整数”:1,“字符串”:桑吉夫·特雷汉,“对象”:(
692b663b-b7d5-43d5-daadc2ff)]`
我想从
json
变量中检索对象键值

我只需要字符串形式的数据
692b663b-b7d54a-7dd-aadc2ff


我尝试了很多方法,但是没有得到我想要的数据格式。

既然您使用的是Swift,为什么不使用
Codable
类型呢?他们更容易使用,你不必到处做奇怪的铸造或测试

struct响应:可编码{
让长度:Int?
设整数:Int
让字符串:字符串
让对象:SomeObject
}
结构SomeObject:Codable{
让uuid:uuid
}
做{
让response=try JSONDecoder().decode(response.self,from:data)
}抓住{
打印(错误)
}
现在您可以直接请求字段

打印(response.object.uuid)

似乎您的对象键是一个字符串数组。下面是如何获得值的方法

if let yourObject = json["object"] as? [String] {
  if yourObject.count != 0 { 
    yourObjectValue = yourObject[0]
  }
}

你尝试过的“很多东西”是什么?你能在这里分享json吗?谢谢你的回复,告诉我一件事,在应用程序数据未清除之前,在IOS应用程序中存储
令牌
的技术更好。你可以使用用户默认值。如果答案有效,请将其标记为正确答案。:)如果您想以加密格式存储它以使其安全,可以使用KeychainSwift包装器。keychainSwift将永久存储数据。我想在卸载应用程序时从应用商店中删除数据。用户默认值是否适用于此??
if let yourObject = json["object"] as? [String] {
  if yourObject.count != 0 { 
    yourObjectValue = yourObject[0]
  }
}