我正在使用下面的函数从swift中的parse中检索数据。但我在“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 $0x8, %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)
}
答案 0 :(得分:0)
findObjectsInBackgroundWithBlock:
返回一个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")