无法将'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]!'
什么是什么问题?
答
- NSArray的替换用[NSValue],如@vadian建议
- 使用选购的转换
as?
,因为它是更可靠的,如果转换失败,你不会得到崩溃。 - 使用下标代替
valueForKey
方法。
下面是代码:
if let antiAliasing = dict["antAliasing"] as? [NSValue] {
UIBezierPath.interpolateCGPoints(withHermite: antiAliasing, closed: true)
}
'抗锯齿= dict.value(forKey: “antAliasing”)作为? NSArray ?? [ANY]'这将清除错误,但检查r是否得到正确的结果 – Koushik
U需要正确打开可选值 – Koushik
从给定的参数标签'pointsAsNSValues'我试试'as! [NSValue]'。除非你能解释你为什么需要KVC,否则不要使用'valueForKey'。 – vadian