无法将'NSArray'类型的值转换为期望的参数类型'[Any]!'

问题描述:

我已经添加了Objective-C类别到我的Swift 3.0代码。它有头的方法:无法将'NSArray'类型的值转换为期望的参数类型'[Any]!'

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

当我把它从我的银行代码:

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

我得到了错误:

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

什么是什么问题?

+0

'抗锯齿= dict.value(forKey: “antAliasing”)作为? NSArray ?? [ANY]'这将清除错误,但检查r是否得到正确的结果 – Koushik

+0

U需要正确打开可选值 – Koushik

+3

从给定的参数标签'pointsAsNSValues'我试试'as! [NSValue]'。除非你能解释你为什么需要KVC,否则不要使用'valueForKey'。 – vadian

  1. NSArray的替换用[NSValue],如@vadian建议
  2. 使用选购的转换as?,因为它是更可靠的,如果转换失败,你不会得到崩溃。
  3. 使用下标代替valueForKey方法。

下面是代码:

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