'subscript(_:)' 的使用不明确
Ambiguous use of 'subscript(_:)'
func getCurrency()
{
let myLink:[String] = ["url1", "url2", "url3"]
for link in myLink{
let url = URL(string: link)
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil{
print("ERROR")
}
else{
if let content = data{
do{
if link == myLink[0]{
let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
if let ratesusd = myJson["INR_USD"] as? Double{
self.usdValue = ratesusd
}
}
else if link == myLink[1]{
let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
if let rateseuro = myJson["INR_EUR"] as? Double{
self.euroValue = rateseuro
}
}
else if link == myLink[2]{
let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
if let ratespound = myJson["INR_GBP"] as? Double{
self.poundValue = ratespound
}
}
}
catch{
}
}
}
}
task.resume()
}
}
经常显示此错误。我已将 if let content = data{
更改为 if let content = data["content"] as? Double{
但它显示另一个错误,即 "Value of optional type 'Data?' must be unwrapped to refer to member 'subscript' of wrapped base type 'Data'"。我在包括 Whosebug 在内的许多网站上看到了一些相关查询,但它们是 MacOS,但我在 WatchOS 上工作。任何人都请帮助!
一个 JSON 对象从未 未指定 AnyObject
。如果您希望字典将其转换为字典
let myJson = try JSONSerialization.jsonObject(with: content) as! [String:Any]
这修复了错误,因为编译器现在知道真实类型。
并且从不指定 .mutableContainers
。该选项在 Swift
中无效
func getCurrency()
{
let myLink:[String] = ["url1", "url2", "url3"]
for link in myLink{
let url = URL(string: link)
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
if error != nil{
print("ERROR")
}
else{
if let content = data{
do{
if link == myLink[0]{
let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
if let ratesusd = myJson["INR_USD"] as? Double{
self.usdValue = ratesusd
}
}
else if link == myLink[1]{
let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
if let rateseuro = myJson["INR_EUR"] as? Double{
self.euroValue = rateseuro
}
}
else if link == myLink[2]{
let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
if let ratespound = myJson["INR_GBP"] as? Double{
self.poundValue = ratespound
}
}
}
catch{
}
}
}
}
task.resume()
}
}
经常显示此错误。我已将 if let content = data{
更改为 if let content = data["content"] as? Double{
但它显示另一个错误,即 "Value of optional type 'Data?' must be unwrapped to refer to member 'subscript' of wrapped base type 'Data'"。我在包括 Whosebug 在内的许多网站上看到了一些相关查询,但它们是 MacOS,但我在 WatchOS 上工作。任何人都请帮助!
一个 JSON 对象从未 未指定 AnyObject
。如果您希望字典将其转换为字典
let myJson = try JSONSerialization.jsonObject(with: content) as! [String:Any]
这修复了错误,因为编译器现在知道真实类型。
并且从不指定 .mutableContainers
。该选项在 Swift