我收到了这个错误,并且是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)
}发布于 2016-01-27 12:26:53
您需要使用Array(Slice<Type>)方法将ArraySlice转换为Array
if (self.points?.count >= 5) {
let lastFivePoints = Array(self.points![(self.points!.count-5)..<self.points!.count])
let angle = VectorCalculator.angleWithArrayOfPoints(lastFivePoints)
}发布于 2017-07-12 02:49:00
您可以使用返回ArraySlice的前缀(upTo end: Self.Index)方法代替范围操作符,这会使代码更短。方法的定义:该方法返回从集合开始到(但不包括)指定位置(索引)的子序列。
if (self.points?.count >= 5) {
let lastFivePoints = Array<CGPoint>(self.points?.prefix(upTo:5)) as [AnyObject]
let angle = VectorCalculator.angleWithArrayOfPoints(lastFivePoints)
}
// You can also do this
let lastFivePoints = Array<CGPoint>(self.points?[0...4]) 发布于 2018-01-22 19:45:37
我尝试使用Array(lastFivePoints),但遇到错误
表达式的
类型在没有更多上下文的情况下不明确

我最终做了:
let arr = lastFivePoints.map({ (x) -> T in
return x
})其中T是本例中的内容类CGPoint
https://stackoverflow.com/questions/35028784
复制相似问题