「ios開発」Swiftの列挙(Enumeration )の基本使い方

1.enumメソッド
enum SomeEumeration { // enumeration definition goes here }
enum CompassPoint { case North case South case East case West }
2.enum Planet { case Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Nepturn }

3.列挙値をマッチングとSwitch文法(Matching Enumeration Values with a Switch Statement)
コード下記:
directionToHead = .South switch directionToHead {
case .North:
println(“Lots of planets have a north")
case .South:
println(“Watch out for penguins")
case .East:
println(“Where the sun rises")
case .West:
println(“Where the skies are blue")
} // prints “Watch out for penguins”
4.商品のバーコードの列挙を定義
enum Barcode { case UPCA(Int, Int, Int) case QRCode(String)

任意商品のバーコードの列挙を定義
var productBarcode = Barcode.UPCA(8, 85909_51226, 3)

コード下記:

let positionToFind = 9
if let somePlanet = Planet.fromRaw(positionToFind) {
switch somePlanet {
case .Earth:
println(“Mostly harmless")
default:
println(“Not a safe place for humans")
}
} else
{
println(“There isn’t a planet at position \(positionToFind)")
} // prints “There isn’t a planet at position 9

IOS

Posted by arkgame