0

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 ?

Hamish
  • 78,605
  • 19
  • 187
  • 280
Amani Elsaed
  • 1,558
  • 2
  • 21
  • 36
  • `antiAliasing = dict.value(forKey: "antAliasing") as? NSArray ?? [ANY]` This will clear the error but check if r getting the correct results – Koushik Feb 23 '17 at 12:04
  • U need to unwrap the optional value properly – Koushik Feb 23 '17 at 12:05
  • 3
    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 Feb 23 '17 at 12:06

1 Answers1

4
  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)
}
alexburtnik
  • 7,661
  • 4
  • 32
  • 70