2014-10-10 95 views
0

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

但是,当我尝试获取整数像一个ID ....我得到这个错误后,应用程序启动成功。

输出显示为零

绿色错误

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?或者有没有办法将它转换为字符串,因为valueForKey可能只接受字符串?

iOS新手请帮忙。

JSON数据

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

你得到了什么错误?编辑您的问题以包含实际的错误消息。 – rdelmar 2014-10-11 00:09:29

回答

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是一个i​​nt” 多次,所以现在我们知道该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) 
    } 
} 
+0

对不起,您能否澄清我应该在哪里添加此代码...:/ – 2014-10-11 01:07:05

+0

另外,我得到的类型'Int'不符合协议'StringLiteralConvertible' – 2014-10-11 01:12:07

+0

我收到错误,NSNumber不是NSArray的子​​类型 – 2014-10-11 01:19:37