Last active
February 27, 2018 03:55
-
-
Save hmhmsh/be3a3da398be3272ab98d00d5a1967f0 to your computer and use it in GitHub Desktop.
2つのOptional型の値を場合分けして処理する書き方(場合分け時の処理は自分の環境での例です)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func logA(tag: String, param: [String: String]) { | |
print("\(tag) : \(param)") | |
} | |
func logB(tag: String) { | |
print("\(tag) : param なし") | |
} | |
func main0(param: [String: String]?, id: String?) { | |
if var param = param { | |
if let id = id { | |
param["b"] = id | |
} | |
logA(tag: "main0", param: param) | |
} else { | |
if let id = id { | |
let param = ["b": id] | |
logA(tag: "main0", param: param) | |
} else { | |
logB(tag: "main0") | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func main1(param: [String: String]?, id: String?) { | |
switch (param, id) { | |
case (var param?, let id): | |
param["b"] = id | |
logA(tag: "main1", param: param) | |
case (let param?, nil): | |
logA(tag: "main1", param: param) | |
case (nil, let id?): | |
let param = ["b": id] | |
logA(tag: "main1", param: param) | |
case (nil, nil): | |
logB(tag: "main1") | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func main2(param: [String: String]?, id: String?) { | |
if param == nil && id == nil { | |
logB(tag: "main2") | |
return | |
} | |
var param = param ?? [String: String]() | |
param["b"] = id | |
logA(tag: "main2", param: param) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment