Sie sind auf Seite 1von 20

Swift Basics

Mobile Device Development

UFCFX5-15-3

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

Mobile Device Development

UFCFX5-15-3

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

Mobile Device Development

UFCFX5-15-3

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

Mobile Device Development

UFCFX5-15-3

Constant Declaration

Constant declaration uses the let keyword

let PI = 3.141592654

Mobile Device Development

UFCFX5-15-3

Outputting Comments using println

println Hello World

Mobile Device Development

UFCFX5-15-3

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

Mobile Device Development

UFCFX5-15-3

Random Number Generator

(arc4random_uniform(10))

Int(arc4random_uniform(10)) // integer

Mobile Device Development

UFCFX5-15-3

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

Mobile Device Development

UFCFX5-15-3

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)")

Mobile Device Development

UFCFX5-15-3

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)
}

Mobile Device Development

UFCFX5-15-3

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

Mobile Device Development

UFCFX5-15-3

Swift Structures (struct)

struct Resolution {
var width = 1024
var height = 768
}
//Resolution is now a new Swift type

Mobile Device Development

UFCFX5-15-3

Swift Class Definition

class VideoMode{
var
var
var
var

resolution = Resolution()// struct type


interlaced = false
frameRate = 0.0
name: String? //? Infers optional type

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

Mobile Device Development

UFCFX5-15-3

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

Mobile Device Development

UFCFX5-15-3

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

Mobile Device Development

UFCFX5-15-3

The Swift 2 Main Additions

Error handling model e.g..

func loadData() throws { }


func test() {
do {
try loadData()
} catch {
print(error)
}
}

Implementation of a Set type


Improvements to Cocoa UI

Mobile Device Development

UFCFX5-15-3

Summary

Swift 2 is a programming language which draws syntax and structures from other well - established languages.
Swift 2 uses inferred (loose) typing, its basic syntax is similar to JavaScript and ActionScript 3.0
Swift 2 is only currently available for the OSX platform via
XCode 7.x
Swift 2 features a Set type and try-catch throw error model
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.

Mobile Device Development

UFCFX5-15-3

Swift Coding Resources


Apple Developer Portal
https://developer.apple.com/swift/resources/
Online Swift compiler
http://www.runswiftlang.com/
Swift Tutorials
https://www.hackingwithswift.com/
Mobile Device Development

UFCFX5-15-3

iOS Recommend Text

Mobile Device Development

UFCFX5-15-3

Das könnte Ihnen auch gefallen