Last active
July 24, 2017 16:18
-
-
Save creaaa/091bf9e365a1ef3023326a7440eac592 to your computer and use it in GitHub Desktop.
挙動1: クロージャ(正しくはブロック)のreturnのスコープに注意(例: forEachの引数に渡すクロージャ内でのreturn等) 挙動2: これはもっとやばいがreturn直後の1行は実行される
This file contains 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
(1...3).forEach { | |
print($0) | |
return | |
print("ここもでるwww") | |
print("ここはでないwww") | |
} | |
/* | |
Result: | |
1 | |
ここもでるwww | |
2 | |
ここもでるwww | |
3 | |
ここもでるwww | |
*/ | |
// これがguard letとかもからむと厄介。guard let内のreturnも"ブロック"なので同じ挙動をおこすのだ: | |
(1...3).forEach { | |
guard let data = self.getHtml(url: url) else { | |
return | |
} | |
} | |
↑ これ、dataがnilでも、1周ではなく3周するので注意 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment