Skip to content

Instantly share code, notes, and snippets.

@SatoTakeshiX
Created December 3, 2016 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SatoTakeshiX/cca03214e26dd0541bf7c090dde0ddf7 to your computer and use it in GitHub Desktop.
Save SatoTakeshiX/cca03214e26dd0541bf7c090dde0ddf7 to your computer and use it in GitHub Desktop.
吉川さんにいい感じのSwiftの書き方を教えてもらった。(*´ω`*) #love_swift #CodePiece
//: Playground - noun: a place where people can play
import UIKit
//not good code
var photos: [UIImage] = {
var images = [UIImage]()
for index in 1...8 {
let photoName = "nature" + String(index) + ".jpg"
let image = UIImage.init(named: photoName) ?? UIImage()
images.append(image)
}
return images
}()
//better good code
var photos2: [UIImage] = {
return (1...8).map {
index -> UIImage in
let photoName = "nature" + String(index) + ".jpg"
let image = UIImage.init(named: photoName) ?? UIImage()
return image
}
}()
//very good code
var photos3 : [UIImage] = {
return (1...8).map {
UIImage.init(named: "nature" + String($0) + ".jpg")!
}
}()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment