Search code examples
swiftobjective-c-swift-bridge

Cannot convert value of type 'NSArray' to expected argument type '[Any]!'


I have added an Objective-C category to my Swift 3.0 code. It has a method with header:

+(UIBezierPath *)interpolateCGPointsWithHermite:(NSArray *)pointsAsNSValues closed:(BOOL)closed;

When I call it from my Swift code:

antiAliasing = dict.value(forKey: "antAliasing") as! NSArray
UIBezierPath.interpolateCGPoints(withHermite: antiAliasing, closed: true)

I got the error:

Cannot convert value of type 'NSArray' to expected argument type '[Any]!'

What is the problem with that ?


Solution

    1. Replace NSArray with [NSValue], as @vadian suggested
    2. Use optional conversion as?, since it is more reliable and you won't get a crash if conversion fails.
    3. Use subscript instead of valueForKey method.

    Here is the code:

    if let antiAliasing = dict["antAliasing"] as? [NSValue] {
        UIBezierPath.interpolateCGPoints(withHermite: antiAliasing, closed: true)
    }