From Objective-C to Swift

Post on 11-Feb-2017

82 views 0 download

Transcript of From Objective-C to Swift

www.erlang-solutions.com

ERLANG SOLUTIONS GET2GETHER TIGRE 2016

www.erlang-solutions.com

FROM OBJECTIVE-C TO SWIFT

www.erlang-solutions.com

HELLO!

Pablo Villarpablo@inakanetworks.com@pablolvillarwww.erlang-solutions.com www.inaka.net

www.erlang-solutions.com

EXPERIENCES WE'VE LEARNEDFROM SOMESWIFTFEATURES

How has this new language affected our

developer lives?

www.erlang-solutions.com

1.SWIFT ISSTATICALLYTYPED

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

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.

www.erlang-solutions.com

2.SWIFT HAS GOTOPTIONALS

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.

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.

www.erlang-solutions.com

3.SWIFT TAKES AFUNCTIONALAPPROACH

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

www.erlang-solutions.com

4.SWIFT IS APROTOCOLORIENTEDPROGRAMMINGLANGUAGE

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

www.erlang-solutions.com

USING PLAIN INHERITANCE

www.erlang-solutions.com

USING PROTOCOL COMPOSITION

www.erlang-solutions.com

5.SWIFT HAS GOTGENERICS

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

www.erlang-solutions.com

6.MODELSIN SWIFT ARE MOREPOWERFUL

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).

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

www.erlang-solutions.com

7.SWIFT IS EASIER TOREAD

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

www.erlang-solutions.com

8.SWIFT IS

OPENSOURCE

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

www.erlang-solutions.com

WRAPPING UP...

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.

www.erlang-solutions.com

THANK YOU!

Any questions?pablo@inakanetworks.com@pablolvillarwww.erlang-solutions.com www.inaka.net