In Swift an Optional Type is considered a safe way to use variables. An Optional can represent a value that has been set or more importantly the lack of a set value as they default to nil when no value is present (yes that technically means they do always have a value). You tell Swift that you want to use an optional type by using ? after the variable type, so for example.

Read Full Article…Optionals

Closures – The Basics

You maybe familiar with Closures from other languages, simply put they are blocks of code assigned to a variable. In this example we create a closure called welcome take note of the syntax in this example, it takes no arguments but returns a string. Then we simply call it like a function, try this in a playground and see what you get back, it should be “Hello!”

Read Full Article…Closures – The Basics

Classes – The Basics

Classes in Swift are something you will be using a lot if you want to really get to grips with creating applications. I suggest that if you do not know what Object Oriented Programming methodology is that you at least read up on the basics before starting any serious development, there is a highly regarded book by Erich Gamma on the subject titled Design Patterns: Elements of Reusable Object-Oriented Software.

Classes in Swift are simple to create and use, this is I feel an improvement over Objective-C where at times it felt harder than it needed to be due to the syntax involved.

To follow along I suggest creating a playground so you can instantly see the results.

Read Full Article…Classes – The Basics

Functions – The Basics

( NOTE: Updated for Beta 3 syntax changes )

Functions are next up on the basics list, in a class you’ll see them called Methods. Think of functions as blocks of code (not to be confused with the confusing Blocks in previous iOS releases) that perform a task and either return something back or adjust something elsewhere in your program. Functions can be called with parameters that are used in the functions code. Often the function returns some meaningful data back from the call that can then be used for it’s desired purpose.

To put it another way, think of a function as being a dish washer and the dirty plates going in as the parameters. The dish washer does it’s thing and then returns back a clean set of plates ready to be used for something else. In a sudo code language it might be something like this.

Read Full Article…Functions – The Basics