arrays - swift 错误 : Cannot convert value of type 'ArraySlice' to expected argument type

标签 arrays swift

我遇到了这个错误,我是 Swift 的新手。我想获取 >= 5 的数组的最后 5 个点,并将这 5 个点作为数组参数传递给函数。我怎样才能做到这一点并克服这个错误?

无法将“ArraySlice”类型的值转换为预期的参数类型“[CGPoint]”

if (self.points?.count >= 5) {
    let lastFivePoints = self.points![(self.points!.count-5)..<self.points!.count]
    let angle = VectorCalculator.angleWithArrayOfPoints(lastFivePoints)
}

最佳答案

你需要转换ArraySliceArray使用方法Array(Slice<Type>)

if (self.points?.count >= 5) {
    let lastFivePoints = Array(self.points![(self.points!.count-5)..<self.points!.count])
    let angle = VectorCalculator.angleWithArrayOfPoints(lastFivePoints)
}

关于arrays - swift 错误 : Cannot convert value of type 'ArraySlice' to expected argument type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35028784/

相关文章:

javascript - 每次将 i 与 array.length 进行比较时,循环都会检查 array.length 吗?

ios - 使用 PropertyWrapper 时同时访问

ios - 如何等到委托(delegate)方法被调用?

swift - 添加阴影以仅用两个带半径的角进行 View

c# - 一维数组碰撞

arrays - 为什么 char** argv 与 char* argv[] 相同

c - 在C中打印出随机数数组

java - 为什么我的循环在计算最后一个数字之前就跳出了?

ios - 滚动 tableview 时 UITableViewCells 内容动画变得乱七八糟

swift - Realm :Swift 2 中的变量查询