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
みんなでプロポーザルを書いてみた
yuriko1211
0
260
Creating a Free Video Ad Network on the Edge
mizoguchicoji
0
120
LLM生成文章の精度評価自動化とプロンプトチューニングの効率化について
layerx
PRO
2
190
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
1k
watsonx.ai Dojo #4 生成AIを使ったアプリ開発、応用編
oniak3ibm
PRO
1
110
Jakarta EE meets AI
ivargrimstad
0
550
色々なIaCツールを実際に触って比較してみる
iriikeita
0
330
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
5
2.1k
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
290
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
100
ヤプリ新卒SREの オンボーディング
masaki12
0
130
Outline View in SwiftUI
1024jp
1
330
Featured
See All Featured
Embracing the Ebb and Flow
colly
84
4.5k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
The Cost Of JavaScript in 2023
addyosmani
45
6.7k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
How to Ace a Technical Interview
jacobian
276
23k
Raft: Consensus for Rubyists
vanstee
136
6.6k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Automating Front-end Workflow
addyosmani
1366
200k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
370
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
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
͋Γ͕ͱ͏͍͟͝·ͨ͠