From Objective-C to Swift

28
www.erlang-solutions.com ERLANG SOLUTIONS GET2GETHER TIGRE 2016

Transcript of From Objective-C to Swift

Page 1: From Objective-C to Swift

www.erlang-solutions.com

ERLANG SOLUTIONS GET2GETHER TIGRE 2016

Page 2: From Objective-C to Swift

www.erlang-solutions.com

FROM OBJECTIVE-C TO SWIFT

Page 3: From Objective-C to Swift

www.erlang-solutions.com

HELLO!

Pablo [email protected]@pablolvillarwww.erlang-solutions.com www.inaka.net

Page 4: From Objective-C to Swift

www.erlang-solutions.com

EXPERIENCES WE'VE LEARNEDFROM SOMESWIFTFEATURES

How has this new language affected our

developer lives?

Page 5: From Objective-C to Swift

www.erlang-solutions.com

1.SWIFT ISSTATICALLYTYPED

Page 6: From Objective-C to Swift

www.erlang-solutions.com

SWIFT IS STATICALLY TYPED

▸ Type checking at compile-time▸ We can catch mistakes at earlier stages▸ Less runtime crashes▸ Less magic, more knowledge

Page 7: From Objective-C to Swift

www.erlang-solutions.com

Swift (Static Types)

More restrictions when writing code.

It's harder to get along with the compiler and be good friends, but that's a good thing.

SWIFT IS STATICALLY TYPED

OBJ-C (Dynamic Types)

More freedom when writing code.

However, this magic comes with the high cost of experiencing undesired behavior or potential crashes at runtime.

Page 8: From Objective-C to Swift

www.erlang-solutions.com

2.SWIFT HAS GOTOPTIONALS

Page 9: From Objective-C to Swift

www.erlang-solutions.com

Swift

You need to define specific paths for null vs non-null scenarios.

You are not allowed to perform operations on a nil variable that's supposed not to be nil.

SWIFT HAS GOT OPTIONALS

OBJ-C

You can call a method with a pointer variable that is nil, and nothing will happen.

This no-op leads to unpredictable behavior.

Page 10: From Objective-C to Swift

www.erlang-solutions.com

SWIFT HAS GOT OPTIONALS

▸ We are forced to deal with null types▸ Our code becomes safer▸ We get used not to forget the error paths▸ We code with intention▸ Sometimes, we discover hidden scenarios that

haven't been defined in the specs yet.▹ In consequence, we become more proactive

in the development loop.

Page 11: From Objective-C to Swift

www.erlang-solutions.com

3.SWIFT TAKES AFUNCTIONALAPPROACH

Page 12: From Objective-C to Swift

www.erlang-solutions.com

SWIFT TAKES A FUNCTIONAL APPROACH

▸ Enums on steroids▸ Pattern matching▸ Functions as first-class citizens

▹ Perfect fit for completion closures▹ Higher order functions▹ Map, filter, reduce

▸ The importance of:▹ Immutability▹ Value types▹ Avoiding state

▸ Testable code as a consequence

Page 13: From Objective-C to Swift

www.erlang-solutions.com

4.SWIFT IS APROTOCOLORIENTEDPROGRAMMINGLANGUAGE

Page 14: From Objective-C to Swift

www.erlang-solutions.com

SWIFT IS A PROTOCOL ORIENTED PROGRAMMING LANGUAGE

▸ Protocols in Swift have been given more features and are more encouraged

▸ Favor composition over inheritance▸ Code is more extensible and flexible▸ In combination with generics, they become way

powerful

Page 15: From Objective-C to Swift

www.erlang-solutions.com

USING PLAIN INHERITANCE

Page 16: From Objective-C to Swift

www.erlang-solutions.com

USING PROTOCOL COMPOSITION

Page 17: From Objective-C to Swift

www.erlang-solutions.com

5.SWIFT HAS GOTGENERICS

Page 18: From Objective-C to Swift

www.erlang-solutions.com

SWIFT HAS GOT GENERICS

▸ With generics, we can solve general problems writing less code...

▸ This concept encourages us to:▹ Think of more layers of abstraction▹ Solve problems in a generic way▹ Find out common code architecture patterns in our

apps and generalize them▹ Write more open-source code

Page 19: From Objective-C to Swift

www.erlang-solutions.com

6.MODELSIN SWIFT ARE MOREPOWERFUL

Page 20: From Objective-C to Swift

www.erlang-solutions.com

MODELS IN SWIFT ARE MORE POWERFUL

Swift

You can define classes, structs and enums.

They all have the same power at their core.

OBJ-C

You can define classes, structs and enums.

While classes is the only one that has real power, structs and enums are vastly poor in comparison (they come from C).

Page 21: From Objective-C to Swift

www.erlang-solutions.com

▸ Structs and Enums have been leveled up▸ We started to use them much more▸ We have learned:

▹ The value of value types▹ How to avoid side effects▹ Pattern-matching

▹ Why the if clause can be avoided▹ The importance of immutability

MODELS IN SWIFT ARE MORE POWERFUL

Page 22: From Objective-C to Swift

www.erlang-solutions.com

7.SWIFT IS EASIER TOREAD

Page 23: From Objective-C to Swift

www.erlang-solutions.com

SWIFT IS EASIER TO READ, WRITE, MAINTAIN, ETC...

▸ Cleaner syntax▸ Better expressiveness▸ Much more friendly▸ More similar to modern languages▸ Encourages more devs to learn it

▹ Community is getting way bigger

bye-bye-brackets.png

Page 24: From Objective-C to Swift

www.erlang-solutions.com

8.SWIFT IS

OPENSOURCE

Page 25: From Objective-C to Swift

www.erlang-solutions.com

SWIFT IS OPEN SOURCE!

▸ Language is evolving around the community▹ Proposals are encouraged

▹ So are discussions▹ Much more documentation▹ More people getting involved

▸ Bring Swift to other platforms▹ Linux▹ Android▹ Windows

Page 26: From Objective-C to Swift

www.erlang-solutions.com

WRAPPING UP...

Page 27: From Objective-C to Swift

www.erlang-solutions.com

IN CONCLUSION

Swift has changed the way we program and how we think of programming.

We've got less error prone code and more solid codebases.

We are also learning from other paradigms, which makes us more flexible and open-minded.

Page 28: From Objective-C to Swift

www.erlang-solutions.com

THANK YOU!

Any [email protected]@pablolvillarwww.erlang-solutions.com www.inaka.net