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

Robert Atkins picture Robert Atkins · May 7, 2015 · Viewed 12.5k times · Source

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

Robert Atkins picture Robert Atkins · May 7, 2015

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})