Parv Bhasker
Parv Bhasker

Reputation: 1901

Segmentation fault: 11, xcode -12

the app is working on xcode 11, but as soon as I run it on xcode 12 it gives Segmentation fault: 11 error.

form debugging find out that the issue is in this function, if I comment the code it works fine then, didn't seem any error to me, is there anything happening ?

open func addViewController(_ vc:UIViewController)->Void{
    
    controllers.append(vc)
    
    // Setup the viewController view
    
    vc.view.translatesAutoresizingMaskIntoConstraints = false
    scrollview.addSubview(vc.view)
    
    // Constraints
    
    let metricDict = ["w":vc.view.bounds.size.width,"h":vc.view.bounds.size.height]
    
    // - Generic cnst
    
    vc.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[view(h)]", options:[], metrics: metricDict, views: ["view":vc.view] as [String: UIView]))
    vc.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[view(w)]", options:[], metrics: metricDict, views: ["view":vc.view] as [String: UIView]))
    scrollview.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[view]|", options:[], metrics: nil, views: ["view":vc.view] as [String: UIView]))
    
    // cnst for position: 1st element
    
    if controllers.count == 1{
        scrollview.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[view]", options:[], metrics: nil, views: ["view":vc.view] as [String: UIView]))
        // cnst for position: other elements
    } else {
        
        let previousVC = controllers[controllers.count-2]
        if let previousView = previousVC.view {
            // For this constraint to work, previousView can not be optional
            scrollview.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[previousView]-0-[view]", options:[], metrics: nil, views: ["previousView":previousView,"view":vc.view] as [String: UIView]))
        }
        
        if let cst = lastViewConstraint {
            scrollview.removeConstraints(cst)
        }
        lastViewConstraint = NSLayoutConstraint.constraints(withVisualFormat: "H:[view]-0-|", options:[], metrics: nil, views: ["view":vc.view] as [String: UIView])
        scrollview.addConstraints(lastViewConstraint!)
    }
}

Upvotes: 0

Views: 676

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100543

Parts of code like this

let metricDict = ["w":vc.view.bounds.size.width,"h":vc.view.bounds.size.height]

,

["view":vc.view] as [String: UIView]

and

["previousView":previousView,"view":vc.view] as [String: UIView]

could cause that problem , so you can try to light-weight segmentation work so compiler can pass from it

open func addViewController(_ vc:UIViewController)->Void{
    
    controllers.append(vc)
    
    // Setup the viewController view
    
    vc.view.translatesAutoresizingMaskIntoConstraints = false
    scrollview.addSubview(vc.view)
    
    // Constraints
    
    let metricDict:[String:CGFloat] = ["w":vc.view.bounds.size.width,"h":vc.view.bounds.size.height]
    
    // - Generic cnst
    
    let con:[String: UIView] = ["view":vc.view]
    
    vc.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[view(h)]", options:[], metrics: metricDict, views: con ))
    vc.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[view(w)]", options:[], metrics: metricDict, views: con ))
    scrollview.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[view]|", options:[], metrics: nil, views: con ))
    
    // cnst for position: 1st element
    
    if controllers.count == 1{
        scrollview.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[view]", options:[], metrics: nil, views: con))
        // cnst for position: other elements
    } else {
        
        let previousVC = controllers[controllers.count-2]
        let relt:[String: UIView] = ["previousView":previousView,"view":vc.view]
        if let previousView = previousVC.view {
            // For this constraint to work, previousView can not be optional
            scrollview.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:[previousView]-0-[view]", options:[], metrics: nil, views:relt ))
        }
        
        if let cst = lastViewConstraint {
            scrollview.removeConstraints(cst)
        }
        lastViewConstraint = NSLayoutConstraint.constraints(withVisualFormat: "H:[view]-0-|", options:[], metrics: nil, views: con)
        scrollview.addConstraints(lastViewConstraint!)
    }
}

Upvotes: 1

Related Questions