gpt4 book ai didi

ios - 无法从 Parse 加载图像 - Swift 动态转换失败

转载 作者:行者123 更新时间:2023-11-29 02:12:41 25 4
gpt4 key购买 nike

我正在尝试从 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 中正确称为“图像”。用户对象来自前一屏幕的 Segue。当我注释掉图像代码时,用户名字标签会正确显示,因此我知道用户对象是有效的。

如果我在应用程序似乎崩溃的地方设置一个断点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 也是一种很好的做法。例如:
用户名 -> 用户名标签
timer -> timerLabel 等
希望这会有所帮助!

关于ios - 无法从 Parse 加载图像 - Swift 动态转换失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29111262/

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