简体   繁体   中英

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. After I build it with XCode 7.0 GM it started to crash on start. 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. I have AddView.xib and AddView2.xib files, in which file's owner is set respectively to AddView and 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.

Is there any changes in the XCode 7.0(.1), which causing such problems? 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? Any help is highly appreciated!

EDIT1: Here is the whole project

EDIT2: Just found out that if I copy the loadViewWithName content to loadView , it doesn't crash.

I found solution to the problem - made the global function UIView extension. I'm still not sure what was the problem and why the code doesn't work with global function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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