Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Slides source code
footer: :copyright: akosma 2015
slidenumbers: true
<!-- theme: "Letters from Sweden" white -->
# [fit] Cocoa is the new [Carbon][carbon]
# [fit] The Future of Apple’s Beloved Framework
---
# [fit] Adrian Kosmaczewski
# [fit] [@akosma][akosma]
# [fit] [https://github.com/akosma][github]
---
> Everything changes and nothing stands still.
-- [Heraclitus of Ephesus][heraclitus], (535 - 475 BC)
---
> Πάντα χωρεῖ καὶ οὐδὲν μένει
-- [Ἡράκλειτος][heraclitus]
---
![fit 40%](http://www.moebius-bcn.com/wp-content/uploads/2012/03/1976-Apple-logo.jpg)
![fit 12%](http://4.bp.blogspot.com/-dN1EO9Sl4Y8/Tro4LyMZXzI/AAAAAAAAFlk/ME2L9Qt5Fi4/s1600/Apple+1976-1998.png)
![fit 76%](http://www.theinquirer.net/IMG/095/255095/apple-logo-.jpg)
![fit](https://gatonejo.files.wordpress.com/2012/12/apple-logo.png)
^ Apple loves change
---
![](http://upload.wikimedia.org/wikipedia/commons/6/68/Apple_II_Plus.jpg)
![](https://upload.wikimedia.org/wikipedia/commons/0/02/Macintosh_SE_b.jpg)
![](http://upload.wikimedia.org/wikipedia/commons/e/e0/Power_Macintosh_7220.jpg)
![](http://www4.pcmag.com/media/images/336347-apple-imac-27-inch-late-2013.jpg)
![fit](http://store.storeimages.cdn-apple.com/4118/as-images.apple.com/is/image/AppleInc/aos/published/images/s/ce/scene2/solo/scene2-solo-2x?wid=551&hei=1493&fmt=png-alpha&qlt=95&.v=1410264897500)
^ Look at the evolution in the hardware
---
# [fit] 6502 → 68000 → PowerPC → Intel → ARM
^ Now we have CPUs that could take you to Mars
---
# [fit] 8 bit → 16 bit → 32 bit → 64 bit
^ Pointer arithmetic FTW
---
# [fit] [Integer Basic][basic]
# [fit]→ [Object Pascal][pascal] & [MacApp][macapp]
# [fit]→ C++ & [Metrowerks PowerPlant][powerplant]
# [fit]→ [Objective-C][objc] & [Cocoa][cocoa]
# [fit]→ [Swift][swift] & [Standard Library][stdlib]
---
# [fit] Procedural → Object Oriented →
# [fit] Functional
---
> "Plus ça change, plus c'est la même chose."
-- [Jean-Baptiste Alphonse Karr][karr] (1808 - 1890)
^ But the marketing department always told us that these languages were the best...
---
# [fit] Heraclitus :collision: Jean-Baptiste
^ They never agreed.
---
# [fit] Transitional Technologies
^ Emulators, binary formats, etc.
---
![left fit](http://www.vectronicscollections.org/apple/images/powerbook165c/desk2.jpg)
# [Mac 68k Emulator][emu68k]
# 1992 - 2008 (!)
---
![left fit](http://web.archive.org/web/19980613012558/http://www.mae.apple.com/images/MAE3large.gif)
# [Macintosh Application Environment][macappenv]
# 1994 - 1998
^ Copland, Taligent, OpenDoc…
---
![left fit](http://regmedia.co.uk/2011/03/22/cheetah_12.jpg)
# [Classic Environment][classic]
# Deprecated in OS X 10.5 "Leopard"
# 2000 - 2008
---
# [Carbon][carbon]
# Deprecated in OS X 10.8 "Mountain Lion"
# 2000 - 2012
```c
#if __i386__
#include <Carbon/Carbon.h>
#else
#include <Carbon.h>
#endif
```
^ The Photoshop team never got the memo.
---
![](http://i1-news.softpedia-static.com/images/news-700/LLVM-3-1-Has-Support-for-ARM-Processors.png)
# [fit] The [LLVM][llvm] Compiler Infrastructure
# 2003 - now
^ Together with WebKit, a very important open source project
---
![left fit](https://upload.wikimedia.org/wikipedia/en/1/1b/Word_OSX_intel.jpg)
# [Rosetta][rosetta]
# Deprecated in OS X 10.7 "Lion"
# 2006 - 2011
^ Together with Universal Binaries, smoothed the transition to i386
---
![400%](https://upload.wikimedia.org/wikipedia/commons/d/de/Mac_Universal_logo.png)
# [fit] [Universal Binaries][universal]
---
# Objective-C 2.0 (1/2)
# 2006 - now
```objectivec
// Before
@implementation SomeClass
- (NSObject *)stuff {
return _stuff;
}
- (void)setStuff:(NSObject *)newStuff {
[newStuff retain];
[_stuff release];
_stuff = newStuff;
}
@end
```
^ Remember Accessorizer?
---
# Objective-C 2.0 (2/2)
# 2006 - now
```objectivec
// After
@interface SomeClass
@property (nonatomic, strong) NSObject *stuff;
@end
```
^ Can you say lightweight? Garbage collection, Properties, Fast enumeration, 64-bit capabilities
---
# LLVM
# Around 2009
# [fit] → Xcode Static Analyzer
# [fit] → Automatic Resource Counting (ARC)
^ The path to Swift was set.
---
# [Cocoa][cocoa]
# 1988 - now
# Deprecated in 5… 4… 3… :satisfied:
```objectivec
@import Cocoa;
```
---
# [fit] `nullable` Objective-C Pointers [^1] :alien:
```objectivec
@interface LocationDataController : NSObject
@property (nonnull, nonatomic) NSArray *locations;
@property (nullable, nonatomic) Location *latestLocation;
- (void)addPhoto:(nonnull Photo *)photo
forLocation:(nonnull Location *)location;
- (nullable Photo *)photoForLocation:(nonnull Location *)location;
@end
```
[^1]: ["Swift 1.2" by Nate Cook][swift12]
---
# [fit] Cocoa should remain for
# [fit] backwards compatibility during
# [fit] 10 years
# [fit] at least, until 2025!
^ We are back again in a transitional period.
---
![](images/objc.png)
^ The demise of ObjC will make some devs very happy
---
# [fit] What is Swift?
![right 100%](https://upload.wikimedia.org/wikipedia/en/4/43/Apple_Swift_Logo.png)
^ A great language to make strongly-typed Hello World apps.
---
# [fit] Modern
# [fit] Safe
# [fit] Fast
# [fit] Interoperable
^ Apple says "Objective-C without the C…"
---
![fit](images/csharphaskell.png)
![fit](images/haskell.png)
^ …which is not a good description.
---
# [fit] Features
---
# [fit] Protocols, [Pattern Matching][swift]
# [fit] Generics, Tuples, Optionals, Enums
# [fit] Strong Typing, Type Inference
# [fit] Structs, Functions
---
# [fit] OOP features of [Swift][swift] are based in
# [fit] Functional
# [fit] primitives and mechanisms!
---
# [fit] Inspiration
---
# [C#][csharp], [JavaScript][dollar], Haskell, Erlang, Ruby, C++, Java, [Scala][scala]
^ LINQ, jQuery + Node.js, anything but the syntax, DSL, Boost + Alexandrescu, Scala syntax
---
![](images/eclipse.png)
^ Let's not repeat the mistakes of the Java era
---
# [fit] How would the successor of [Cocoa][cocoa]
# [fit] look like?
---
# [fit] Gedankenexperiment :thought_balloon:
---
![](images/playgrounds.png)
^ Playgrounds FTW
---
# [Swift Standard Library][stdlib]
```swift
let array = [10, 200, 3000]
for (pos, value) in enumerate(array) {
println("\(pos): \(value)")
}
// 0: 10
// 1: 200
// 2: 3000
```
^ Can't help but thinking it is somehow incomplete, yet the embryo of the future Swift framework! You know a Swift developer has issues when you spot a static class method somewhere during a code review. AND WE NEED MORE DOCS!
---
# [LINQ][swift] (1/2)
```csharp
public void linq()
{
List<Product> products = GetProductList();
var expensive =
from p in products
where p.Stock > 0 && p.Price > 1000
select p;
foreach (var product in expensive)
{
Console.WriteLine(product.Name);
}
}
```
---
# [LINQ][swift] (2/2)
```swift
func linq(){
let products = productsList()
let expensive = products
.find { p in p.stock > 0 && p.price > 1000 }
for p in expensive {
println(p.name)
}
}
```
^ Syntax sugar, anyone? NSPredicate + strong typing.
---
![fit](images/xcode.png)
^ We could borrow some ideas from Visual Studio.NET, actually.
---
# [Dollar.swift][dollar]
```swift
let double = { (params: Int...) -> [Int] in
return $.map(params) { $0 * 2 }
}
let subtractTen = { (params: Int...) -> [Int] in
return $.map(params) { $0 - 10 }
}
let doubleSubtractTen = $.compose(double, subtractTen)
doubleSubtractTen(5, 6, 7)
```
```
=> [0, 2, 4]
```
^ JS and Swift have some structural similarities but radically different operational environments
---
# [Alamofire][alamofire]
```swift
let user = "user"
let password = "password"
Alamofire.request(.GET, "http://somewhere.over/the.rainbow")
.authenticate(user: user, password: password)
.validate(statusCode: 200..<300)
.responseString { (_, _, string, _) in
println(string)
}
.responseJSON { (_, _, JSON, _) in
println(JSON)
}
```
^ By default, Alamofire treats any completed request to be successful, regardless of the content of the response. Calling validate before a response handler causes an error to be generated if the response had an unacceptable status code.
---
# [Notification Center][notif] (1/3)
```swift
struct Notification<A> {
let name: String
}
func postNotification<A>(note: Notification<A>, value: A) {
let userInfo = ["value": Box(value)]
let center = NSNotificationCenter.defaultCenter()
center.postNotificationName(note.name,
object: nil,
userInfo: userInfo)
}
```
^ Example by Chris Eidhof from objc.io. We wrap the notification and its associated userInfo value into a Swift struct, and then dispatch it as usual. The Box struct allows us do something that Swift generics do not allow us to do yet.
---
# [Notification Center][notif] (2/3)
```swift
class NotificationObserver {
let observer: NSObjectProtocol
init(notification: Notification<A>, block aBlock: A -> ()) {
let center = NSNotificationCenter.defaultCenter()
observer = center.addObserverForName(notification.name,
object: nil,
queue: nil) { note in
if let value = (note.userInfo?["value"] as? Box<A>)?.unbox {
aBlock(value)
}
}
}
deinit { … }
}
```
^ The `deinit` is required to get rid of the reference to the observer
---
# [Notification Center][notif] (3/3)
```swift
let notif: Notification<NSError> = Notification(name: "Global panic")
let myError: NSError = NSError(domain: "io.objc.sample",
code: 42,
userInfo: [:])
postNotification(notif, myError)
let observer = NotificationObserver(notification: notif) { err in
println(err.localizedDescription)
}
```
^ And here we use the new infrastructure. The whole Notification Center is hidden from view.
---
# [Events][events] (1/2)
```swift
class Event<T> {
typealias EventHandler = T -> ()
private var eventHandlers = [EventHandler]()
func addHandler(handler: EventHandler) {
eventHandlers.append(handler)
}
func raise(data: T) {
for handler in eventHandlers {
handler(data)
}
}
}
```
^ What is an event? Can we get rid of target-action? Here an event is simply defined as a wrapper around a generic function.
---
# [Events][events] (2/2)
```swift
let event = Event<Void>()
event.addHandler { println("Hello") }
event.addHandler { println("World") }
event.raise()
// What?
let event = Event<(String, String)>()
event.addHandler { a, b in println("Hello \(a), \(b)") }
event.raise("First", "Second")
```
^ Swift treats a function with multiple parameters just the same as one with a single tuple parameter. As a result, you do not need to construct a tuple in order to raise an event with two string parameters.
---
# [SwiftyUserDefaults][defaults]
```swift
Defaults["color"] = "red"
Defaults["launchCount"] = 0
Defaults["color"] ?= "white"
Defaults["firstLaunchAt"].date // returns NSDate?
Defaults["launchCount"] += 1
if !Defaults.hasKey("hotkey") {
Defaults.remove("hotkeyOptions")
}
```
^ A very simple API around a key-value store.
---
# [SwiftMoment][moment]
```swift
let today = moment()
let before = moment("2015-01-19")
let earlier = before < today // true
let soon = today + 1.hours
let duration = 5.hours + 56.minutes
let format = "EE yyyy/dd MMMM HH:mm"
let birthday = moment("Tue 1973/4 September 12:30", format)!
let str = birthday.format(dateFormat: "EE QQQQ yyyy/dd/MMMM")
// "Tue 3rd quarter 1973/04/September"
```
^ My baby. Not yet ready for production but moving into that direction.
---
# [fit] Are we there yet?
# [fit] Hint: **no**.
^ We are barely starting.
---
# [fit] Protocols, [Pattern Matching][swift]
# [fit] Generics, Tuples, Optionals, Enums
# [fit] Strong Typing, Type Inference
# [fit] Structs, Functions
---
# [fit] Functions and structs
# [fit] instead of objects and classes
^ We are moving towards a functional world… so listen to Junior!
---
# [fit] Not all is OK
# [fit] What about reflection? Documentation? UI?
^ We don't live in a perfect world.
---
# [fit] Time will tell :clock10:
---
# [fit] Thanks! :clap:
---
# [fit] Adrian Kosmaczewski
# [fit] [@akosma](https://twitter.com/akosma)
# [fit] [https://github.com/akosma](https://github.com/akosma)
---
# [fit] Special thanks to
# [fit] Junior Bontognali [@bontojr][bontojr] for the organisation
# [fit] Fabrice Truillot de Chambrier [@fabricetdc][fabricetdc]
# [fit] And the great people I follow on Twitter
---
# [fit] Slides created with [Deckset][deckset]
[akosma]:https://twitter.com/akosma
[alamofire]:https://github.com/Alamofire/Alamofire
[basic]:https://en.wikipedia.org/wiki/Integer_BASIC
[bontojr]:https://twitter.com/bontojr
[carbon]:https://en.wikipedia.org/wiki/Carbon_(API)
[classic]:https://en.wikipedia.org/wiki/Classic_Environment
[cocoa]:https://developer.apple.com/technologies/mac/cocoa.html
[csharp]:https://github.com/mythz/swift-linq-examples
[deckset]:http://www.decksetapp.com
[defaults]:https://github.com/radex/SwiftyUserDefaults
[dollar]:https://github.com/ankurp/Dollar.swift
[emu68k]:https://en.wikipedia.org/wiki/Mac_68k_emulator
[events]:http://www.scottlogic.com/blog/2015/02/05/swift-events.html
[fabricetdc]:https://twitter.com/fabricetdc
[github]:https://github.com/akosma
[heraclitus]:https://en.wikiquote.org/wiki/Heraclitus
[karr]:https://en.wikipedia.org/wiki/Jean-Baptiste_Alphonse_Karr
[llvm]:http://llvm.org
[macapp]:https://en.wikipedia.org/wiki/MacApp
[macappenv]:https://en.wikipedia.org/wiki/Macintosh_Application_Environment
[moment]:https://github.com/akosma/SwiftMoment
[notif]:http://www.objc.io/snippets/16.html
[objc]:https://en.wikipedia.org/wiki/Objective-C
[pascal]:https://en.wikipedia.org/wiki/Object_Pascal#Early_history_at_Apple
[powerplant]:https://en.wikipedia.org/wiki/PowerPlant
[pythonic]:https://github.com/practicalswift/Pythonic.swift
[rosetta]:https://en.wikipedia.org/wiki/Rosetta_(software)
[scala]:https://leverich.github.io/swiftislikescala/
[stdlib]:http://swiftdoc.org
[swift]:https://developer.apple.com/swift/
[swift12]:http://nshipster.com/swift-1.2/
[universal]:https://en.wikipedia.org/wiki/Universal_binary
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.