Learn iOS development using Swift (Arabic tutorials) - First session slides, which covers the following topics:
- What is and Why Swift?
- Swift: basic information.
- Variables
- Constants
- Swift type inference
And ends with a nice demo of Swift's Playground.
2. TODAY TOPICS
• What is and Why Swift?
• Swift: basic information.
• Variables
• Constants
• Swift type inference
• Quick demo
Ahmed Ali
3. WHAT IS AND WHY SWIFT
• Swift is the brand new language for developing iOS and Mac OS X applications.
• Supports many modern features.
• Strictly-typed language.
• Better compile time validation.
Ahmed Ali
4. BASICS
• No main method.
• The trailing semicolon after every statement is optional.
• The parentheses in if condition, for, while loops are optional.
• OOP and Procedural programming are fully supported.
Ahmed Ali
5. VARIABLES
• Defining variable syntax:
var myVar : Int = 0
• Allow nil, optional variables syntax:
var myVar : Int?
• You have to unwrap optional variables to access their values:
var nonOptionalVar : Int = myVar!
• Unwrapping variable who has no value (or its value is nil) will trigger a crash.
• Check if an optional has a value before trying to unwrap it:
if myVar != nil {
print("myVar equale to(myVar!)")
}
Ahmed Ali
6. VARIABLES (CONT)
• Optional Binding:
if let myConst : Int = myVar{
print("myVar has the value (myConst)")
}
• Implicitly unwrapped optional:
var myVar : Int!
Ahmed Ali
7. VARIABLES (CONT)
• Normal optional vs implicitly unwrapped optional:
• Behind the scene they are the same thing.
• Implicitly unwrapped is used whenever you are sure once it has a value, it’ll always
has a value.
• You can check implicitly unwrapped optional, or bind them in if conditions as you do
with normal optional.
Ahmed Ali
8. CONSTANTS
• Constants are preferred when a value will not change.
• Can be defined as optional, however, their value will not change.
• Definition syntax:
let constant : String = "Constant value"
Ahmed Ali
9. TYPE INFERENCE
• You don’t always have to write the type explicitly.
• Swift compiler is smart enough to try to get the right type from the current context.
let constant = "Constant value"
Ahmed Ali