2017-01-16 63 views
-9

你好,我想知道.0和.1做了什麼,或者說在某個點常量y知道它是x和y軸,但是什麼是。 0和.1做什麼?.0和.1是什麼意思Swift 3.0.1

let somePoint = (1, 1) 
switch somePoint { 
case (0, 0): 
    print("(0, 0) is at the origin") 
case (_, 0): 
    print("(\(somePoint.0), 0) is on the x-axis") 
case (0, _): 
    print("(0, \(somePoint.1)) is on the y-axis") 
case (-2...2, -2...2): 
    print("(\(somePoint.0), \(somePoint.1)) is inside the box") 
default: 
    print("(\(somePoint.0), \(somePoint.1)) is outside of the box") 
} 
// Prints "(1, 1) is inside the box" 
+7

我會極力推薦閱讀[元組部分(https://developer.apple.com/library/content/documentation/Swift/Conceptual/ Swift_Programming_Language/TheBasics.html#// apple_ref/doc/uid/TP40014097-CH5-ID329)。 – Hamish

+0

您正在聲明一個(Int,Int)元組。它本質上與x或y沒有任何關係,比如調用let somePoint = CGPoint(x:1.0,y:1.0)。所以.0意味着元組的第一個或第零個元素,而.1意味着第二個元素。 –

+2

從語言指南:*或者,使用從零開始的索引號訪問元組中的單個元素值* – Keiwan

回答

2

somePoint.0.0正在訪問的第一個元素(索引0)的元組somePoint的。 .1正在訪問第二個元素(在索引1處)。

正如其他人所指出的,這是包括在first section of the language guide, "The Basics".

+0

非常感謝亞歷山大,這一切都讓現在就來。謝謝。 –

+0

您應該按順序閱讀語言指南。 – Alexander

+0

我正在閱讀它,但在解釋中沒有解釋,至少在控制流中尚未解釋。 –