gpt4 book ai didi

swift - 为什么我得到一个 Thread 1 : EXC_BAD_ACCESS (code = EXC_I386_GPFLT)

转载 作者:行者123 更新时间:2023-11-28 05:56:02 31 4
gpt4 key购买 nike

var soundPoolLabel: UILabel {
let label = UILabel(frame: CGRect(x: 20, y: 90, width: 540, height: 94))
label.text = "SoundPool"
label.textColor = UIColor.black
label.font = UIFont(name: "Bodoni 72 Oldstyle", size: 80)
let attributedString = NSMutableAttributedString(string: label.text!)
attributedString.addAttribute(kCTKernAttributeName as NSAttributedStringKey, value: CGFloat(1.0), range: NSRange(location: 0, length: attributedString.length))
label.attributedText = attributedString
return label
}

soundPoolLabel.translatesAutoresizingMaskIntoConstraints = false
let topConstraint = soundPoolLabel.topAnchor.constraint(equalTo: view.topAnchor, constant: 90)
NSLayoutConstraint.activate([topConstraint])

最佳答案

标签不应是计算属性。它应该只初始化一次。做这样的事情来解决问题。

var soundPoolLabel: UILabel = {
let label = UILabel(frame: CGRect(x: 20, y: 90, width: 540, height: 94))
label.text = "SoundPool"
label.textColor = UIColor.black
label.font = UIFont(name: "Bodoni 72 Oldstyle", size: 80)
let attributedString = NSMutableAttributedString(string: label.text!)
attributedString.addAttribute(kCTKernAttributeName as NSAttributedStringKey, value: CGFloat(1.0), range: NSRange(location: 0, length: attributedString.length))
label.attributedText = attributedString
return label
}()

这是因为每次您使用soundPoolLabel 时,它都会创建一个新实例,而不是使用同一个实例。系统在 subview 层级中找不到新的实例,抛出EXC_BAD_ACCESS错误

关于swift - 为什么我得到一个 Thread 1 : EXC_BAD_ACCESS (code = EXC_I386_GPFLT),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51488929/

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