//: Playground – noun: a place where people can play
// Swift EnumType (enum)
// Kyoung Shin Park (2016 Fall)
import Cocoa
import Foundation
// enum
enum Geometry : Int, CustomStringConvertible {
case Sphere = 1
case Cone
case Cylinder
case RectangularPrism
case SquarePyramid
case IsoscelesTriangularPrism
init(_ name: String)
{
switch name {
case “Sphere”, “구”, “1”: self = .Sphere
case “Cone”, “원뿔”, “2”: self = .Cone
case “Cylinder”, “원기둥”, “3”: self = .Cylinder
case “RectangularPrism”, “사각기둥”, “4”: self = .RectangularPrism
case “SquarePyramid”, “정사각뿔”, “5”: self = .SquarePyramid
case “IsoscelesTriangularPrism”, “이등변삼각기둥”, “6”: self = .IsoscelesTriangularPrism
default: self = .Sphere
}
}
var description: String {
switch self {
case .Sphere: return “Sphere”
case .Cone: return “Cone”
case .Cylinder: return “Cylinder”
case .RectangularPrism: return “RectangularPrism”
case .SquarePyramid: return “SquarePyramid”
case .IsoscelesTriangularPrism: return “IsoscelesTriangularPrism”
}
}
static func getGeometryBy(index : Int) -> Geometry? {
switch index {
case 1: return .Sphere
case 2: return .Cone
case 3: return .Cylinder
case 4: return .RectangularPrism
case 5: return .SquarePyramid
case 6: return .IsoscelesTriangularPrism
default: return nil
}
}
// use anyGenerator to get a generator that can enumerate across your values
static func enumerate() -> AnyGenerator<Geometry> {
var nextIndex = Sphere.rawValue
return anyGenerator { Geometry(rawValue: nextIndex++) }
}
}
for geo in Geometry.enumerate() {
print(geo.description)
}
var geo = Geometry(“1”)
print(geo.description)
geo = Geometry(“원뿔”)
print(geo.description)
geo = Geometry(“Cylinder”)
print(geo.description)
geo = Geometry.RectangularPrism
print(geo.description)
geo = .SquarePyramid
print(geo.description)
if let g = Geometry.getGeometryBy(6) {
print(g.description)
}