Presentation is loading. Please wait.

Presentation is loading. Please wait.

UFCFX5-15-3Mobile Device Development Swift Basics.

Similar presentations


Presentation on theme: "UFCFX5-15-3Mobile Device Development Swift Basics."— Presentation transcript:

1 UFCFX5-15-3Mobile Device Development Swift Basics

2 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

3 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

4 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

5 UFCFX5-15-3Mobile Device Development Constant Declaration Constant declaration uses the let keyword let PI = 3.141592654

6 UFCFX5-15-3Mobile Device Development Outputting Comments using println println ”Hello World”

7 UFCFX5-15-3Mobile Device Development Value Output with Comments var firstNumber:Int var secondNumber:Int var answer:Int firstNumber = 5 secondNumber = 14 answer = firstNumber + secondNumber println("The answer is:\(answer)") // syntax for comment and output

8 UFCFX5-15-3Mobile Device Development Random Number Generator ( arc4random_uniform(10)) Int(arc4random_uniform(10)) // integer

9 UFCFX5-15-3Mobile Device Development Random Number Generation var firstNumber:Int var secondNumber:Int var answer:Int firstNumber = Int(arc4random_uniform(10)) secondNumber = Int(arc4random_uniform(10)) answer = firstNumber + secondNumber println("The answer is:\(answer)”)

10 UFCFX5-15-3Mobile Device Development Swift Functions ‘func’ // function definition func 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)")

11 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 array for(number) in arrayOfIntegers{ println(number) }

12 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])

13 UFCFX5-15-3Mobile Device Development Swift Structures (struct) struct Resolution { var width = 1024 var height = 768 } //Resolution is now a new Swift type

14 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

15 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”

16 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”

17 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.


Download ppt "UFCFX5-15-3Mobile Device Development Swift Basics."

Similar presentations


Ads by Google