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
短期間での新規プロダクト開発における「コスパの良い」Goのテスト戦略」 / kamakura.go
n3xem
2
170
KubeCon + CloudNativeCon NA 2024 Overviewat Kubernetes Meetup Tokyo #68 / amsy810_k8sjp68
masayaaoyama
0
250
競技プログラミングへのお誘い@阪大BOOSTセミナー
kotamanegi
0
360
Security_for_introducing_eBPF
kentatada
0
110
fs2-io を試してたらバグを見つけて直した話
chencmd
0
230
tidymodelsによるtidyな生存時間解析 / Japan.R2024
dropout009
1
770
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
1
370
テストコード文化を0から作り、変化し続けた組織
kazatohiei
2
1.5k
Stackless и stackful? Корутины и асинхронность в Go
lamodatech
0
700
Zoneless Testing
rainerhahnekamp
0
120
創造的活動から切り拓く新たなキャリア 好きから始めてみる夜勤オペレーターからSREへの転身
yjszk
1
130
第5回日本眼科AI学会総会_AIコンテスト_3位解法
neilsaw
0
170
Featured
See All Featured
Designing for humans not robots
tammielis
250
25k
How To Stay Up To Date on Web Technology
chriscoyier
789
250k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
2
290
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
A better future with KSS
kneath
238
17k
Code Review Best Practice
trishagee
65
17k
Code Reviewing Like a Champion
maltzj
520
39k
Writing Fast Ruby
sferik
628
61k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
KATA
mclloyd
29
14k
A Philosophy of Restraint
colly
203
16k
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
͋Γ͕ͱ͏͍͟͝·ͨ͠