gpt4 book ai didi

ios - 使用 valueForKey 使用 swift 获取整数

转载 作者:行者123 更新时间:2023-11-28 21:56:42 24 4
gpt4 key购买 nike

我开发了一个简单的应用程序,它可以获取 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 可能只接受字符串?

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";
}
)

查看 Controller

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()

到:

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 is an 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)
}
}

关于ios - 使用 valueForKey 使用 swift 获取整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26309602/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com