Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Lazy stored property
Search
yashigani
July 27, 2015
Programming
3
2.1k
Lazy stored property
関モバ#4の発表資料です.
Lazy stored propertyについて話しました.
yashigani
July 27, 2015
Tweet
Share
More Decks by yashigani
See All by yashigani
Hatena Engineer Seminar #10
yashigani
0
2.2k
Dependency Injection in iOS Apps
yashigani
2
460
Multiple layout with UITableViewCell
yashigani
1
3.4k
Code reviewing in iOS app development
yashigani
9
4.4k
Useful 3 techniques developing in iOS Apps
yashigani
0
1.5k
Introduction to WebKitPlus
yashigani
2
2.3k
Types in Swift
yashigani
5
2.3k
Swift with apps by Hatena
yashigani
13
29k
What's new WebKit.framework
yashigani
2
1.4k
Other Decks in Programming
See All in Programming
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
1k
Micro Frontends Unmasked Opportunities, Challenges, Alternatives
manfredsteyer
PRO
0
110
Less waste, more joy, and a lot more green: How Quarkus makes Java better
hollycummins
0
100
as(型アサーション)を書く前にできること
marokanatani
10
2.7k
Contemporary Test Cases
maaretp
0
140
Outline View in SwiftUI
1024jp
1
330
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
260
subpath importsで始めるモック生活
10tera
0
310
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
100
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
300
Featured
See All Featured
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
Ruby is Unlike a Banana
tanoku
97
11k
Done Done
chrislema
181
16k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Facilitating Awesome Meetings
lara
50
6.1k
RailsConf 2023
tenderlove
29
900
Being A Developer After 40
akosma
87
590k
We Have a Design System, Now What?
morganepeng
50
7.2k
The Pragmatic Product Professional
lauravandoore
31
6.3k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Art, The Web, and Tiny UX
lynnandtonic
297
20k
Transcript
MB[ZTUPSFEQSPQFSUZ @yashigani ؔϞόΠϧΞϓϦݚڀձ#4
yashigani id:yashigani_w @yashigani Mobile Application Engineer http://yashigani.hatenablog.com Hatena
https://flic.kr/p/4L3Fz5 What is lazy stored property?
• 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
class Lazy { lazy var person: String = { println("lazy
evaluation!") return "someone" }() } let l = Lazy() let person = l.person // => lazy evaluation! println(person) // => someone
ͳΜͷʹཱͭͷʁ
lazy property͕ศརͳͱ͜Ζ • ΠϯελϯεͷॳظԽ͕ྃͨ͠ޙʹ֎෦͔Βೖ ͞ΕΔσʔλʹґଘͨ͠propertyʹରͯ͠ • ॳظԽʹίετ͕͔͔Δ͕ɼ͏͔Θ͔Βͳ͍
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 } }
View ControllerͰେ׆༂ • storyboardͩͱҙͷΠχγϟϥΠβ͕͑ͳ͍ • ΠχγϟϥΠβͰͳ͘prepareForSegueͰґଘೖ • Implicitly Unwrapped OptionalͳpropertyΛdidSet
ͰॳظԽ͢ΔΑΓlazyʹͨ͠΄͏͕ศར • viewԆϩʔυ͞ΕΔ
// ͜ΕΑΓ final class ViewController: UIViewController { var user: User!
var client: APIClient! var userManager: UserManager! { didSet { user = userManager.loginUser client = APIClient(user.session) } } }
// ͬͪ͜ͷ΄͏͕͍͍ΑͶ final class ViewController: UIViewController { lazy var user:
User = self.userManager.loginUser lazy var client: APIClient = APIClient(self.user.session) var userManager: UserManager! }
import MBProgressHUD // MBProgressHUDͱ͔͜͏͓ͯ͘͠ͱศར final class ViewController: UIViewController { lazy
var : MBProgressHUD = { var HUD = MBProgressHUD(view: self.view) self.view.addSubview(HUD) return HUD }() }
ͭ·ΓMB[Z࠷ߴ
ߋʹਐΜͩઃܭ
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
͜Εςετʹ͑Δ • ΞϓϦέʔγϣϯ͕ґଘ͢ΔϦιʔεͳͲΛ Contextͱͯ͠·ͱΊΔ • ContextͷͭϦιʔεʹґଘ͢ΔpropertyΛ lazy stored propertyʹ͢Δ •
ΞϓϦέʔγϣϯͰContextΛstaticʹऔಘ͢Δ ͕ɼςετͰ֎͔Βࠩ͠ସ͑
// 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 }
// 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") }
• όέπϦϨʔʹͳΓ͕ͪͳґଘͷड͚͠Λ γϯϓϧͳ࣮ͱঢ՚ • γϯϓϧͳ࣮Ͱ͋ΔͱͱʹςετՄೳੑΛߴ͘ อͭ
·ͱΊ
• lazy stored propertyඞཁʹͳͬͨΒॳظԽ͞ΕΔ • جຊతʹ͍͍͔Μ͡ʹಈ͚͘Ͳɼςετ͍͢͠ Έ͍ͨͳͷΛ࡞ΕΔ • lazy࠷ߴʂ͍ͪΜ͖ͳम০Ͱ͢
5PCF BMB[ZQSPHSBNNFS
͋Γ͕ͱ͏͍͟͝·ͨ͠