ios development using swift - swift basics (1)

10
iOS Development using Swift SWIFT BASICS (1) Ahmed Ali

Upload: ahmed-ali

Post on 23-Jun-2015

402 views

Category:

Technology


2 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: iOS development using Swift - Swift Basics (1)

iOS Development using Swift

SWIFT BASICS (1)

Ahmed Ali

Page 2: iOS development using Swift - Swift Basics (1)

TODAY TOPICS

• What is and Why Swift?

• Swift: basic information.

• Variables

• Constants

• Swift type inference

• Quick demo

Ahmed Ali

Page 3: iOS development using Swift - Swift Basics (1)

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

Page 4: iOS development using Swift - Swift Basics (1)

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

Page 5: iOS development using Swift - Swift Basics (1)

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

Page 6: iOS development using Swift - Swift Basics (1)

VARIABLES (CONT)• Optional Binding:

if let myConst : Int = myVar{

print("myVar has the value \(myConst)")

}

• Implicitly unwrapped optional:

var myVar : Int!

Ahmed Ali

Page 7: iOS development using Swift - Swift Basics (1)

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

Page 8: iOS development using Swift - Swift Basics (1)

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

Page 9: iOS development using Swift - Swift Basics (1)

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

Page 10: iOS development using Swift - Swift Basics (1)

Demo Time

Ahmed Ali