Reputation: 13
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
// 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**
}
Upvotes: 1
Views: 679
Reputation: 2794
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
Upvotes: 3