Lazy stored property

Lazy stored property

関モバ#4の発表資料です.
Lazy stored propertyについて話しました.

07236dd218acc51812c47f92cd6bba19?s=128

yashigani

July 27, 2015
Tweet

Transcript

  1. MB[ZTUPSFEQSPQFSUZ @yashigani ؔ੢ϞόΠϧΞϓϦݚڀձ#4

  2. yashigani id:yashigani_w @yashigani Mobile Application Engineer http://yashigani.hatenablog.com Hatena

  3. https://flic.kr/p/4L3Fz5 What is lazy stored property?

  4. • Swift has two types of properties • computed or

    stored • Indicate lazy stored property by lazy modifier • Lazy stored property created when it is first accessed
  5. class Lazy { lazy var person: String = { println("lazy

    evaluation!") return "someone" }() } let l = Lazy() let person = l.person // => lazy evaluation! println(person) // => someone
  6. ͳΜͷ໾ʹཱͭͷʁ

  7. lazy property͕ศརͳͱ͜Ζ • ΠϯελϯεͷॳظԽ͕׬ྃͨ͠ޙʹ֎෦͔Β஫ೖ ͞ΕΔσʔλʹґଘͨ͠propertyʹରͯ͠ • ॳظԽʹίετ͕͔͔Δ͕ɼ࢖͏͔Θ͔Βͳ͍

  8. class JSONDecoder { private var data: NSData private(set) lazy var

    JSONObject: [String: AnyObject] = { let data = self.data let JSONObject: AnyObject? = NSJSONSerialization.JSONObjectWithData(dat a, options: .AllowFragments, error: nil) return JSONObject as? [String: AnyObject] ?? [:] }() init(data: NSData) { self.data = data } }
  9. View ControllerͰେ׆༂ • storyboardͩͱ೚ҙͷΠχγϟϥΠβ͕࢖͑ͳ͍ • ΠχγϟϥΠβͰͳ͘prepareForSegueͰґଘ஫ೖ • Implicitly Unwrapped OptionalͳpropertyΛdidSet

    ͰॳظԽ͢ΔΑΓlazyʹͨ͠΄͏͕ศར • view͸஗Ԇϩʔυ͞ΕΔ
  10. // ͜ΕΑΓ final class ViewController:
 UIViewController { var user: User!

    var client: APIClient! var userManager: UserManager! { didSet { user = userManager.loginUser client = APIClient(user.session) } } }
  11. // ͬͪ͜ͷ΄͏͕͍͍ΑͶ final class ViewController:
 UIViewController { lazy var user:

    User = self.userManager.loginUser lazy var client: APIClient = APIClient(self.user.session) var userManager: UserManager! }
  12. import MBProgressHUD // MBProgressHUDͱ͔͸͜͏͓ͯ͘͠ͱศར final class ViewController: UIViewController { lazy

    var : MBProgressHUD = { var HUD = MBProgressHUD(view: self.view) self.view.addSubview(HUD) return HUD }() }
  13. ͭ·ΓMB[Z͸࠷ߴ

  14. ߋʹਐΜͩઃܭ΁

  15. class Lazy { var person: String = "someone" lazy var

    greeting: String = "Hello, lazy \(self.person)!" } let l = Lazy() l.person = "programmer" let person = l.person println(person) // => programmer println(l.greeting) // => Hello, lazy programmer
  16. ͜Ε͸ςετʹ࢖͑Δ • ΞϓϦέʔγϣϯ͕ґଘ͢ΔϦιʔεͳͲΛ Contextͱͯ͠·ͱΊΔ • Contextͷ΋ͭϦιʔεʹґଘ͢ΔpropertyΛ
 lazy stored propertyʹ͢Δ •

    ΞϓϦέʔγϣϯͰ͸ContextΛstaticʹऔಘ͢Δ ͕ɼςετͰ͸֎͔Βࠩ͠ସ͑
  17. // Application Context public class Context { public static let

    staticContext = Context() public var path: String { return "data.txt" } } // A View Controller final public class ViewController: UIViewController { public var context = Context.staticContext lazy public var dataStore: String = self.context.path }
  18. // test code func testViewController() { class MockContext: Context {

    override var path: String
 { return "test.txt" } } let vc = ViewController() // ॳظԽ͞ΕΔ·͑ʹcontextΛࠩ͠ସ͑Δ vc.context = MockContext() assert(vc.dataStore == "test.txt") }
  19. • όέπϦϨʔʹͳΓ͕ͪͳґଘͷड͚౉͠Λ
 γϯϓϧͳ࣮૷΁ͱঢ՚ • γϯϓϧͳ࣮૷Ͱ͋Δͱͱ΋ʹςετՄೳੑΛߴ͘ อͭ

  20. ·ͱΊ

  21. • lazy stored property͸ඞཁʹͳͬͨΒॳظԽ͞ΕΔ • جຊతʹ͍͍͔Μ͡ʹಈ͚͘Ͳɼςετ΋͠΍͍͢ Έ͍ͨͳͷΛ࡞ΕΔ • lazy࠷ߴʂ͍ͪ͹Μ޷͖ͳम০Ͱ͢

  22. 5PCF
 BMB[ZQSPHSBNNFS

  23. ͋Γ͕ͱ͏͍͟͝·ͨ͠