2014年08月12日
Posted by 屋台ブルー at
2014年08月12日00:00 Comment(2)
Swiftで遊ぼう! - 35 Swift Tourで戯れる
Swift Tour中にXcode6のβ5がリリースされ、待望のThe Swift Programming Languageも改訂されました! β5までの変更点が反映されているので、今まさに最新の内容になっているため、Tourも再び最初っから確認作業。
まだまだコントロールフローの確認中
let vegetable = "red pepper"
switch vegetable {
case "celery":
let vegetableComment = "Add some raisins and make ants on a log."
case "cucumber", "watercress":
let vegetableCommnet = "That would make a good tea sandwich."
case let x where x.hasSuffix("pepper"):
let vegetableComment = "Is it a spicy \(x)?"
default:
let vegetableComment = "Everything tastes good i soup."
}
Switchでコントロールするとき、その振る舞いをcase:で記述するが、caseを使う時、その条件をwhereで狭めることができる。
whereの説明で確認すると、
let yetAnotherPoin = (1, -1)
switch yetAnotherPoin {
case let (x, y) where x == y:
var message = "(\(x), \(y)) is on the line x == y"
case let (x, y) where x == -y:
var message = "(\(x), \(y)) is on the line x == -y"
case let (x, y):
var message = "(\(x), \(y)) is just some arbitrary point"
}
になっている。最後のcase let (x, y):の条件文で、すべてのx、yが含まれるため、default:の記述は必要ないと書かれている。
しかし、あえてdefault:を書き込んでみると、
let yetAnotherPoin = (1, -1)
switch yetAnotherPoin {
case let (x, y) where x == y:
var message = "(\(x), \(y)) is on the line x == y"
case let (x, y) where x == -y:
var message = "(\(x), \(y)) is on the line x == -y"
default:
var message = "(\(x), \(y)) is just some arbitrary point"←このエラーの説明は下
}
こではエラーが生じる... あーなんか嫌になってしまいそう。
-----2014年8月12日追記
やったー、コメントを頂けました! こういう小さな疑問でも解決すると嬉しいですね。ありがとうNaoさん、これからもよろしくお願い致します。
default:で設定したx, yの指定がdefaultに記述されていないのでエラーが生じたようです。納得です。
ということで、エラーを回避する記述は以下のようにパラメーター無しか、もとのパラメーターを使うかですね。
default:
var message = "(x, y) is just some arbitrary point"
default:
var message = "\(yetAnotherPoin)) is just some arbitrary point"
まだまだコントロールフローの確認中
let vegetable = "red pepper"
switch vegetable {
case "celery":
let vegetableComment = "Add some raisins and make ants on a log."
case "cucumber", "watercress":
let vegetableCommnet = "That would make a good tea sandwich."
case let x where x.hasSuffix("pepper"):
let vegetableComment = "Is it a spicy \(x)?"
default:
let vegetableComment = "Everything tastes good i soup."
}
Switchでコントロールするとき、その振る舞いをcase:で記述するが、caseを使う時、その条件をwhereで狭めることができる。
whereの説明で確認すると、
let yetAnotherPoin = (1, -1)
switch yetAnotherPoin {
case let (x, y) where x == y:
var message = "(\(x), \(y)) is on the line x == y"
case let (x, y) where x == -y:
var message = "(\(x), \(y)) is on the line x == -y"
case let (x, y):
var message = "(\(x), \(y)) is just some arbitrary point"
}
になっている。最後のcase let (x, y):の条件文で、すべてのx、yが含まれるため、default:の記述は必要ないと書かれている。
しかし、あえてdefault:を書き込んでみると、
let yetAnotherPoin = (1, -1)
switch yetAnotherPoin {
case let (x, y) where x == y:
var message = "(\(x), \(y)) is on the line x == y"
case let (x, y) where x == -y:
var message = "(\(x), \(y)) is on the line x == -y"
default:
var message = "(\(x), \(y)) is just some arbitrary point"←このエラーの説明は下
}
こではエラーが生じる... あーなんか嫌になってしまいそう。
-----2014年8月12日追記
やったー、コメントを頂けました! こういう小さな疑問でも解決すると嬉しいですね。ありがとうNaoさん、これからもよろしくお願い致します。
default:で設定したx, yの指定がdefaultに記述されていないのでエラーが生じたようです。納得です。
ということで、エラーを回避する記述は以下のようにパラメーター無しか、もとのパラメーターを使うかですね。
default:
var message = "(x, y) is just some arbitrary point"
default:
var message = "\(yetAnotherPoin)) is just some arbitrary point"
この記事へのコメント
case let (x, y) 〜には、switchで指定した変数をx,yに代入するという意味があります。
最後のdefaultではx,yが出てこないのに、その後のvar message=〜でx,yを使用しているのでエラーになるのではないでしょうか。
最後のdefaultではx,yが出てこないのに、その後のvar message=〜でx,yを使用しているのでエラーになるのではないでしょうか。
Posted by Na at 2014年08月12日 07:45
Nao?さん、コメントありがとうございます!
周りのプログラムの分かる人がいないので、本当に助かりました。私みたいな初心者だと小さなつまずきからなかなか立ち上がれないんですよ。
理解できました! 仰るようにdefaultで指定されていないパラメータ使うと駄目なんですね。
default:
var message = "(\(yetAnotherPoin)) is just some arbitrary point"
このように指定したやると問題なく動きました!
Naoさん、これからもコメントお願い致します!
ありがとうございました。
周りのプログラムの分かる人がいないので、本当に助かりました。私みたいな初心者だと小さなつまずきからなかなか立ち上がれないんですよ。
理解できました! 仰るようにdefaultで指定されていないパラメータ使うと駄目なんですね。
default:
var message = "(\(yetAnotherPoin)) is just some arbitrary point"
このように指定したやると問題なく動きました!
Naoさん、これからもコメントお願い致します!
ありがとうございました。
Posted by 屋台ブルー at 2014年08月12日 10:32