我正在开发一个显示实时比特币价格的应用程序。我正在使用2个API来做到这一点-一种纯文本和一种JSON。JSON API有点麻烦。
这是我的Swift代码
func BTCFallback(){
var string2 = currencySelector.currentTitle
var url = NSURL(string:"https://bitpay.com/api/rates/" + (string2)!)
var request = NSURLRequest(URL: url)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:fallback)
var data = NSData(contentsOfURL:url);
let value = NSString(string: USD.text).doubleValue / NSString(data:data, encoding:NSUTF8StringEncoding).doubleValue
// Define JSON string
var JSONString = "\(data)"
// Get NSData using string
if let JSONData = JSONString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) {
// Parse JSONData into JSON object
var parsingError: NSError?
if let JSONObject = NSJSONSerialization.JSONObjectWithData(JSONData, options: nil, error: &parsingError) as? [String: AnyObject] {
// If the parsing was successful grab the rate object
var rateObject: AnyObject? = JSONObject["rate"]
// Make sure the rate object is the expected type
if let rate = rateObject as? Float {
println("rate is \(rate)")
BTC.text = "\(rate)"
}
} else {
// There was an error parsing the JSON data
println("Error parsing JSON: \(parsingError)")
BTC.text = "err1"
}
}
}
在上面的代码中,currencySelector.currentTitle等于ISO货币代码,例如USD。BTC.text是一个UI元素。
预期的行为是该代码会将“ rate”的对应项设置为BTC.text的文本。如果有帮助,API将返回类似{“ code”:“ USD”,“ name”:“ US $”,“ rate”:376.71}之类的信息。我希望使用上面的示例将BTC.text设置为376.71
这是正在发生的事情:领事者给出了错误 Error parsing JSON: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x16eb0f60 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.})
我究竟做错了什么?提前致谢!
这完全是处理返回的数据和反序列化的问题。
这是示例代码,请注意,对Optionals的处理应该更好,这只是为了演示基本代码。出于示例目的,我使用了一个简单的同步Web调用。
var url: NSURL! = NSURL(string:"https://bitpay.com/api/rates/AUD")
var request = NSURLRequest(URL: url)
var response: NSURLResponse?
var error: NSError?
var data: NSData? = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&error)
println("data: \(data)")
if let data: NSData = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&error) {
println("data: \(data)")
var parsingError: NSError?
if let rateDictionary = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &parsingError) as NSDictionary? {
println("rateDictionary: \(rateDictionary)")
// If the parsing was successful grab the rate object
if var rateString: AnyObject = rateDictionary["rate"] {
println("rateString: \(rateString)")
// Make sure the rate object is the expected type
if let rate = rateString.floatValue {
println("rate is \(rate)")
}
}
}
}
Ouput:
数据:可选(7b22636f 6465223a 22415544 222c226e 616d6522 3a224175 73747261 6c69616e 20446f6c 6c617222 2c227261 7465223a 3430372e 39393137 7d) rateDictionary:{ 代码= AUD; 名称=“澳元”; 率=“ 407.9917”; } rateString:407.9917 利率是407.992
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句