Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用swift使用valueForKey获取整数_Ios_Json_Uitableview_Nsarray_Nsurlsession - Fatal编程技术网

Ios 使用swift使用valueForKey获取整数

Ios 使用swift使用valueForKey获取整数,ios,json,uitableview,nsarray,nsurlsession,Ios,Json,Uitableview,Nsarray,Nsurlsession,我开发了一个简单的应用程序,可以获取json数据并将值放入TableView中。当我使用valueForKey并获取字符串值时,一切正常 但是当我尝试获取一个像id这样的整数时……我在应用程序成功启动后得到了这个错误 输出显示为零 绿色错误 libswiftCore.dylib`swift_dynamicCastObjCClassUnconditional: 0x110c05980: pushq %rbp 0x110c05981: movq %rsp, %rbp 0x110c05984

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

但是当我尝试获取一个像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只能接受字符串,是否有办法将其转换为字符串?

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)
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()
}
更改:

var mydata = NSArray()
致:

并替换:

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

根据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)
    }
}

你犯了什么错误?编辑您的问题以包含实际的错误消息。对不起,您能否澄清我应该在何处将其添加到我的代码中…:/I获得,键入“Int”不符合协议“StringLiteralConvertible”我收到错误,NSNumber不是NSArray的子类型。您是否希望self.mydata成为id值数组:
[1,2,3,4,5,6]
?让我们来看看。
// 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)
    }
}