Skip to content

Instantly share code, notes, and snippets.

@takuan-osho
Last active October 3, 2015 05:33
Show Gist options
  • Select an option

  • Save takuan-osho/6c0471607b7ed3b3f698 to your computer and use it in GitHub Desktop.

Select an option

Save takuan-osho/6c0471607b7ed3b3f698 to your computer and use it in GitHub Desktop.
Optional Bindingみたいなのをするこういう場合、if letとguardどっちを使えばメンテしやすくなるんだろ? #CodePiece
// 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