Last active
November 12, 2015 03:34
-
-
Save el-hoshino/caa7befc0031872b0e51 to your computer and use it in GitHub Desktop.
switch 文で Optional Binding を使いたかった話 ref: http://qiita.com/lovee/items/7bd9aee01d7ccbf72d2b
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
let dict = [1: 2, 3: 4] | |
let key = 1 | |
if let value = dict[key] { | |
print(value) | |
} |
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
// このコードは間違いです。 | |
let dict = [1: 2, 3: 4] | |
let key = 1 | |
switch key { | |
case let value = dict[key]: | |
print(value) | |
default: | |
break | |
} |
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
let dict = [1: 2, 3: 4] | |
let key = 1 | |
switch dict[key] { | |
case .Some(let value): | |
print(value) | |
default: | |
break | |
} |
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
let dict = [1: 2, 3: 4] | |
let key = 1 | |
switch (key, dict[key]) { | |
case (let key, _) where key < 0: | |
print("Invalid") | |
case (_, let value?): | |
print(value) | |
default: | |
print("nil") | |
} |
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
let dict1 = [1: 2, 3: 4] | |
let dict2 = [5: 6, 7: 8] | |
let key = 5 | |
var value = 0 | |
switch key { | |
case let key where {() -> Int? in if let res = dict1[key] { value = res }; return dict1[key]}() != nil: | |
print(value) | |
case let key where {() -> Int? in if let res = dict2[key] { value = res }; return dict2[key]}() != nil: | |
print(value) | |
default: | |
print("nil") | |
} |
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
let dict1 = [1: 2, 3: 4] | |
let dict2 = [5: 6, 7: 8] | |
let key = 5 | |
var value = 0 | |
let getDictValueFor = {(key: Int, fromDict dict: [Int: Int], inout andWriteTo value: Int) -> Int? in | |
if let result = dict[key] { | |
value = result | |
} | |
return dict[key] | |
} | |
switch key { | |
case let key where getDictValueFor(key, fromDict: dict1, andWriteTo: &value) != nil: | |
print(value) | |
case let key where getDictValueFor(key, fromDict: dict2, andWriteTo: &value) != nil: | |
print(value) | |
default: | |
print("nil") | |
} |
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
let dict1 = [1: 2, 3: 4] | |
let dict2 = [5: 6, 7: 8] | |
let key = 5 | |
var value = 0 | |
let getDictValueFor = {(key: Int, fromDict dict: [Int: Int], inout andWriteTo value: Int) -> Int? in | |
if let result = dict[key] { | |
value = result | |
} | |
return dict[key] | |
} | |
switch key { | |
case let key where getDictValueFor(key, fromDict: dict1, andWriteTo: &value) != nil: | |
print(value) | |
case let key where getDictValueFor(key, fromDict: dict2, andWriteTo: &value) != nil: | |
print(value) | |
default: | |
print("nil") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment