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
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
9
1k
のびしろを広げる巻き込まれ力:偶然を活かすキャリアの作り方/oso2024
takahashiikki
1
410
PagerDuty を軸にした On-Call 構築と運用課題の解決 / PagerDuty Japan Community Meetup 4
horimislime
1
110
飲食業界向けマルチプロダクトを実現させる開発体制とリアルな現状
hiroya0601
1
390
プロジェクト新規参入者のリードタイム短縮の観点から見る、品質の高いコードとアーキテクチャを保つメリット
d_endo
1
1k
2万ページのSSG運用における工夫と注意点 / Vue Fes Japan 2024
chinen
3
1.3k
僕がつくった48個のWebサービス達
yusukebe
18
17k
Dev ContainersとGitHub Codespacesの素敵な関係
ymd65536
1
130
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
210
推し活の ハイトラフィックに立ち向かう Railsとアーキテクチャ - Kaigi on Rails 2024
falcon8823
6
2.2k
詳細解説! ArrayListの仕組みと実装
yujisoftware
0
480
破壊せよ!データ破壊駆動で考えるドメインモデリング / data-destroy-driven
minodriven
16
4k
Featured
See All Featured
Rails Girls Zürich Keynote
gr2m
93
13k
RailsConf 2023
tenderlove
29
880
How GitHub (no longer) Works
holman
311
140k
Mobile First: as difficult as doing things right
swwweet
222
8.9k
The Language of Interfaces
destraynor
154
24k
YesSQL, Process and Tooling at Scale
rocio
167
14k
Into the Great Unknown - MozCon
thekraken
31
1.5k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Side Projects
sachag
452
42k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
Building Adaptive Systems
keathley
38
2.2k
Typedesign – Prime Four
hannesfritz
39
2.4k
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
͋Γ͕ͱ͏͍͟͝·ͨ͠