2014-10-07 57 views
-1

我试图检索和一个API显示的数据,但我不断收到这真是烦人运行时错误,我不知道发生了什么!运行时错误而下载和显示JSON数据

视图控制器文件:

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'替换为'label'时,它一切正常,并且请求经过并被打印。只有当我切换到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) 
+3

这个运行时错误是...? – 2014-10-07 22:16:34

+0

您应该提供有关您有哪些错误以及发生哪一行的更多详细信息。 – Antonio 2014-10-07 22:17:06

回答

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

+0

我加了更多信息,对不起! – AlexCatch 2014-10-07 22:36:25

+0

添加了更多的答案,以回应你的加入。 – Mundi 2014-10-07 22:57:48