Robert Atkins Robert Atkins - 4 years ago 289
Swift Question

"fatal error: array cannot be bridged from Objective-C"—Why are you even trying, Swift?

I have declared a Swift protocol:

protocol Option {
var name: String { get }
}


I declare multiple implementations of this protocol—some classes, some enums.

I have a view controller with a property declared as so:

var options: [Option] = []


When I try and set this property to an array of objects that implement the
Option
protocol in another VC's
prepareForSegue
, I get a runtime error:

fatal error: array cannot be bridged from Objective-C


Why doesn't this work? The compiler has all the information it needs, and I don't understand what Objective-C has to do with it at all—my project contains only Swift files, and these arrays aren't coming in or out of any framework methods that would necessitate them being bridged to
NSArray
.

Answer Source

I have found a solution. It is quite... unsatisfying, but it works. Where I set the array on the destination view controller I do:

destinationViewController.options = options.map({$0 as Option})
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download