cocoa - Swift 3 Load xib. NSBundle.mainBundle().loadNibNamed return Bool -


i trying figure out how create custom view using xib files. in question next method used.

nsbundle.mainbundle().loadnibnamed("cardview", owner: nil, options: nil)[0] as! uiview 

cocoa has same method,however, method has changed in swift 3 loadnibnamed(_:owner:toplevelobjects:), returns bool, , previous code generates "type bool has no subscript members" error, obvious, since return type bool.

so, question how load view xib file in swift 3

first of method has not been changed in swift 3.

loadnibnamed(_:owner:toplevelobjects:) has been introduced in macos 10.8 , present in versions of swift. loadnibnamed(nibname:owner:options:) has been dropped in swift 3.

the signature of method is

func loadnibnamed(_ nibname: string,                        owner: any?,              toplevelobjects: autoreleasingunsafemutablepointer<nsarray>?) -> bool 

so have create pointer array of views on return.

var toplevelobjects = nsarray() if bundle.main.loadnibnamed("cardview", owner: self, toplevelobjects: &toplevelobjects) {    let views = (toplevelobjects array).filter { $0 nsview }    return views[0] as! nsview } 

edit: updated answer filter nsview instance reliably.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -