Array element cannot be bridged to Objective-C

后端 未结 5 1435
庸人自扰
庸人自扰 2020-12-01 14:13

I have this code that creates a view and applies a gradient to it.

import UIKit
import QuartzCore


let rect : CGRect = CGRectMake(0,0,320,100)

var vista :          


        
相关标签:
5条回答
  • 2020-12-01 14:16

    I found that I could fix the problem by explicitly using CGColorRef rather than CGColor for my colours, e.g.:

        var bottomColour:CGColorRef = UIColor.redColor().CGColor
        var topColour:CGColorRef = UIColor(red: 255.0/255.0, green: 123.0/255.0, blue: 37.0/255.0, alpha: 1.0).CGColor
    
        gradientLayer.colors = [bottomColour, topColour]
    

    ...worked fine, without any NSArray or AnyObject casting. If I take out the explicit CGColorRef in the type declarations for the colours, I get the "array element cannot be bridged to Objective-C" error.

    0 讨论(0)
  • 2020-12-01 14:22

    With Swift arrays, you can call bridgeToObjectiveC() on them, and they'll turn into NSArrays. The same is true of Swift dictionaries.

    0 讨论(0)
  • 2020-12-01 14:27

    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]

    0 讨论(0)
  • 2020-12-01 14:34

    This runtime error can also be triggered if it tries to bridge an array of type [MySwiftProtocol] over to Objective-C.

    The solution is to mark your protocol with @objc:

    @objc protocol MySwiftProtocol {
       // ...
    }
    
    0 讨论(0)
  • 2020-12-01 14:38

    For anyone trying to get this to work in Swift, you can do the following:

    let arrayColors: NSArray = [UIColor.blackColor().CGColor as AnyObject, UIColor.clearColor().CGColor as AnyObject]
    

    I noticed that in my Objective-C code that I was casting the CGColorRef types to id. Not sure why though. If anyone has a reason why that would be great!

    0 讨论(0)
提交回复
热议问题