3 ms·
Emerging Best Practices in Swift
- Camillo 11y agoThe second code example ("if you were doing this before ARC") is completely wrong. "alloc" is a class method that allocates memory for an instance, on which "init" is then called. Calling it on an instance returned by a factory method is nonsensical. The error is made consistently all three times, so it's clear that the author is genuinely confused about Objective-C. Maybe he's better at Swift, but it doesn't look good.
- thealistra 11y agoThe code samples were weird. Why injected property has to be optional, we can inject it using the constructor.
- Glide 11y agoI think the DI was for a view controller in this case. In normal usage I would agree. DI -> use the constructor. Even in languages like Java and C# I would advocate constructor injection. View controllers and where they get instantiated with storyboards, xibs, etc? Not so much.
- AshFurrow 11y agoYup.
- archagon 11y ago* Lazy init for non-unit-test use. * No need to have 20 parameters in your init method signature. * Imitates Cocoa standard practices re: delegates. * Swift's optional binding makes it safe if you don't force-unwrap objects (and makes your code more robust to boot).
- interpol_p 11y agoYeah, it looks like he meant to use `retain` rather than `alloc` (assuming he means to use them outside of the next autorelease pool drain).
- martind81 11y agoI stopped reading the article there. :)
- holyjaw 11y agoThe author is Ash Furrow, who has written numerous Obj-C books and apps. I highly doubt he's confused about Obj-C; he'll probably turn up in this thread at some point though, so it may be worth asking.
- AshFurrow 11y agoHa, yeah. Just a typo – it's been so long since I've had to write retain/release, and thank goodness!
- mjmsmith 11y agoWhy is this explicitly testing for false: if defaults.boolForKey("launchBefore") == false { rather than: if !defaults.boolForKey("launchBefore") {
- holyjaw 11y agoProbably a style thing. I think I recall reading about this in one of his Swift or Objective-C books? Can't quite remember. EDIT: Or maybe even just a hastily typed example code
- florian_buerger 11y agoI use this style myself. It is way easier to read than `!condition`. If you are scanning the code, a single `!` at the beginning of the condition won't stand out that much.
- andyfleming 11y agoUnrelated: Is there a plugin syncing speaker deck and the video? Or something custom?
- bdash 11y agoIts based on http://presentz.org http://presentz.org, which sadly appears to no longer be maintained.
- sinhpham 11y agoIs the currying example even currying? It looks like it's just a function returning a function to me. I thought currying it's some thing like func contains(subString: String, string: String) -> Bool, and calling contains("a") would automatically return a func(x: String) -> Bool.
- bdash 11y agoCurrying refers to the process of taking a function that accepts multiple arguments, and turning it into a sequence of functions, each accepting a single argument. The example you provide would be more aptly described as partial application, which is the process of fixing a number of arguments to a function, producing a function of smaller arity.
- bsaul 11y agoI'm a bit curious as to why people talk about "protocol oriented" as something new. It seems to me to be just a way of doing interface-oriented programming, like people coding in java have been doing for the last ten years, as well as golang dev, and many other languages... Am i missing a difference, or is it just not to confuse people that only develop in objective-c ?
- AshFurrow 11y agoYeah, sorry for the typos in the slides – I missed them while proofreading and unfortunately can't correct them now. I think the people in this thread discussing how my stylistic choices are "wrong" may have missed my point. Too bad.