SWIFT Tuple Pattern及Struct Pattern

來源:互聯網
上載者:User

標籤:

定義一個Tuple

let color = (1.0,1.0,1.0,1.0)

switch color{

case (0.0,0.5...1.0,let blue,_): //匹配第一個值為0.0第二個值為0.5到1.0第三四值為任意,並將第三個值傳給blue變數

    println("Blue is \(blue)")

case let (r,g,b,1.0) where r == g && g == b: //額外條件where 本個亦是的值要相等

    println("Opaque grey \(r * 100)%")

default:

    println("unkonw")

}

元祖就說完了,接下來定義一個Struct

struct Color {

    var red:Double?

    var green:Double?

    var blue:Double?

    var alpha:Double?

    

    init(red:Double,green:Double,blue:Double,alpha:Double){

        self.red = red

        self.green = green

        self.blue = blue

        self.alpha = alpha

    }

}

 

var mycolor = Color(red: 10, green: 20, blue: 60, alpha: 1.0)

 

switch mycolor{

case let a where a.red == 10 && a.blue == 60:

    println("yes")

default:

    println("unkonw")

}

SWIFT Tuple Pattern及Struct Pattern

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.