简体   繁体   中英

Swift 3 type casting error

I have this code working fine in swift 2.2

let arrayNib = Bundle.main.loadNibNamed("VehicleDetailsTableViewCell", owner: nil, options: nil) as! NSArray

While in swift 3.0 code gives me error

Cannot convert value of [Any]? to type NSArray in coercion

try this

let arrayNib = Bundle.main.loadNibNamed("VehicleDetailsTableViewCell", owner: nil, options: nil)! as [Any]
print (arrayNib)

I can use it like

let arrayNib = Bundle.main.loadNibNamed("VehicleDetailsTableViewCell", owner: nil, options: nil)

It gives me array of [Any?].

Error is pretty explicit.

You're trying convert Optional<T> to say U .

While T is castable to U , Optional<T> is not .

If you want you may cast Optional<T> to Optional<U> . Like in your case [Any]? to NSArray? .

But I strongly recommend to use Swift array and add some real types.

It seems, there is a new array type in Swift that is being used in new APIs; here's what a playground says:

在此处输入图片说明

I guess a follow-up question is whether using NSArray is essential.

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