简体   繁体   English

切换到XCode 7.0(.1)后,发布模式下的自定义视图(XIB)崩溃

[英]Custom view (XIB) crash in Release mode after switching to XCode 7.0(.1)

I've had an app which worked perfectly fine until XCode 7.0 GM. 我有一个在XCode 7.0 GM之前运行良好的应用程序。 After I build it with XCode 7.0 GM it started to crash on start. 当我使用XCode 7.0 GM构建它之后,它在启动时开始崩溃。 It crash on loading one of my custom views, code below: 加载我的自定义视图之一时崩溃,代码如下:

func loadViewWithName(viewName: String, owner: UIView) {
    let view = NSBundle.mainBundle().loadNibNamed(viewName, owner: owner, options: nil).first as! UIView
    view.frame = owner.bounds
    owner.addSubview(view)
}

class AddView2 : UIView {
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        loadView()
    }

    @IBOutlet weak var pendingToAddNotification: UIView!

    private func loadView() {
        loadViewWithName("AddView2", owner: self)

        // print("asd") // if this is uncommented the app doesn't crash

        pendingToAddNotification.backgroundColor = UIColor.blackColor() // CRASH HERE, when I try to to change something in the loaded view, most probably because the outlet isn't set (it is nil), which was my assumption since it is called after view is loaded. 
    }
}

class AddView : UIView {
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        loadView()
    }

    @IBOutlet weak var pendingToAddNotification: AddView2!

    private func loadView() {
        loadViewWithName("AddView", owner: self)

        pendingToAddNotification.backgroundColor = UIColor.blackColor()
    }
}

, where AddView contains AddView2, and ViewController contains AddView. ,其中AddView包含AddView2,而ViewController包含AddView。 I have AddView.xib and AddView2.xib files, in which file's owner is set respectively to AddView and AddView2. 我有AddView.xib和AddView2.xib文件,其中文件的所有者分别设置为AddView和AddView2。

I thought that I was doing something wrong, but it is very strange why the same code is working in DEBUG mode and more interesting why if I uncomment the mentioned above print the app doesnt crash and starts as expected. 我以为我做错了什么,但是很奇怪为什么相同的代码在DEBUG模式下工作,更有趣的是,如果我取消注释上述print ,应用程序不会崩溃并按预期启动。

Is there any changes in the XCode 7.0(.1), which causing such problems? XCode 7.0(.1)中是否有任何更改会导致此类问题? It looks like some sort of race condition, which is fixed by the print (since it needs additional time to execute) or by Debug mode (since it is slower), but as far as I know both views' loading must be done in the main thread, or am I wrong? 它看起来像某种竞态条件,由print (因为它需要更多的时间来执行)或由调试模式(因为它的运行速度较慢)解决,但据我所知,两个视图的加载都必须在主线程,还是我错了? Any help is highly appreciated! 任何帮助深表感谢!

EDIT1: Here is the whole project 编辑1:这是整个项目

EDIT2: Just found out that if I copy the loadViewWithName content to loadView , it doesn't crash. EDIT2:刚刚发现,如果我将loadViewWithName内容复制到loadView ,它不会崩溃。

I found solution to the problem - made the global function UIView extension. 我找到了解决问题的方法-做了全局函数UIView扩展。 I'm still not sure what was the problem and why the code doesn't work with global function. 我仍然不确定是什么问题,为什么代码不能与全局函数一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM