Learning Swift and the Best of Both Worlds play

Well, having suffered through Objective C and liked XCode compiler, it has sure been interesting to see what Apple has come up next with Swift. It’s the brain child of Chris Lattner. He’s had a pretty incredible career, developing LLVM (a compiler backend) and then Clang (a front end) and finally XCode that’s been used for Apple’s development tools.

The Swift uses LLVM and runs in the same environment as Objective C, but has scripting like constructs built in as well as being strongly typed, so it is in the middle ground between scripting simplicity and the power of C. The iBook is actually a pretty good description of everything that it can do with native support for sequences, lists, ranges, it is pretty neat. If we were doing a marketingplaybook.com essay, it would be about how Swift is the best of compiled and interpreted languages:

  • It has the really nice abbreviated syntax of a Python but has the strict type checking of Objective C (sometime even more so)
  • It has the power of classes and because it uses the LLVM backend, you can mix and match it with Objective C

And Playgrounds let’s you make changes and insert code and see what is happening on the right pane. Pretty cool because Swift incrementally compiles so it really is the best of both worlds.

Others are doing the same thing with Google Go and Facebook Hack, so there’s so much more for programmers to learn. How fun is that!

Check this out for coolness where a data structure as a series of sequences and dictionaries and this looks at all the numbers in the sequences to find the largest number:


let​ ​interestingNumbers​ = [
​ ​"Prime"​: [​2​, ​3​, ​5​, ​7​, ​11​, ​13​],
​ ​"Fibonacci"​: [​1​, ​1​, ​2​, ​3​, ​5​, ​8​],
​ ​"Square"​: [​1​, ​4​, ​9​, ​16​, ​25​],
​]
​var​ ​largest​ = ​0
​for​ (​kind​, ​numbers​) ​in​ ​interestingNumbers​ {
​ ​for​ ​number​ ​in​ ​numbers​ {
​ ​if​ ​number​ > ​largest​ {
​ ​largest​ = ​number
​ }
​ }
​}

​largest

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/us/jEUH0.l

 

This thing definitely has it’s subtleties, for instance the use of question mark to say that the variable may have a legitmate value or a nil is pretty cool but different syntax. Touch-magazine explains it pretty well though.

First there is this cool idea of a data type type that applies to all types, so this means you can make any type Optional:


enum Optional : NilLiteralConvertible {
case None
case Some(T)
}

This interesting syntax means that there are two cases, with a nil for case and then Some(T) means just return the type. So it allows something like `OptionalInt` which says it can be a nil or an integer value. So that `Int?` just maps to `Optional<Int>` 

There is a different use of the question mark, where it is syntactic sugar that says if the object is a nil then don’t execute it.  Finally, there is the ability to cast things as needed, so there is both “upcasting” and “downcasting”, so you there is this idiom that says takes something and cast it down to its more basic component. For instance, if a string is an object, then the “if let” gives you a nice temporary constant to refer to it as that “downcast” item.

“`

var str : String

if let object=str is NSObject {

   println(“I’m an object at my core”)

}

“`

Similarly you can upcast something, so for instance with the `as?` it means that if you can see if it is the higher level object or through a forced conversion

“`

var floatValue : Float

if let intValue=floatValue as? Int {

  println(“I can treat this object \(floatValue) as a integer \(intValue)by a forced cast which cuts off the decimal part for now”)

}

“`