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
sqlcを利用してsqlに型付けを
kamiyam
0
240
Subclassing, Composition, Python, and You
hynek
3
170
XP2024 っていう国際会議に行ってきたよの記
bonotake
4
240
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
10
1.5k
ACES Meet におけるリリース作業改善の取り組み
fukucheee
0
130
レイトレ合宿10 レンダラー紹介 / Renderer Introduction, Ray Tracing Camp 10
shocker_0x15
0
190
2024-10-02 dev2next - Application Observability like you've never heard before
jonatan_ivanov
0
180
pytest プラグインを開発して DRY に自動テストを書こう
inuatsu
2
260
4年間変わらなかった YOUTRUSTのアーキテクチャ
daiki1003
1
630
Kubernetes上でOracle_Databaseの運用を楽にするOraOperatorの紹介
nnaka2992
0
150
UnJSで簡単に始めるCLIツール開発 / cli-tool-development-with-unjs
aoseyuu
2
310
Compose Multiplatform과 Ktor로 플랫폼의 경계를 넘어보자
kwakeuijin
0
270
Featured
See All Featured
WebSockets: Embracing the real-time Web
robhawkes
59
7.3k
Practical Orchestrator
shlominoach
186
10k
Creatively Recalculating Your Daily Design Routine
revolveconf
217
12k
Bootstrapping a Software Product
garrettdimon
PRO
304
110k
Why Our Code Smells
bkeepers
PRO
334
57k
Learning to Love Humans: Emotional Interface Design
aarron
272
40k
BBQ
matthewcrist
85
9.2k
The Mythical Team-Month
searls
218
43k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
3
230
Fireside Chat
paigeccino
32
3k
Building Your Own Lightsaber
phodgson
102
6k
Side Projects
sachag
452
42k
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
͋Γ͕ͱ͏͍͟͝·ͨ͠