UFCFX5-15-3Mobile Device Development Swift Basics.
-
Upload
buck-rodgers -
Category
Documents
-
view
224 -
download
0
Transcript of UFCFX5-15-3Mobile Device Development Swift Basics.
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
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
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
UFCFX5-15-3Mobile Device Development
Constant Declaration
Constant declaration uses the let keyword
let PI = 3.141592654
UFCFX5-15-3Mobile Device Development
Outputting Comments using println
println ”Hello World”
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
UFCFX5-15-3Mobile Device Development
Random Number Generator
(arc4random_uniform(10))
Int(arc4random_uniform(10)) // integer
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)”)
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)")
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)
}
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])
UFCFX5-15-3Mobile Device Development
Swift Structures (struct)
struct Resolution {
var width = 1024 var height = 768
}
//Resolution is now a new Swift type
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
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”
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”
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.