[英]Swift 3 cannot subscript a value of type 'String' with an index of type 'String' using SwiftyJSON and Alamofire
This is the code, i was making a bitcoin price tracker app and im new to ios development so i am struggling with this error :( any help is appreciated. I am using Alamofire and SwiftyJSON to do the json work. They are imported using cocoapods. This is the json im using: https://apiv2.bitcoinaverage.com/indices/global/ticker/BTCUSD 这是代码,我正在制作一个比特币价格跟踪器应用程序,我是ios开发的新手,所以我正在努力解决这个错误:(任何帮助都很感激。我正在使用Alamofire和SwiftyJSON来做json工作。它们是使用cocoapods导入的这是json我使用的: https : //apiv2.bitcoinaverage.com/indices/global/ticker/BTCUSD
// Connect the UI
@IBOutlet weak var PriceLabel: UILabel!
@IBOutlet weak var PercentageLabel: UITextView!
@IBOutlet weak var CurrencyPicker: UIPickerView!
var url : String = "https://apiv2.bitcoinaverage.com/indices/global/ticker/BTCUSD"
var Price : String = ""
var jsondata = ""
// Called before app is shown to user
override func viewDidLoad() {
super.viewDidLoad()
// Request the JSON Data
requestJson()
}
func requestJson(){
Alamofire.request(url).response { response in
print("Request: \(response.request)")
print("Response: \(response.response)")
if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
print("Data: \(utf8Text)")
self.parseJSON(json: utf8Text)
}
}
}
func parseJSON(json: String){
Price = jsondata["ask"].stringvalue **This is where it kicks an error**
}
func requestJson(){
Alamofire.request(url).response { response in
if let data = response.data {
self.parseJSON(json: JSON(data: data))
}
}
}
func parseJSON(json: JSON){
Price = json["ask"].stringValue
}
for get json from URL you can also use responseJSON 要从URL获取json,您还可以使用responseJSON
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.