1 of 19

MOBILE COMPUTING 2

By:

Dr. Mohammad Shoab

Week 12

2 of 19

Swift Programming Language

  • Swift 4 is a new programming language developed by Apple Inc for iOS and OS X development. Swift 4 adopts the best of C and Objective-C, without the constraints of C compatibility.
  • Swift 4 uses the same runtime as the existing Obj-C system on Mac OS and iOS, which enables Swift 4 programs to run on many existing iOS 6 and OS X 10.8 platforms.
  • Swift 4 comes with playground feature where Swift 4 programmers can write their code and execute it to see the results immediately.
  • The first public release of Swift was released in 2010. It took Chris Lattner almost 14 years to come up with the first official version, and later, it was supported by many other contributors. Swift 4 has been included in Xcode 6 beta.

2

Mobile Computing 2

Department of Computer Science

3 of 19

Advantages of Swift

  • Swift 4 makes use of safe programming patterns.
  • Swift 4 provides modern programming features.
  • Swift 4 provides Objective-C like syntax.
  • Swift 4 is a fantastic way to write iOS and OS X apps.
  • Swift 4 provides seamless access to existing Cocoa frameworks.
  • Swift 4 unifies the procedural and object-oriented portions of the language.
  • Swift 4 does not need a separate library import to support functionalities like input/output or string handling.

3

Mobile Computing 2

Department of Computer Science

4 of 19

Basic Syntax

  • Let's start once again with the following Hello, World! program created for OS X playground, which includes import Cocoa as shown below:

/* My first program in Swift 4 */

var myString = "Hello, World!"

print(myString)

  • If you create the same program for iOS playground, then it will include import UIKit and the program will look as follows −

import UIKit

var myString = "Hello, World!"

print(myString)

4

Mobile Computing 2

Department of Computer Science

5 of 19

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

6 of 19

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

7 of 19

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

8 of 19

Tuples

  • Swift 4 also introduces Tuples type, which are used to group multiple values in a single compound Value. The values in a tuple can be of any type, and do not need to be of same type.

Syntax:

var TupleName = (Value1, value2,… any number of values)

Ex:

var error501 = (501, “Not implemented”)

8

Mobile Computing 2

Department of Computer Science

9 of 19

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

10 of 19

Swift Arrays

  • Swift 4 arrays are used to store ordered lists of values of the same type. Swift 4 puts strict checking which does not allow you to enter a wrong type in an array, even by mistake.

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

11 of 19

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

12 of 19

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.

    • Function Declaration − tells the compiler about a function's name, return type, and parameters.
    • Function Definition − It provides the actual body of the function.

Swift 4 functions contain parameter type and its return types.

12

Mobile Computing 2

Department of Computer Science

13 of 19

Cont…

Syntax:

func funcname(Parameters) -> returntype {

Statement1

Statement2

---

Statement N

return parameters

}

13

Mobile Computing 2

Department of Computer Science

14 of 19

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

15 of 19

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

16 of 19

Cont…

Functions without parameters:

func votersname() -> String {

return "Alice"

}

print(votersname())

16

Answer:

Alice

Mobile Computing 2

Department of Computer Science

17 of 19

The End

17

Mobile Computing 2

Department of Computer Science

18 of 19

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

19 of 19

Q6. Swift 4 adopts the best of

  1. C and Objective-C
  2. C and Java
  3. C and C++
  4. C and C#

Q7. Swift 4 provides seamless access to existing

  1. .Net framework
  2. Cocoa frameworks
  3. Java framework
  4. None of the above

19

Mobile Computing 2

Department of Computer Science