2016-01-27 28 views
7

私はこのエラーが発生しており、Swiftを初めて使用しています。私は> 5の配列の最後の5つの点を取得し、その5つの点を配列の引数として関数に渡したいと考えています。どうすればこの問題を解決し、このエラーを回避できますか?Swift Error: 'ArraySlice'タイプの値を期待される引数タイプに変換できません。

あなたが代わりに範囲演算子の方法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) 
} 
+3

'Array(yourArraySlice)'を試してください –

+1

[Swift 2.0からサブアレイを取得する方法](http://stackoverflow.com/questions/33060238/how-to-get-a-subarray-from-swift-2 -0) – Moritz

答えて

12

'[するCGPoint]' 期待される引数の型に型 'ArraySlice' の値を変換できません。あなたのコードを短くする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]) 
1

を使用してArrayArraySliceを変換する必要が

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

私はArray(lastFivePoints)を使用してみましたが、私は私がやってしまったエラー

Type of expression is ambiguous without more context

enter image description here

ました:

Tは、この例のコンテンツクラスするCGPointです
let arr = lastFivePoints.map({ (x) -> T in 
          return x 
         }) 

関連する問題