8
8

More than 5 years have passed since last update.

swiftのswitch文で型で分岐する書き方

Last updated at Posted at 2015-11-25

追記

こちらの書き方が可能のようです。
こっちで書きましょう
https://developers.eure.jp/event/10-tips-when-moving-from-objective-c-to-swift-ja/?utm_source=dlvr.it&utm_medium=twitter

switch文

switch someValue {
case _ as Int:
    Log.d(nil, "This is Int")
case _ as String:
    Log.d(nil, "This is String")
case _ as Hoge:
    Log.d(nil, "This is Hoge")
default:
    // unknown case
    break
}

if文で同じ処理

if someValue is Int {
    Log.d(nil, "This is Int")
}else if someValue is String {
    Log.d(nil, "This is String")
}else if someValue is Hoge {
    Log.d(nil, "This is Hoge")
}else {
    // unknown case
}
8
8
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
8
8