Standford Swift course

Swift is a programming language that takes the good stuffs from all other languages. It’s imperative, it has a lot of functional programming features, and an object-oriented syntax.

People using Java, Python, functional languages like Scala or Haskell will find many of it’s functional stuffs very familiar. People shared opinion that it borrows heavily from Rust.

The functional programming goods that we can see are:

  1. Closures / functions as first class objects, that can be passed around as parameters and returned easily.
  2. Optional arguments
  3. Multiple return values
  4. Optional semicolons
  5. .. for fully inclusive range

Syntactically and aesthetically Swift has common things with Python, Ruby, Javascript, like:

  • readability
  • trailing closures (Ruby)
  • variable / constant declarations that often look dynamically typed (when they are actually using an inferred static type)

If you are a new person learning iOS development, Swift is an easy language to learn.

  • Type inference
  • Cleaner syntax
  • Generics & functional paradigms

However, most of tutorials and development problems (on stackoverflow) are in Objective-C, so there is clear advantage for a developer using Objective-C.

Create a view with frame and color (swift vs ojb-c):

Swift:

let view = UIView(frame: CGRectMake (0, 0, 200, 200) )

view.backgroundColor = UIColor.blueColor()

Objective-C:

UIView *view = [[UIView alloc] initWithFrame: CGRectMake (0, 0, 200, 200)];

view.backgroundColor = [UIColor blueColor];

A great (possibly coolest) feature of Swift is Generics.

Generics

Instead of writing swap2Ints, swap2Doubles, swap2Strings functions which are basically the same, we use <T> which is a placeholder for type, called Type parameter.

Type parameter is put after the function name, doesn’t have to be T but any valid identifier in <>

T can be used in parameters, return or inside the function body.

Not only generic types, we also have generic classes, structures and enum …

Advertisements