Switched to swift 2.0 and getting AnyObject error

228 views Asked by At

Why cam I getting the Cannot convert value of type [AnyObject]! to expected argument type. I am trying to load a nib into a view controller.

func loadNibNamed(name: String!, owner: AnyObject!, options: [NSObject : AnyObject]!) -> [View1]!{
    NSBundle.mainBundle(loadNibNamed("View1", owner: self, options: nil)).lastObject
}
2

There are 2 answers

0
Alex On

I think this is right code in Swift2.0

NSBundle.mainBundle().loadNibNamed("View1", owner: self, options: nil).last

lastObject is for Swift1.2 and now it isn't available in swift2.0 use last instead.

And to use it , you can convert it into UIView.

2
phuongle On

Try this:

func loadNibNamed(name: String, owner: AnyObject, options: [NSObject : AnyObject]) -> [AnyObject] {
        return NSBundle.mainBundle().loadNibNamed(name, owner: owner, options: options)
}