Hello Swift
What is Swift?
Swift is a new Apple open-source language used to develop iOS Apps. It is open source and available now on GitHub; replacing Objective-C as the main language for the iOS and OSX platforms.
The advantages of Swift over Objective-C are:
- Faster - Swift apps compile faster then their obj-c counterparts.
- Safe - Safe - Swift focuses a lot on ‘let’ and ‘var’ which makes the code safe by avoiding any unintended overwrite of values.
- Strong - Swift supports generic, enums and a lot of new features which makes it a better language to develop in.
First Project
Its time to dive right into exploring swift. Lets get started
Open Xcode and create a new playground
We will be using the playground to learn basic swift before progressing on to make an app.
Go Ahead create a new playground from the File->New option.
First screen shall look like this
Replace the contents with the following
let and var
Swift focuses majorly on let and var variables.
variables defined using let are constant and cannot be changed once a value is initalized to them while the var can be changed later on.
This introduces a focus on making the code secure with correct usage of constant variable from the start, therefore no bugs later on.
Array
Array are defined using the square brackets []. Here we pass few values to initalize it. Each value can be accessed using its index.
Dictionary
Dictionary is a key value object and is used when the data has to be fetched key based.
Common example is for a user, you dont want to have user name at 0 index, birthday at 1. This is problematic and confusing.
What you want is username key to have the name value and user_birthday to have the birthday value. Something like :
Go Ahead and check the playground to see all this live. We have added comments there for you to understand along.
Conclusion
Thank you for reading this far. For any questions you are welcome to ask in our chat room.
Next Steps
Please take a look at our other tutorials :)