Array element cannot be bridged to Objective-C
The reason Objective-C is mentioned is because UIKit and QuartzCore are Objective-C frameworks. In particular, gradient.colors = arrayColors
is calling an Objective-C method that expects an NSArray
.
This seems like a bug, as Apple's documentation makes it sound like that the array should auto-bridge to an NSArray
so long as the items in the array can be considered AnyObject
:
When you bridge from a Swift array to an NSArray object, the elements
in the Swift array must be AnyObject compatible. For example, a Swift
array of type Int[] contains Int structure elements. The Int type is
not an instance of a class, but because the Int type bridges to the
NSNumber class, the Int type is AnyObject compatible. Therefore, you
can bridge a Swift array of type Int[] to an NSArray object. If an
element in a Swift array is not AnyObject compatible, a runtime error
occurs when you bridge to an NSArray object.You can also create an NSArray object directly from a Swift array literal, following the same bridging rules outlined above. When you
explicitly type a constant or variable as an NSArray object and assign
it an array literal, Swift creates an NSArray object instead of a
Swift array.
For now, a work around would be either to declare arrayColors
as an NSArray
:
let arrayColors: NSArray = [cor1.CGColor, cor2.CGColor]
Or to declare it as taking AnyObject
:
let arrayColors: Array <AnyObject> = [cor1.CGColor, cor2.CGColor]
fatal error: array cannot be bridged from Objective-C —Why are you even trying, Swift?
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})
Array cannot be bridged from Objective-C error
The problem you're having is in this line of code: jumperCablesRoles.append(rejuvenatedEnterprisesEmployees[i].employeeRoles)
.
As far as I can tell, employeeRoles
is an array of String
s in itself ([String]
). This means that if you add an employee's employeeRoles
to the jumperCablesRoles
array, jumperCablesRoles
is actually an array which contains an array of String
s. ([[String]]
). This is why, when you try to force cast it into [String]
it will fail.
I'm not sure exactly what you want the result to be, so I can't help you with that, but you can try to append each element of employeeRoles
separately to jumperCablesRoles
if that's what you're going for. If that's what you want, just leave a comment and I'll provide additional code.
Edit: I just went ahead and found a solution anyways, simply replace jumperCablesRoles.append(rejuvenatedEnterprisesEmployees[i].employeeRoles)
with the following:
rejuvenatedEnterprisesEmployees[i].employeeRoles.forEach {jumperCablesRoles.append($0)}
This adds every employeeRole
(which is a String
) to jumperCablesRoles
separately.
Annotate NSArray NSNumber * * in Objective-C so that it is bridged to Array Int
Unfortunately you can't change the static type when importing a symbol from Objective-C.
But you can assign a Swift Int
array without any type cast (assuming foo
is an instance of the ObjC class). However the type doesn't change.
foo.myProperty = [1, 2, 3, 4, 5]
print(type(of: foo.myProperty)) // Optional<Array<NSNumber>>
On the other hand to get a distinct [Int]
type you have to cast the type
let mySwiftProperty = foo.myProperty as! [Int]
print(type(of: mySwiftProperty)) // Array<Int>
UIImagePickerController and fatal error: array element cannot be bridged to Objective-C
You need to import MobileCoreServices
as kUTTypeImage
defined in MobileCoreServices
as let kUTTypeImage: CFString!
.
So add the framework MobileCoreServices
and write import MobileCoreServices
in your .swift
file.
Go to BuildPhase
-> Link Libraries
-> +
-> MobileCoreServices.framework
and add import MobileCoreServices
in your .swift
file.
Edit: Replace your line with as kUTTypeImage is optional so unwrap it
cameraUI.mediaTypes = [kUTTypeImage!]
Objective-C Bridge NSArray Crash
Your call to NSLog
is incorrect. The first parameter of NSLog
is the format string. You meant:
NSLog(@"%@", myObjectiveCNSArray);
I'm surprised you didn't get a warning about this.
Related Topics
Swift - Increment Label With Stepper in Tableview Cell
Nsfontattributedstring Worked Before Xcode 6.1
The Strange Behaviour of Swift's Anyobject
Why Non Optional Any Can Hold Nil
Intrinsiccontentsize() - Method Does Not Override Any Method from Its Superclass
Error When Trying to Save Image in Nsuserdefaults Using Swift
Using Combine's Future to Replicate Async Await in Swift
Swiftui: Global Overlay That Can Be Triggered from Any View
Do Capture Lists of Inner Closures Need to Redeclare 'Self' as 'Weak' or 'Unowned'
Add @Published Behaviour for Computed Property
Deep Copy for Array of Objects in Swift
Swift 3: Set Finder Label Color
How to Add Material to Modelentity Programatically in Realitykit
Firebase Instanceid.Instanceid().Token() Method Is Deprecated
How to Have Two Alerts on One View in Swiftui
Self' Used Before All Stored Properties Are Initialized
How to Make #Selector Refer to a Closure in Swift
Swift 3 Error:Argument Labels '(_:)' Do Not Match Any Available Overloads