无法将类型 'NSArray' 的值转换为预期的参数类型“[Any]!”
Cannot convert value of type 'NSArray' to expected argument type '[Any]!'
我已将 Objective-C 类别添加到我的 Swift 3.0 代码中。它有一个方法 header:
+(UIBezierPath *)interpolateCGPointsWithHermite:(NSArray *)pointsAsNSValues closed:(BOOL)closed;
当我从我的 Swift 代码调用它时:
antiAliasing = dict.value(forKey: "antAliasing") as! NSArray
UIBezierPath.interpolateCGPoints(withHermite: antiAliasing, closed: true)
我收到错误:
Cannot convert value of type 'NSArray' to expected argument type
'[Any]!'
这有什么问题?
- 按照@vadian 的建议将 NSArray 替换为 [NSValue]
- 使用可选转换
as?
,因为它更可靠,如果转换失败也不会崩溃。
- 使用下标代替
valueForKey
方法。
代码如下:
if let antiAliasing = dict["antAliasing"] as? [NSValue] {
UIBezierPath.interpolateCGPoints(withHermite: antiAliasing, closed: true)
}
我已将 Objective-C 类别添加到我的 Swift 3.0 代码中。它有一个方法 header:
+(UIBezierPath *)interpolateCGPointsWithHermite:(NSArray *)pointsAsNSValues closed:(BOOL)closed;
当我从我的 Swift 代码调用它时:
antiAliasing = dict.value(forKey: "antAliasing") as! NSArray
UIBezierPath.interpolateCGPoints(withHermite: antiAliasing, closed: true)
我收到错误:
Cannot convert value of type 'NSArray' to expected argument type '[Any]!'
这有什么问题?
- 按照@vadian 的建议将 NSArray 替换为 [NSValue]
- 使用可选转换
as?
,因为它更可靠,如果转换失败也不会崩溃。 - 使用下标代替
valueForKey
方法。
代码如下:
if let antiAliasing = dict["antAliasing"] as? [NSValue] {
UIBezierPath.interpolateCGPoints(withHermite: antiAliasing, closed: true)
}