Last active
October 3, 2015 05:33
-
-
Save takuan-osho/6c0471607b7ed3b3f698 to your computer and use it in GitHub Desktop.
Optional Bindingみたいなのをするこういう場合、if letとguardどっちを使えばメンテしやすくなるんだろ? #CodePiece
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
| // Hacking with Swiftオリジナルのコード | |
| // from https://www.hackingwithswift.com/read/4/3/choosing-a-website-uialertcontroller | |
| func openPage(action: UIAlertAction!) { | |
| let url = NSURL(string: "https://" + action.title!)! | |
| webView.loadRequest(NSURLRequest(URL: url)) | |
| } | |
| // if letを使って書きかえてみたコード | |
| func openPage(action: UIAlertAction!){ | |
| if let title = action.title, let url = NSURL(string: "https://" + title) { | |
| webView.loadRequest(NSURLRequest(URL: url)) | |
| } | |
| } | |
| // guard使って書きかえてみたコード | |
| func openPage(action: UIAlertAction!) { | |
| guard let title = action.title else { | |
| return | |
| } | |
| guard let url = NSURL(string: "https://" + title) else { | |
| return | |
| } | |
| webView.loadRequest(NSURLRequest(URL: url)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment