2016-01-27 19 views
7

पर 'ArraySlice' प्रकार के मान को परिवर्तित नहीं कर सकता है मुझे यह त्रुटि मिल रही है और स्विफ्ट में नया हूं। मैं एक सरणी के पिछले 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) 
} 
+3

'ऐरे (yourArraySlice)' –

+1

का प्रयास करें [स्विफ्ट 2.0 से सबएरे कैसे प्राप्त करें] के संभावित डुप्लिकेट (http://stackoverflow.com/questions/33060238/how-to-get-a-subarray-from-swift-2 -0) – Moritz

उत्तर

12

आप विधि 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) 
} 
1

के बजाय सीमा ऑपरेटर का उपयोग Array को ArraySlice बदलने की आवश्यकता कनवर्ट नहीं कर सकता आप उपसर्ग (अप टू एंड: सेल्फ.इंडेक्स) विधि का उपयोग कर सकते हैं जो ऐरेस्लाइस लौटाता है जो आपके कोड को छोटा बनाता है। विधि की परिभाषा: विधि संग्रह की शुरुआत से बाद में निर्दिष्ट होती है, लेकिन निर्दिष्ट स्थिति (अनुक्रमणिका) सहित नहीं।

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]) 
0

मैं Array(lastFivePoints) उपयोग करने की कोशिश, लेकिन मैं त्रुटि

Type of expression is ambiguous without more context

enter image description here

मैं कर समाप्त हो गया है:

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

जहां टी इस उदाहरण

के लिए सामग्री वर्ग CGPoint है
संबंधित मुद्दे