Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 添加弱子视图导致iOS14崩溃_Swift_Weak References_Ios14_Swift Optionals - Fatal编程技术网

Swift 添加弱子视图导致iOS14崩溃

Swift 添加弱子视图导致iOS14崩溃,swift,weak-references,ios14,swift-optionals,Swift,Weak References,Ios14,Swift Optionals,我得到了iOS14用户的崩溃日志。我能做点什么吗,还是应该等到iOS14正式发布 这是带有文本字段的myCustomTableViewCell: class TextTVCell: UITableViewCell, UITextFieldDelegate { weak var delegate: TextTVCellDelegate? weak var textField: UITextField? = { // I have to make it optional becau

我得到了iOS14用户的崩溃日志。我能做点什么吗,还是应该等到iOS14正式发布

这是带有文本字段的myCustomTableViewCell:

class TextTVCell: UITableViewCell, UITextFieldDelegate {
weak var delegate: TextTVCellDelegate?

weak var textField: UITextField? = {         // I have to make it optional because of weak reference
    let textField = UITextField()
    textField.translatesAutoresizingMaskIntoConstraints = false
    return textField
}()

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    setupSubviews()
}
required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

private func setupSubviews() {
    contentView.addSubview(textField!)
    ......... // constraints, ...
} 
强制展开会导致任何具有iOS14的设备崩溃。在下面,它工作得很好

这是我的事故记录:

Incident Identifier: AB559313-AD92-4B3A-AA90-97AE47315DEF
Hardware Model:      iPhone9,4
Process:             FormaleBriefe [6654]
Path:                /private/var/containers/Bundle/Application/798B5135-DEB8-451D-858F-AE9FA47F6C7B/FormaleBriefe.app/FormaleBriefe
Identifier:          frugalResolution.Briefe
Version:             5 (2.2.2)
AppStoreTools:       11E707
AppVariant:          1:iPhone9,4:13
Code Type:           ARM-64 (Native)
Role:                Foreground
Parent Process:      launchd [1]
Coalition:           frugalResolution.Briefe [1134]


Date/Time:           2020-08-02 22:52:50.5708 +0200
Launch Time:         2020-08-02 22:52:48.3752 +0200
OS Version:          iPhone OS 14.0 (18A5332f)
Release Type:        Beta
Baseband Version:    4.50.04
Report Version:      104

Exception Type:  EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000001, 0x0000000104fac2ac
Termination Signal: Trace/BPT trap: 5
Termination Reason: Namespace SIGNAL, Code 0x5
Terminating Process: exc handler [6654]
Triggered by Thread:  0

Thread 0 name:
Thread 0 Crashed:
0   FormaleBriefe                   0x0000000104fac2ac TextTVCell.setupSubviews() + 3800 (TextTVCell.swift:41)
1   FormaleBriefe                   0x0000000104fab458 TextTVCell.setupSubviews() + 132 (TextTVCell.swift:0)
2   FormaleBriefe                   0x0000000104facb98 specialized TextTVCell.init(style:reuseIdentifier:) + 296 (TextTVCell.swift:32)
3   FormaleBriefe                   0x0000000104fab374 @objc TextTVCell.init(style:reuseIdentifier:) + 72 (<compiler-generated>:0)
4   UIKitCore                       0x000000019b63f448 -[UITableView _dequeueReusableViewOfType:withIdentifier:] + 532 (UITableView.m:8843)
5   FormaleBriefe                   0x0000000104f8ca34 LetterWriterVC.tableView(_:cellForRowAt:) + 1504 (LetterWriterVC.swift:555)
6   FormaleBriefe                   0x0000000104f8d6bc @objc
事件标识符:AB559313-AD92-4B3A-AA90-97AE47315DEF
硬件型号:iPhone9,4
流程:FormalBriefe[6654]
路径:/private/var/containers/Bundle/Application/798B5135-DEB8-451D-858F-AE9FA47F6C7B/formalBriefe.app/formalBriefe
标识符:frugalResolution.Briefe
版本:5(2.2.2)
AppStoreTools:11E707
AppVariant:1:iPhone9,4:13
代码类型:ARM-64(本机)
角色:前台
父进程:已启动[1]
联盟:frugalResolution.简报[1134]
日期/时间:2020-08-02 22:52:50.5708+0200
发射时间:2020-08-02 22:52:48.3752+0200
操作系统版本:iPhone操作系统14.0(18A5332f)
发行类型:Beta版
基带版本:4.50.04
报告版本:104
异常类型:EXC_断点(SIGTRAP)
异常代码:0x0000000000000001、0x0000000104fac2ac
终止信号:跟踪/BPT陷阱:5
终止原因:命名空间信号,代码0x5
终止进程:exc处理程序[6654]
由以下线程触发:0
线程0名称:
线程0崩溃:
0 FormalBriefe 0x0000000104fac2ac textVcell.setupSubview()+3800(textVcell.swift:41)
1 FormalBriefe 0x0000000104fab458 textVcell.setupSubview()+132(textVcell.swift:0)
2 FormalBriefe 0x0000000104facb98专用TextTVCell.init(样式:reuseIdentifier:)+296(TextTVCell.swift:32)
3 FormalBriefe 0x0000000104fab374@objc textVcell.init(样式:reuseIdentifier:)+72(:0)
4 UIKitCore 0x000000019b63f448-[UITableView\u dequeueReusableViewOfType:with Identifier:+532(UITableView.m:8843)
5 FormalBriefe 0x0000000104f8ca34 LetterWriterVC.表格视图(uu:cellForRowAt:)+1504(LetterWriterVC.swift:555)
6 FormalBriefe 0x0000000104f8d6bc@objc

您的代码始终是反模式(又称错误)。一个简单的测试将显示模式的问题。我将使用视图控制器:

weak var textField: UITextField? = { 
    let textField = UITextField()
    return textField
}()

override func viewDidLoad() {
    super.viewDidLoad()
    let tf = self.textField
    print(tf as Any) // nil
}
我们运行应用程序,控制台打印出
nil
。你很容易明白为什么。在某个时刻,
textField
实例属性被初始化,因此闭包运行并返回一个文本字段,并将其分配给
textField
。但是这个参考是很弱的!这意味着:不要抓住我。因此实例属性不保留文本字段;它删除它,文本字段不存在,引用被
nil
替换

简单的解决方法:不要这样做!正如我所说,这是一个反模式。删除单词
,或者,如果您打算保持对文本字段的弱引用,请使用不同的模式,例如:

weak var textField: UITextField?
private func createTextField() -> UITextField  {
    let textField = UITextField()
    self.textField = textField
    return textField
}
override func viewDidLoad() {
    super.viewDidLoad()
    print(tf as Any) // text field
    print(self.textField as Any) // text field
    // and now put it into the interface immediately
    // or it will go out of existence _again!_
}
就我个人而言,我所做的并不是这样。我这样做:

weak var textField: UITextField?
private func createTextField() -> UITextField  {
    let textField = UITextField()
    return textField
}
override func viewDidLoad() {
    super.viewDidLoad()
    let tf = self.createTextField()
    self.textField = tf
    // and now put it into the interface immediately or it will go out of existence
}
在现实生活中,该代码将出现在
设置程序中。换句话说,我将
设置
例程的工作定为生成视图并将它们分配给弱引用,同时将它们放入接口中


当然,还有另一种可能性,那就是放下软弱的
,什么也不做。使
textField
成为有力的参考!对子视图进行强引用绝对没有什么错——当然,除非该子视图也对您进行了强引用,但这种情况不太可能发生,而且如果发生,可以避免。

我很惊讶这种方法居然奏效。试图猜测它为什么会这样是毫无意义的。你的
总是错的。删除它。你绝对不应该等到iOS 14正式发布后再修复iOS 14的bug,这正是开发者测试版的目的。在你的电脑上安装Xcode测试版,在你的一台设备上安装测试版,或者使用模拟器,然后开始修复。这是去年我刚开始编程时的代码。所以,谢谢你现在说清楚了。谢谢你的解释!我真的很喜欢最后一种方法!