10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SwiftのenumでWeekdayとか書いてみたメモ

Last updated at Posted at 2014-06-08

Swiftのenumには関数が定義できるようなので、ちょっと試してみた。

Weekday.swift
enum Weekday {
    case Sun, Mon, Tue, Wed, Thu, Fri, Sat
    
    func isHoliday() -> Bool {
        switch(self){
        case Sun, Sat:
            return true
        default:
            return false
        }
    }
    
    func isWorkday() -> Bool {
        return !self.isHoliday()
    }
}

var day : Weekday = .Sun
var p = day.isHoliday()
println(p) // -> true
p = day.isWorkday()
println(p) // -> false

day = Weekday.Mon // あえて、Weekday. をつけてみた。
p = day.isHoliday()
println(p) // -> false
p = day.isWorkday()
println(p) // -> true


println(Weekday.Sat.isHoliday()) // -> true
println(Weekday.Sat.isWorkday()) // -> false
println(Weekday.Wed.isHoliday()) // -> false
println(Weekday.Wed.isWorkday()) // -> true

// println(.Wed.isHoliday()) // <- こうは書けない。

結構おもしろい。

10
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?