ufcfx5-15-3mobile device development swift basics

17
UFCFX5-15-3 Mobile Device Development Swift Basics

Upload: buck-rodgers

Post on 15-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: UFCFX5-15-3Mobile Device Development Swift Basics

UFCFX5-15-3Mobile Device Development

Swift Basics

Page 2: UFCFX5-15-3Mobile Device Development Swift Basics

UFCFX5-15-3Mobile Device Development

Agenda

Swift in the Context of other Programming Languages Variable declaration, Typing and Assignment Program Output and Formatting Generating Random Numbers Data Structures: Arrays and Dictionaries Function Definitions Structure Declaration Class Declaration and Instantiation

Page 3: UFCFX5-15-3Mobile Device Development Swift Basics

UFCFX5-15-3Mobile Device Development

The Swift Programming Language

Variable declaration syntax has similarities with JavaScript and ActionScript

Swift features several programming constructs from other languages including ObjectiveC and Smalltalk

Swift borrows concepts from a number of programming paradigms, e.g. procedural and object-oriented programming and is therefore a multi-paradigm programming environment

Swift allows type inference e.g. loose typing Swift programming statements do not required line ending

characters such as semicolons

Page 4: UFCFX5-15-3Mobile Device Development Swift Basics

UFCFX5-15-3Mobile Device Development

Variable Declaration and Assignment

var aNumber // inferred (loose) typing - no statement ending i.e. ;

or

var aNumber:Int // uppercase I for Integer!

aNumber = 5 // variable assignment

Page 5: UFCFX5-15-3Mobile Device Development Swift Basics

UFCFX5-15-3Mobile Device Development

Constant Declaration

Constant declaration uses the let keyword

let PI = 3.141592654

Page 6: UFCFX5-15-3Mobile Device Development Swift Basics

UFCFX5-15-3Mobile Device Development

Outputting Comments using println

println ”Hello World”

Page 7: UFCFX5-15-3Mobile Device Development Swift Basics

UFCFX5-15-3Mobile Device Development

Value Output with Comments

var firstNumber:Intvar secondNumber:Intvar answer:Int

firstNumber = 5secondNumber = 14

answer = firstNumber + secondNumber

println("The answer is:\(answer)") // syntax for comment and output

Page 8: UFCFX5-15-3Mobile Device Development Swift Basics

UFCFX5-15-3Mobile Device Development

Random Number Generator

(arc4random_uniform(10))

Int(arc4random_uniform(10)) // integer

Page 9: UFCFX5-15-3Mobile Device Development Swift Basics

UFCFX5-15-3Mobile Device Development

Random Number Generation

var firstNumber:Intvar secondNumber:Intvar answer:Int

firstNumber = Int(arc4random_uniform(10))secondNumber = Int(arc4random_uniform(10))

answer = firstNumber + secondNumber

println("The answer is:\(answer)”)

Page 10: UFCFX5-15-3Mobile Device Development Swift Basics

UFCFX5-15-3Mobile Device Development

Swift Functions ‘func’

// function definitionfunc addTwoInts(numberOne: Int, numberTwo:Int)-> Int {

return numberOne + numberTwo

}

// function call with variables from earlier example

answer = addTwoInts(firstNumber, secondNumber)

println(“The sum of the two numbers is \(answer)”) println("The sum of \(firstNumber) + \(secondNumber) is:\(answer)")

Page 11: UFCFX5-15-3Mobile Device Development Swift Basics

UFCFX5-15-3Mobile Device Development

Array Declaration

var arrayOfIntegers:[Int] = [1,2,3,4,5,6,7,8,9,10]

// print out all the numbers in the arrayfor(number) in arrayOfIntegers{

println(number)

}

Page 12: UFCFX5-15-3Mobile Device Development Swift Basics

UFCFX5-15-3Mobile Device Development

Dictionaries (Keys and Values)

let interestingNumbers = [ "Prime":[2, 3, 5, 7, 11, 13], "Fiboncacci": [1, 1, 2, 3, 5, 8], "Square":[1, 4, 9, 16, 25], ] for(kind, numbers) in interestingNumbers{ println(kind, numbers)

}// output

(Square, [1, 4, 9, 16, 25])

(Fiboncacci, [1, 1, 2, 3, 5, 8])

(Prime, [2, 3, 5, 7, 11, 13])

Page 13: UFCFX5-15-3Mobile Device Development Swift Basics

UFCFX5-15-3Mobile Device Development

Swift Structures (struct)

struct Resolution {

var width = 1024 var height = 768

}

//Resolution is now a new Swift type

Page 14: UFCFX5-15-3Mobile Device Development Swift Basics

UFCFX5-15-3Mobile Device Development

Swift Class Definition

class VideoMode{

var resolution = Resolution()// struct type var interlaced = false var frameRate = 0.0 var name: String? //? Infers optional type

}//Resolution is used as type in the class definition

Page 15: UFCFX5-15-3Mobile Device Development Swift Basics

UFCFX5-15-3Mobile Device Development

Creating Instances of a Structure and Class

let aResolution = Resolution() // struct instance

let aVideoMode = VideoMode()// class instance

// accessing properties

println(“The width of aResolution is \(aResolution.width)”)

// prints “The width of aResolution is 1024”

Page 16: UFCFX5-15-3Mobile Device Development Swift Basics

UFCFX5-15-3Mobile Device Development

Setting Properties of a Class and Structure

// set the video mode resolution in the class struct

aVideoMode.resolution.width = 1280

println(“The width of aVideoMode is now \(aVideoMode.resolution.width )”)

// prints “The width of aVideoMode is now 1280”

Page 17: UFCFX5-15-3Mobile Device Development Swift Basics

UFCFX5-15-3Mobile Device Development

Summary

Swift is new programming language which draws syntax and structures from other well - established languages.

Swift uses inferred (loose) typing, its basic syntax is similar to JavaScript and ActionScript 3.0 Swift is only currently available for the OSX platform via Xcode 6.x There are currently at least two online portals which provide compilation of Swift program code The Apple Developer Portal provides language documentation and resources such as the Swift

Language and Cocoa with Swift as ebooks.