MOBILE COMPUTING 2
By:
Dr. Mohammad Shoab
Week 12
Swift Programming Language
2
Mobile Computing 2
Department of Computer Science
Advantages of Swift
3
Mobile Computing 2
Department of Computer Science
Basic Syntax
/* My first program in Swift 4 */
var myString = "Hello, World!"
print(myString)
import UIKit
var myString = "Hello, World!"
print(myString)
4
Mobile Computing 2
Department of Computer Science
Swift Data Types
5
Type | Typical Bit Width | Typical Range |
Int8 | 1byte | -127 to 127 |
UInt8 | 1byte | 0 to 255 |
Int32 | 4bytes | -2147483648 to 2147483647 |
UInt32 | 4bytes | 0 to 4294967295 |
Int64 | 8bytes | -9223372036854775808 to 9223372036854775807 |
UInt64 | 8bytes | 0 to 18446744073709551615 |
Float | 4bytes | 1.2E-38 to 3.4E+38 (~6 digits) |
Double | 8bytes | 2.3E-308 to 1.7E+308 (~15 digits) |
Mobile Computing 2
Department of Computer Science
Variable Declaration in Swift
A variable declaration tells the compiler where and how much to create the storage for the variable.
Syntax:
var variableName = <initial value>
Ex.
var varA = 42
print(varA)
6
Mobile Computing 2
Department of Computer Science
Cont…
Type Annotations: You can provide a type annotation when you declare a variable, to be clear about the kind of values the variable can store.
Syntax:
var variableName:<data type> = <optional initial value>
Ex:
var varA = 42
print(varA)
var varB:Float
varB = 3.14159
print(varB)
7
Mobile Computing 2
Department of Computer Science
Tuples
Syntax:
var TupleName = (Value1, value2,… any number of values)
Ex:
var error501 = (501, “Not implemented”)
8
Mobile Computing 2
Department of Computer Science
Swift String
// String creation using String literal
var stringA = "Hello, Swift 4!"
print( stringA )
// String creation using String instance
var stringB = String("Hello, Swift 4!")
print( stringB )
//Multiple line string
let stringC = """
Hey this is a
example of multiple Line
string by university """
print(stringC)
9
Mobile Computing 2
Department of Computer Science
Swift Arrays
Here is the syntax to create an array of a given size a* and initialize it with a value −
var someArray = [SomeType](count: NumbeOfElements, repeatedValue: InitialValue)
10
Mobile Computing 2
Department of Computer Science
Cont…
Accessing Arrays: Here, the index starts from 0 which means the first element can be accessed using the index as 0, the second element can be accessed using the index as 1 and so on.
var someInts = [Int](count: 3, repeatedValue: 10)
var someVar = someInts[0]
print( "Value of first element is \(someVar)" )
print( "Value of second element is \(someInts[1])" )
print( "Value of third element is \(someInts[2])" )
11
Value of first element is 10
Value of second element is 10
Value of third element is 10
Mobile Computing 2
Department of Computer Science
Swift Functions
A function is a set of statements organized together to perform a specific task. A Swift 4 function can be as simple as a simple C function to as complex as an Objective C language function. It allows us to pass local and global parameter values inside the function calls.
Swift 4 functions contain parameter type and its return types.
12
Mobile Computing 2
Department of Computer Science
Cont…
Syntax:
func funcname(Parameters) -> returntype {
Statement1
Statement2
---
Statement N
return parameters
}
13
Mobile Computing 2
Department of Computer Science
Cont…
Calling a function:
func display(no1: Int) -> Int {
let a = no1
return a
}
print(display(no1: 100))
print(display(no1: 200))
14
Mobile Computing 2
Department of Computer Science
Cont…
Functions with parameters:
func mult(no1: Int, no2: Int) -> Int {
return no1*no2
}
print(mult(no1: 2, no2: 20))
print(mult(no1: 3, no2: 15))
print(mult(no1: 4, no2: 30))
15
Answer:
40
45
120
Mobile Computing 2
Department of Computer Science
Cont…
Functions without parameters:
func votersname() -> String {
return "Alice"
}
print(votersname())
16
Answer:
Alice
Mobile Computing 2
Department of Computer Science
The End
17
Mobile Computing 2
Department of Computer Science
Exercise
Q1. Explain Swift programming language.
Q2. What are the advantages of swift?
Q3. Write about the basic syntax of Swift.
Q4. Explain variable declaration in Swift with example.
Q5. Explain Swift functions.
18
Mobile Computing 2
Department of Computer Science
Q6. Swift 4 adopts the best of
Q7. Swift 4 provides seamless access to existing
19
Mobile Computing 2
Department of Computer Science