ios - fetching integers using valueForKey using swift -
i have developed easy app fetches json info , puts values within tableview. works fine when utilize valueforkey , fetch string value.
but when seek fetch integer id....i error after app launches successfully.
output shows nil
green error
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)
is there anyway fetch id valueforkey or method? or there way convert string given valueforkey may take strings?
new ios please help.
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"; } )
view 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 info json self.jsondata = nsjsonserialization.jsonobjectwithdata(data, options: nsjsonreadingoptions.mutablecontainers, error: nil) nsarray! ### grab info populating tableview ### id integer value, how can fetch if can't utilize valueforkey? self.mydata = self.jsondata.valueforkey("id") nsarray! ###this works fine //self.mydata = self.jsondata.valueforkey("name") nsarray! dispatch_async(dispatch_get_main_queue()) { // reload tableview self.tableview.reloaddata() } } task.resume() }
change:
var mydata = nsarray()
to:
var mydata:[int] = []
and replace:
self.mydata = self.jsondata.valueforkey("id") nsarray!
with:
for dict in self.jsondata [nsdictionary] { if allow idnum = dict["id"] as? int { println("id int") self.mydata.append(idnum) } else if allow idnum = dict["id"] as? string { println("id string") self.mydata.append(idnum.toint() ?? 0) } }
according the op, printed "id int" multiple times, know id int
can clean up:
// loop on array of dictionaries dict in self.jsondata [nsdictionary] { // entry, id number dictionary // , append mydata array if allow idnum = dict["id"] as? int { self.mydata.append(idnum) } }
ios json uitableview nsarray nsurlsession
No comments:
Post a Comment