Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Last active April 18, 2016 17:45
Show Gist options
  • Save KentarouKanno/46be498526551edc0971 to your computer and use it in GitHub Desktop.
Save KentarouKanno/46be498526551edc0971 to your computer and use it in GitHub Desktop.
if

if

let value = 10 

if value == 10 { 
    print("value = 10")
} 

★ if - let

let intValue: Int? = 5
var result: Int = 0

if let intValue = intValue {
    // intValue Unwrapped
    result = intValue + 10
}

------------------------------

let urlString = "https://sample.com/profile_images/image/imagephoto.png"

// url,data,image全てがOptinalで無い場合
if let url = NSURL(string: urlString), data = NSData(contentsOfURL: url), image = UIImage(data: data) {
    UIImageView(image: image)
}

------------------------------

let urlString = "https://sample.com/profile_images/image/imagephoto.png"

// where文の後に更にOptional Bindingをする場合
if let url = NSURL(string: urlString) where url.pathExtension == "png", let data = NSData(contentsOfURL: url), image = UIImage(data: data) {
    UIImageView(image: image)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment