我试图检索并显示来自api的数据,但我一直都会遇到这个非常恼人的运行时错误而且我不知道发生了什么!
ViewController文件:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var valueLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//set corner radius of value label
var buttonLayer: CALayer = valueLabel.layer
buttonLayer.masksToBounds = true
buttonLayer.cornerRadius = 5.0
getCurrentCryptoData()
}
func getCurrentCryptoData() -> Void {
let apiURL = NSURL(string: "https://www.allcrypt.com/api?method=cmcmarketdata")
let sharedSession = NSURLSession.sharedSession()
let downloadTask: NSURLSessionDownloadTask = sharedSession.downloadTaskWithURL(apiURL, completionHandler: { (apiData: NSURL!, response: NSURLResponse!, error: NSError!) -> Void in
let dataObject = NSData(contentsOfURL: apiData)
let cryptoDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(dataObject, options: nil, error: nil) as NSDictionary
let omcvalues = Current(cryptoDictionary: cryptoDictionary)
self.valueLabel.text = "\(omcvalues.omcvalue)"
})
downloadTask.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
我的' current.swift'文件:
import Foundation
import UIKit
struct Current {
var omcvalue: Double
init(cryptoDictionary: NSDictionary) {
let all = cryptoDictionary["return"] as NSDictionary
let markets = all["markets"] as NSDictionary
let omcvalues = markets["OMC/BTC"] as NSDictionary
omcvalue = omcvalues["low_sell"] as Double
}
}
另外,这是我尝试使用的api源: api
我正试图赶往OMC'价值并存储' low_sell'属性,但我在运行时不断收到运行时错误。我想知道我是否正确访问字典,可以在这里使用一些帮助!
更新添加信息 我收到的错误是EXC_BREAKPOINT错误。 现在我知道我正在查找正确的目录,因为我更换了' low_sell'用'标签'一切正常,请求通过并打印。只有当我切换到low_sell时才会这样。
错误代码如下:
libswiftCore.dylib`swift_dynamicCastObjCClassUnconditional:
0x10af95980: pushq %rbp
0x10af95981: movq %rsp, %rbp
0x10af95984: pushq %rbx
0x10af95985: pushq %rax
0x10af95986: movq %rsi, %rcx
0x10af95989: movq %rdi, %rbx
0x10af9598c: xorl %eax, %eax
0x10af9598e: testq %rbx, %rbx
0x10af95991: je 0x10af959ac ; swift_dynamicCastObjCClassUnconditional + 44
0x10af95993: movq 0x7f236(%rip), %rsi ; "isKindOfClass:"
0x10af9599a: movq %rbx, %rdi
0x10af9599d: movq %rcx, %rdx
0x10af959a0: callq 0x10af9846a ; symbol stub for: objc_msgSend
0x10af959a5: testb %al, %al
0x10af959a7: movq %rbx, %rax
0x10af959aa: je 0x10af959b3 ; swift_dynamicCastObjCClassUnconditional + 51
0x10af959ac: addq $0x8, %rsp
0x10af959b0: popq %rbx
0x10af959b1: popq %rbp
0x10af959b2: retq
0x10af959b3: leaq 0xc158(%rip), %rax ; "Swift dynamic cast failed"
0x10af959ba: movq %rax, 0x87427(%rip) ; gCRAnnotations + 8
0x10af959c1: int3
0x10af959c2: nopw %cs:(%rax,%rax)
答案 0 :(得分:0)
在您发布的API中,没有"OMC/BTC"
密钥,只有"OMC\/BTC"
。这可以通过JSONSerialization类进行更正,但如果没有更多信息,您的问题实际上无法解决。
在API结果中,键" low_sell"显示如下:
"low_sell": "0.00008795",
因此,您必须首先转换为String
然后转换为数字。 NSString
:
if let lowSellString = omcvalues["low_sell"] as? NSString {
omcvalue = lowSellString.doubleValue
}
请注意,如果字典中的键存在且包含有效数据,则应始终使用上述let if
模式进行检查。 doubleValue
在这个意义上是安全的,因为如果它无法成功扫描字符串,它将返回0
。