我正在尝试从View Controller中的Parse中提取用户的图像。但是,每当我运行该应用程序时,它都会因以下错误而崩溃:
libswiftCore.dylib`swift_dynamicCastObjCClassUnconditional:
0x106742620: pushq %rbp
0x106742621: movq %rsp, %rbp
0x106742624: pushq %rbx
0x106742625: pushq %rax
0x106742626: movq %rsi, %rcx
0x106742629: movq %rdi, %rbx
0x10674262c: xorl %eax, %eax
0x10674262e: testq %rbx, %rbx
0x106742631: je 0x10674264c ; swift_dynamicCastObjCClassUnconditional + 44
0x106742633: movq 0x82756(%rip), %rsi ; "isKindOfClass:"
0x10674263a: movq %rbx, %rdi
0x10674263d: movq %rcx, %rdx
0x106742640: callq 0x1067451ca ; symbol stub for: objc_msgSend
0x106742645: testb %al, %al
0x106742647: movq %rbx, %rax
0x10674264a: je 0x106742653 ; swift_dynamicCastObjCClassUnconditional + 51
0x10674264c: addq $0x8, %rsp
0x106742650: popq %rbx
0x106742651: popq %rbp
0x106742652: retq
0x106742653: leaq 0xcdc8(%rip), %rax ; "Swift dynamic cast failed"
0x10674265a: movq %rax, 0x8ae57(%rip) ; gCRAnnotations + 8
0x106742661: int3
0x106742662: nopw %cs:(%rax,%rax)
我知道该图像存在,并且用户属性在Parse中被正确称为“图像”。用户对象来自上一个屏幕的序列。当我注释掉图像代码时,用户名字标签正确显示,因此我知道用户对象有效。
如果在应用程序似乎崩溃的地方设置了一个断点
let userImageFile = user?["image"] as PFFile
,则可以使用鼠标指针悬停在userImageFile上,并且显示“ ObjectiveC.NSObject(NSObject)”。一旦我继续经过断点,应用程序将崩溃并显示上述错误。以下是相关代码:
import UIKit
class GameViewController: UIViewController {
@IBOutlet weak var userName: UILabel!
@IBOutlet weak var timer: UILabel!
@IBOutlet weak var userImage: UIImageView!
var user: PFUser?
override func viewDidLoad() {
super.viewDidLoad()
userName.text = user?["first_name"] as String?
let userImageFile = user?["image"] as PFFile
userImageFile.getDataInBackgroundWithBlock{
(imageData: NSData!, error: NSError!) -> Void in
if error == nil{
let image = UIImage(data: imageData)
self.userImage.image = image
} else {
println(error)
}
}
}
}
有任何想法吗?我认为它必须与userImageFile是ObjectiveC NSObject有关,但我不确定该怎么做。任何帮助是极大的赞赏。
谢谢!
最佳答案
尝试执行以下操作:
override func viewDidLoad() {
super.viewDidLoad()
if let user = user{
userName.text = user["first_name"] as? String
if let userImageFile = user["image"] as? PFFile{
userImageFile.getDataInBackgroundWithBlock { imageData, error in
if error == nil{
let image = UIImage(data: imageData)
self.userImage.image = image
} else {
println(error)
}
}
}
}
}
另外,优良作法是使用
IBOutlet
命名,以使底层类型清晰可见。例如:userName-> userNameLabel
计时器-> timerLabel等。
希望这会有所帮助!
关于ios - 无法从解析中加载图片-Swift动态转换失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29111262/