将代码更新到 xcode 9 和 swift 4 后应用程序崩溃

Posted

技术标签:

【中文标题】将代码更新到 xcode 9 和 swift 4 后应用程序崩溃【英文标题】:app crashing after updating code to xcode 9 and swift 4 【发布时间】:2017-10-10 10:35:34 【问题描述】:

将项目升级到 xcode 9 和 swift 4 后应用程序崩溃。在调试时

    backview.addsubview(stackview)

应用程序因 libc++abi.dylib 崩溃:以 NSException 类型的未捕获异常终止。 代码在 swift 3 中完美运行。但在迁移到 swift 4 后它崩溃了。这是我的全部功能。

    var isAlreadyThere = false

    if let audioView = self.view.subviews.filter (
         $0.tag == 123456).first  

        audioView.tag = 123456
        isAlreadyThere = true
    

    if isAlreadyThere 
        return
    

    let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
    let backView = UIVisualEffectView(effect: blurEffect)
    backView.tag = 123456
    backView.frame = CGRect(x: 0, y: self.view.frame.height+35 , width: self.view.frame.width, height: 40)
    backView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    view.addSubview(backView)

    pauseButton = UIButton(type: .custom)
    pauseButton.titleLabel?.font = Util.getFontSize()
    pauseButton.setTitle("Pause", for: .normal)
    pauseButton.setTitleColor(UIColor.black, for: .normal)
    pauseButton.addTarget(self, action: #selector(pausePressed(_:)), for: .touchDown)

    durationLabel = UILabel()
    durationLabel.textColor = UIColor.black
    durationLabel.font = Util.getFontSize()
    durationLabel.textAlignment = .center
    durationLabel.text = "0:00"

    stopButton = UIButton(type: .custom)
    stopButton.titleLabel?.font = Util.getFontSize()
    stopButton.setTitle("Stop", for: .normal)
    stopButton.setTitleColor(UIColor.black, for: .normal)
    stopButton.addTarget(self, action: #selector(stopPressed(_:)), for: .touchDown)

    let stackView = UIStackView(frame: CGRect(x: 0, y:0 , width: backView.frame.width, height: 40))
    stackView.alignment = .fill
    stackView.axis = .horizontal
    stackView.distribution = .fillEqually
    stackView.spacing = 5
    stackView.addArrangedSubview(pauseButton)
    stackView.addArrangedSubview(durationLabel)
    stackView.addArrangedSubview(stopButton)

    backView.addSubview(stackView)

    if audioViewYposition == nil 
        audioViewYposition = self.view.frame.height - 40
    

    UIView.animate(withDuration: 0.2, delay: 0.1, options: .curveEaseInOut, animations: 
        backView.frame.origin.y = self.audioViewYposition
    , completion: nil)

【问题讨论】:

尝试添加异常断点,看看崩溃发生在哪里。 【参考方案1】:

如错误所述,您需要在内容视图上添加子视图,而不是直接在视觉效果视图上,因此解决方案是:

backView.contentView.addSubview(stackView)

确保您的异常断点已启用以从断点跳过几帧以在控制台中查看错误消息:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: '<UIStackView: 0x109946ff0; frame = (0 0; 375 40); layer = <CATransformLayer: 0x1c4236640>> 
has been added as a subview to 
<UIVisualEffectView: 0x109945f40; frame = (0 702; 375 40); autoresize = W+H; tag = 123456; layer = <CALayer: 0x1c002ebc0>>. 
Do not add subviews directly to the visual effect view itself, 
instead add them to the -contentView.'

【讨论】:

感谢您的回答。是的,我添加的是 backView 而不是 contentView。但它在 swift 3 中工作。它在迁移 swift 4 后才开始崩溃。

以上是关于将代码更新到 xcode 9 和 swift 4 后应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章

更新到 Xcode 9 / Swift 4 后 AVCapture 出现奇怪的编译错误 [重复]

无法在 Alamofire 中禁用缓存(Xcode 9 - swift 4)

Xcode 存档验证错误 - Xcode 9 和 Swift 4

Xcode 9 Swift 4 Playgrounds UIGestureRecognizer 不工作

Xcode 9.3.1 - iOS Swift 4.1 UITextView.becomeFirstResponder 线程 1:EXC_BAD_ACCESS(代码=1,地址=0x4d555458)

Xcode 9:使用 Swift 3.1 编译的模块无法在 Swift 4.0 中导入