As soon as I heard about the Swift programming language I got curious. I tried already to look into Objective-C but felt not quite ready yet. My programming knowledge developed now over about 2 years, with almost no prior knowledge. In the past 2 years I learned Ruby (on Rails) and of course all the things related to it. I think the time has come to dig into mobile engineering - what could be a better moment as the release of Swift.
The main resource of knowledge (and of course the only complete resource by now) is the book “The Swift Programming Language” from Apple and can be found for free in the iBook Store.
To run your code, you will need Xcode 6 beta. This version is for now only accessible with an enrollment in one of the Apple Developer programs (80€ a year).
Coming from Ruby
As explained above, I don’t have any knowledge of Objective-C, so this part is written only from a Ruby’s point of view.
Swift is a type safe language. That can actually save you some hassle, because you find bugs in your code in an early stage. That doesn’t mean necessarily that you always have to define the type of a variable or a constant. It works in two ways, first explicitly, second via type inference.
What is really important at this point: Both strings are immutable because of my usage of “let”. To define a mutable variable you explicitly have to define them by “var”. Once set, you can’t change the behavior anymore.
Another important fact coming from Ruby: Don’t mix 1 and 0 with true and false. Variables from type Boolean can’t be assigned a number!
I you define a mutable variable with “var” you really have to be aware, that the type is not changeable. That means as well, that the variable can’t be nil anymore!! Now the optionals become meaning.
If you think about, this is quite nice. If you have a type defined and it is no optional, you always get back a value. The above described explicit optionals are a nice way to make another behavior clear. If you don’t like all of this you can use optionals quite the same as variables in Ruby by just defining them as implicit optionals:
Not super interesting but Swift has the option to only raise an assertion with a specific condition in a nice syntactic way.
That’s it for now. The next story will be about collection types and the so called “control flow”.