(lldb) 从 swift 中的解析检索数据时出错
(lldb) error while retrieving datas from parse in swift
我正在使用以下函数从 swift 中的解析中检索数据。但是我在行 "results.objectAtIndex(i) as NSDictionary" 中收到错误。我不知道什么错误以及为什么会出现该错误?请阅读我的代码并指导我!
func parse_db() {
var sto_tit = [String] ()
var results : NSArray = NSArray()
var par_object : NSDictionary = NSDictionary()
var par_query = PFQuery(className: "story")
par_query.whereKeyExists("story_title")
par_query.findObjectsInBackgroundWithBlock({(NSArray objects, NSError error) in
if (error != nil) {
NSLog("error " + error.localizedDescription)
} else {
results = NSArray(array: objects)
for i in 0...results.count {
println(i) //This line working
par_object = results.objectAtIndex(i) as NSDictionary //Error
sto_tit = par_object.objectForKey("story_title") as Array
}
NSLog("results %@", sto_tit)
}
})
}
错误如下:
0
(lldb)
{
libswiftCore.dylib`swift_dynamicCastObjCClassUnconditional:
0x10adfc620: pushq %rbp
0x10adfc621: movq %rsp, %rbp
0x10adfc624: pushq %rbx
0x10adfc625: pushq %rax
0x10adfc626: movq %rsi, %rcx
0x10adfc629: movq %rdi, %rbx
0x10adfc62c: xorl %eax, %eax
0x10adfc62e: testq %rbx, %rbx
0x10adfc631: je 0x10adfc64c ;swift_dynamicCastObjCClassUnconditional
+ 44
0x10adfc633: movq 0x82756(%rip), %rsi ; "isKindOfClass:"
0x10adfc63a: movq %rbx, %rdi
0x10adfc63d: movq %rcx, %rdx
0x10adfc640: callq 0x10adff1ca ; symbol stub for: objc_msgSend
0x10adfc645: testb %al, %al
0x10adfc647: movq %rbx, %rax
0x10adfc64a: je 0x10adfc653 ; swift_dynamicCastObjCClassUnconditional + 51
0x10adfc64c: addq [=2=]x8, %rsp
0x10adfc650: popq %rbx
0x10adfc651: popq %rbp
0x10adfc652: retq
0x10adfc653: leaq 0xcdc8(%rip), %rax ; "Swift dynamic cast failed"
0x10adfc65a: movq %rax, 0x8ae57(%rip) ; gCRAnnotations + 8
0x10adfc661: int3
0x10adfc662: nopw %cs:(%rax,%rax)
}
findObjectsInBackgroundWithBlock:
returns 一个 PFObjects 的 NSArray。所以当你尝试
var par_object = results.objectAtIndex(i) as NSDictionary
会失败,因为结果的第 i 个对象是 PFObject,无法通过这种方式转换为 NSDictionary。
但是你可以使用
par_object = results.objectAtIndex(i) as PFObject
sto_tit = par_object["story_title"] as [String] //Are you sure about that the "story_title" column is an array?
你的声明是错误的使用
var par_object = PFObject(className: "YOUR_CLASS_NAME")
我正在使用以下函数从 swift 中的解析中检索数据。但是我在行 "results.objectAtIndex(i) as NSDictionary" 中收到错误。我不知道什么错误以及为什么会出现该错误?请阅读我的代码并指导我!
func parse_db() {
var sto_tit = [String] ()
var results : NSArray = NSArray()
var par_object : NSDictionary = NSDictionary()
var par_query = PFQuery(className: "story")
par_query.whereKeyExists("story_title")
par_query.findObjectsInBackgroundWithBlock({(NSArray objects, NSError error) in
if (error != nil) {
NSLog("error " + error.localizedDescription)
} else {
results = NSArray(array: objects)
for i in 0...results.count {
println(i) //This line working
par_object = results.objectAtIndex(i) as NSDictionary //Error
sto_tit = par_object.objectForKey("story_title") as Array
}
NSLog("results %@", sto_tit)
}
})
}
错误如下: 0 (lldb)
{
libswiftCore.dylib`swift_dynamicCastObjCClassUnconditional:
0x10adfc620: pushq %rbp
0x10adfc621: movq %rsp, %rbp
0x10adfc624: pushq %rbx
0x10adfc625: pushq %rax
0x10adfc626: movq %rsi, %rcx
0x10adfc629: movq %rdi, %rbx
0x10adfc62c: xorl %eax, %eax
0x10adfc62e: testq %rbx, %rbx
0x10adfc631: je 0x10adfc64c ;swift_dynamicCastObjCClassUnconditional
+ 44
0x10adfc633: movq 0x82756(%rip), %rsi ; "isKindOfClass:"
0x10adfc63a: movq %rbx, %rdi
0x10adfc63d: movq %rcx, %rdx
0x10adfc640: callq 0x10adff1ca ; symbol stub for: objc_msgSend
0x10adfc645: testb %al, %al
0x10adfc647: movq %rbx, %rax
0x10adfc64a: je 0x10adfc653 ; swift_dynamicCastObjCClassUnconditional + 51
0x10adfc64c: addq [=2=]x8, %rsp
0x10adfc650: popq %rbx
0x10adfc651: popq %rbp
0x10adfc652: retq
0x10adfc653: leaq 0xcdc8(%rip), %rax ; "Swift dynamic cast failed"
0x10adfc65a: movq %rax, 0x8ae57(%rip) ; gCRAnnotations + 8
0x10adfc661: int3
0x10adfc662: nopw %cs:(%rax,%rax)
}
findObjectsInBackgroundWithBlock:
returns 一个 PFObjects 的 NSArray。所以当你尝试
var par_object = results.objectAtIndex(i) as NSDictionary
会失败,因为结果的第 i 个对象是 PFObject,无法通过这种方式转换为 NSDictionary。
但是你可以使用
par_object = results.objectAtIndex(i) as PFObject
sto_tit = par_object["story_title"] as [String] //Are you sure about that the "story_title" column is an array?
你的声明是错误的使用
var par_object = PFObject(className: "YOUR_CLASS_NAME")