简体   繁体   English

Swift错误:无法将“ArraySlice”类型的值转换为预期的参数类型

[英]Swift Error: Cannot convert value of type 'ArraySlice' to expected argument type

I'm getting this error and am new to Swift. 我收到了这个错误,并且是Swift的新手。 I want to take the last 5 points of an array >= 5, and pass those 5 points as an array argument to a function. 我想取一个数组> = 5的最后5个点,并将这5个点作为数组参数传递给函数。 How can I achieve this and get past this error? 我怎样才能实现这个并克服这个错误?

Cannot convert value of type 'ArraySlice' to expected argument type '[CGPoint]' 无法将'ArraySlice'类型的值转换为预期的参数类型'[CGPoint]'

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

You need to convert ArraySlice to Array using method Array(Slice<Type>) 您需要使用方法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)
}

Instead of the range operator you can use prefix(upTo end: Self.Index) method that return ArraySlice which makes your code shorter. 您可以使用返回ArraySlice的前缀(upTo end:Self.Index)方法代替范围运算符,这样可以缩短代码。 Method's definition: The method returns a subsequence from the start of the collection up to, but not including, the specified position (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]) 

I tried using Array(lastFivePoints) but i got error 我尝试使用Array(lastFivePoints)但我收到了错误

Type of expression is ambiguous without more context 如果没有更多的上下文,表达的类型是不明确的

在此输入图像描述

I ended up doing: 我最终做了:

let arr = lastFivePoints.map({ (x) -> T in
                            return x
                        })

where T is the content class CGPoint for this example 其中T是此示例的内容类CGPoint

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 swift:无法将类型“()”的值转换为预期的参数类型“ [Double]” - swift : Cannot convert value of type '()' to expected argument type '[Double]' 迅速。 错误:无法将“[AnyObject?]”类型的值转换为预期的参数类型“AnyObject?” - Swift. Error: Cannot convert value of type '[AnyObject?]' to expected argument type 'AnyObject?' Swift 按错误排序:无法转换“CGFloat”类型的值? 到预期的参数类型'Self' - Swift sorted by error: Cannot convert value of type 'CGFloat?' to expected argument type 'Self' Swift 错误无法转换“[String.Element]”类型的值(又名“Array<character> ') 到预期的参数类型 '[String]'</character> - Swift Error Cannot convert value of type '[String.Element]' (aka 'Array<Character>') to expected argument type '[String]' 错误:无法将类型“ [String]”的值转换为预期的参数类型“ String?” - Error: Cannot convert value of type “[String]” to expected argument type “String?” Swift:无法转换类型“(Any) throws -&gt; Bool”的值? 到预期的参数类型 &#39;(Any) throws -&gt; Bool? - Swift: Cannot convert value of type '(Any) throws -> Bool? to expected argument type '(Any) throws -> Bool? Swift:无法将&#39;String&#39;类型的值转换为预期的参数类型&#39;@noescape(AnyObject)引发-&gt; Bool&#39; - Swift: Cannot convert value of type 'String' to expected argument type '@noescape (AnyObject) throws -> Bool' IOS/Swift:如何在the.firstIndex中表示$0的第一个position? '无法将类型 'Any' 的值转换为预期的参数类型 'String'' - IOS/Swift: How to indicate the first position of the $0 in the .firstIndex ? 'Cannot convert value of type 'Any' to expected argument type 'String'' 无法将类型&#39;(NSDate)-&gt; NSTimeInterval的值转换为预期的参数类型&#39;Double&#39; - Cannot convert value of type '(NSDate) -> NSTimeInterval to expected argument type 'Double' 无法将类型“[ArrayImages]”的值转换为预期的参数类型“[UIImage?]” - Cannot convert value of type '[ArrayImages]' to expected argument type '[UIImage?]'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM