본문 바로가기
Smart Device/Swift

Swift extension #6 Nested Types

by 언덕너머에 2015. 6. 25.

Nested Types

  1. extension Int {
  2. enum Kind {
  3. case Negative, Zero, Positive
  4. }
  5. var kind: Kind {
  6. switch self {
  7. case 0:
  8. return .Zero
  9. case let x where x > 0:
  10. return .Positive
  11. default:
  12. return .Negative
  13. }
  14. }
  15. }

  1. func printIntegerKinds(numbers: [Int]) {
  2. for number in numbers {
  3. switch number.kind {
  4. case .Negative:
  5. print("- ", appendNewline: false)
  6. case .Zero:
  7. print("0 ", appendNewline: false)
  8. case .Positive:
  9. print("+ ", appendNewline: false)
  10. }
  11. }
  12. print("")
  13. }
  14. printIntegerKinds([3, 19, -27, 0, -6, 0, 7])
  15. // prints "+ + - 0 - 0 +"