使用swift使用valueForKey获取整数

时间:2014-10-10 23:52:03

标签: ios json uitableview nsarray nsurlsession

我开发了一个简单的应用程序,它可以获取json数据并将值放在TableView中。当我使用valueForKey并获取字符串值时,一切正常。

但是当我尝试像id一样获取Integer时......我在应用程序成功启动后收到此错误。

OUTPUT显示为

绿色错误

libswiftCore.dylib`swift_dynamicCastObjCClassUnconditional:
0x110c05980:  pushq  %rbp
0x110c05981:  movq   %rsp, %rbp
0x110c05984:  pushq  %rbx
0x110c05985:  pushq  %rax
0x110c05986:  movq   %rsi, %rcx
0x110c05989:  movq   %rdi, %rbx
0x110c0598c:  xorl   %eax, %eax
0x110c0598e:  testq  %rbx, %rbx
0x110c05991:  je     0x110c059ac               ; swift_dynamicCastObjCClassUnconditional + 44
0x110c05993:  movq   0x7f236(%rip), %rsi       ; "isKindOfClass:"
0x110c0599a:  movq   %rbx, %rdi
0x110c0599d:  movq   %rcx, %rdx
0x110c059a0:  callq  0x110c0846a               ; symbol stub for: objc_msgSend
0x110c059a5:  testb  %al, %al
0x110c059a7:  movq   %rbx, %rax
0x110c059aa:  je     0x110c059b3               ; swift_dynamicCastObjCClassUnconditional + 51
0x110c059ac:  addq   $0x8, %rsp
0x110c059b0:  popq   %rbx
0x110c059b1:  popq   %rbp
0x110c059b2:  retq   
0x110c059b3:  leaq   0xc158(%rip), %rax        ; "Swift dynamic cast failed"
0x110c059ba:  movq   %rax, 0x87427(%rip)       ; gCRAnnotations + 8
0x110c059c1:  int3   
0x110c059c2:  nopw   %cs:(%rax,%rax)

无论如何我可以使用valueForKey或其他方法获取id吗?或者有没有办法将它转换为String,因为valueForKey可能只接受字符串?

iOS新手请帮助。

JSON DATA

println(self.jsonData)

(
        {
        id = 1;
        name = "Tommy";
    },
        {
        id = 2;
        name = "Dad";
    },
        {
        id = 3;
        name = "Mom";
    },
        {
        id = 4;
        name = "Vic";
    },
        {
        id = 5;
        name = "Tom";
    },
        {
        id = 6;
        name = "Pam";
    }
)

查看控制器

var jsonData = NSArray()
var mydata = NSArray()

func fetchData() {

    var url = NSURL.URLWithString("http://0.0.0.0:3000/api/fetch.json")

    var task = NSURLSession.sharedSession().dataTaskWithURL(url) {
        (data, response, error) in

        var error: NSError?

       ### parse data into json
        self.jsonData =  NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSArray!

       ### grab the data for populating the tableview
       ### ID is an integer value, so how can i fetch it if I can't use valueForKey?
        self.mydata = self.jsonData.valueForKey("id") as NSArray!

        ###This works fine
        //self.mydata = self.jsonData.valueForKey("name") as NSArray!

        dispatch_async(dispatch_get_main_queue()) {
            // reload the tableview
            self.tableView.reloadData()
        }

    }

    task.resume()
}

1 个答案:

答案 0 :(得分:2)

变化:

var mydata = NSArray()

为:

var mydata:[Int] = []

并替换:

self.mydata = self.jsonData.valueForKey("id") as NSArray!

使用:

for dict in self.jsonData as [NSDictionary] {
    if let idnum = dict["id"] as? Int {
        println("id is an Int")
        self.mydata.append(idnum)
    } else if let idnum = dict["id"] as? String {
        println("id is actually a string")
        self.mydata.append(idnum.toInt() ?? 0)
    }
}

根据OP,这个打印的“id是一个Int”多次,所以现在我们知道id是Int我们可以清理它:

// Loop over the array of dictionaries
for dict in self.jsonData as [NSDictionary] {
    // For this entry, get the id number from the dictionary
    // and append it to the mydata array
    if let idnum = dict["id"] as? Int {
        self.mydata.append(idnum)
    }
}