0
votes

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 ?

1
antiAliasing = dict.value(forKey: "antAliasing") as? NSArray ?? [ANY] This will clear the error but check if r getting the correct resultsKoushik
U need to unwrap the optional value properlyKoushik
From the given parameter label pointsAsNSValues I'd try as! [NSValue]. And don't use valueForKey unless you can explain why you need KVC.vadian

1 Answers

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