Introduction to iOS Swift Programming at TalTech IT College

 
Native Mobile
Applications -
ICD0017
 
TalTech IT College, Andres Käver, 2018-2019, Spring semester
Web: http://enos.Itcollege.ee/~akaver/MobileApps
Skype: akaver   Email: 
akaver@itcollege.ee
 
1
 
iOS - Swift
 
SWIFT
Started in July 2010 
 Chris Lattner
1.0 Sept 9, 2014
2.0 WWDC 2015
2.2 Open Source (swift.org) Dec 3, 2015
3.0 Sept 13, 2016
4.0 Sept 19 2017, 4.1 March 2018, 4.2 Sept 2018
5.0 March 3 2019
 
2015 
 Most loved programming language 
 first place (Stack Overflow)
2016 
 Second place
2017-2018 In top 5
 
2
 
iOS - Swift
 
Swift Playgrounds
iPad app, 3D video game-like interface
Xcode
Web based - http://online.swiftplayground.run
 
3
 
iOS 
 Swift - variables
 
Constants
 
 
 
Type inference
 
 
 
Variables
 
4
let
 school: 
String
 = 
”TalTech IT College"
let
 founded: 
Int
 = 
2000
let
 isAwesome: 
Bool
 = 
true
let
 school = 
”TalTech IT College"
let
 founded = 
2000
let
 isAwesome = 
true
var
 age = 
16
 
iOS 
 Swift - Strings
 
Concatenation
 
 
Interpolation
 
 
Full unicode support
 
5
let
 school = 
”TalTech IT College"
let
 message = 
"Hello, "
 
+
 
school
 
+ 
"!"
let
 school = 
”TalTech IT College"
let
 message = 
"Hello, 
\
(
school
)!"
let
 õäöüžš
ج
信息技术
 = 
"
信息技术
õäüöžš
ب
 
ت
 
جث
"
 
iOS 
 Swift - Strings
 
Characters
 
 
 
Iteration
 
6
let
 õäöüžš
ج
信息技术
 = 
"
信息技术
õäüöžš
ب
 
ت
 
جث
"
print
(
"
\
(
õäöüžš
ج
信息技术
) is 
\
(
õäöüžš
ج
信息技术
.
characters
.
count
) chars long"
)
\\ 16
let
 õäöüžš
ج
信息技术
 = 
"
信息技术
õäüöžš
ب
 
ت
 
جث
"
for
 character 
in
 
õäöüžš
ج
信息技术
.
characters
 {
    
print
(character)
}
 
iOS 
 Swift - Collections
 
Array and Dictionary
 
7
let
 names: [
String
] = [
"Jüri"
, 
"Mari"
, 
"Kalle"
]
 
let
 ages = [
"Kalle"
: 
17
, 
"Malle"
: 
18
]
 
iOS 
 Swift - Loops
 
While and Repeat-While
 
8
var
 name = 
"test"
var
 i = 
0
while
 
i
 < 
name
.
characters
.
count
 {
    
print
(
"At pos 
\
(
i
) is 
\
(
name
[
name
.
index
(
name
.
startIndex
, offsetBy: 
i
)]
)"
)
    
i
 += 
1
}
i
 = 
0
repeat
 {
    
print
(
"At pos 
\
(
i
) is 
\
(
name
[
name
.
index
(
name
.
startIndex
, offsetBy: 
i
)]
)"
)
    
i
 += 
1
} 
while
 
i
 < 
name
.
characters
.
count
 
iOS 
 Swift - Loops
 
For-In Loop 
 closed ranges
 
 
 
Half closed ranges
 
9
for
 number 
in
 
1
...
5
 {
    
print
(number)
}
let
 numbers = [
7
, 
6
, 
42
, 
3
, 
9
, 
1
]
let
 maxCount = 
4
for
 index 
in
 
0
..<
maxCount
 {
    
print
(
numbers
[index])
}
 
iOS 
 Swift - Loops
 
For-In Loop 
 Array & Dictionary
 
10
let
 ages 
=
 [
"Kalle"
: 
17
, 
"Malle"
: 
18
]
 
for
 (name, age) 
in
 
ages
 {
    
print
(
"
\
(
name
) - 
\
(
age
)"
)
}
 
iOS 
 Swift - Array
 
Modifying
 
 
 
Append multiple
 
 
Replace multiple
 
11
var
 numbers = [
7
, 
6
, 
42
, 
3
, 
9
, 
1
]
numbers
.
append
(
5
)
numbers
[
1
] = 
8
numbers
.
append
(contentsOf: [
4
, 
13
, 
-5
])
numbers
[
3
...
5
] = [
1
, 
2
]
 
iOS 
 Swift - Optionals
 
Maybe value is not there?
 
 
 
 
 
Checking
 
12
let
 ages = [
"Kalle"
: 
17
, 
"Malle"
: 
18
]
let
 possibleAge = 
ages
[
"Malle"
]
print
(
"
\
(
possibleAge
)"
)
let
 possibleAge: 
Int?
 = 
ages
[
"Malle"
]
if
 
possibleAge
 == 
nil
 {
    
print
(
"Age not found."
)
}
 
iOS 
 Swift 
 If-Let
 
If-Let
 
13
let
 ages = [
"Kalle"
: 
17
, 
"Malle"
: 
18
]
if
 
let
 age = 
ages
[
"Kalle"
] {
    
print
(
"Kalle's age is 
\
(
age
)"
)
}
 
iOS 
 Swift - Switch
 
Switch
 
14
let
 age = 
90
switch
 
age
 {
case
 
1
:
    
print
(
”First year done"
)
case
 
13
...
19
:
    
print
(
”Teenager is hard period!"
)
case
 
let
 decade 
where
 decade % 
10
 == 
0
:
    
print
(
”Congrats on 
\
(
decade
)*10 years"
)
default
:
    
print
(
”regular birthday"
)
}
 
iOS 
 Swift - Switch
 
Switch over multiple values
 
15
let
 userName = 
"admin"
let
 passwordIsValid = 
true
 
switch
 (
userName
, 
passwordIsValid
) {
case
 (
"admin"
, 
true
):
    
print
(
"admin you are"
)
case
 (
"guest"
, 
_
):
    
print
(
"guests not allowed"
)
case
 (
_
, let isValid):
    
print
(isValid ? 
"admin area granted"
 : 
"DENIED"
)
}
 
iOS 
 Swift - Functions
 
Func keyword
 
16
func
 sendMessage() {
    
let
 message = 
”Hello!"
    
print
(message)
}
 
sendMessage
()
 
iOS 
 Swift - functions
 
Parameters
 
17
func
 sendMessage(shouting: 
Bool
) {
    
var
 message = 
”Hello!"
    message = shouting ? message.
uppercased
() : message
    
print
(message)
}
sendMessage
(shouting: 
true
)
 
iOS 
 Swift - functions
 
Multiple parameters (argument, label)
 
 
 
 
Better to read in usage, bad in function code
 
18
func
 sendMessage(recipient: 
String
, shouting: 
Bool
) {
    
let
 message = 
”Hello 
\
(
recipient
)!"
    
print
(shouting ? message.
uppercased
() : message)
}
sendMessage
(recipient: 
"Andres"
, shouting: 
true
)
func
 sendMessage(to: 
String
, shouting: 
Bool
) {
    
let
 message = 
”Hello 
\
(
to
)!"
    
print
(shouting ? message.
uppercased
() : message)
}
sendMessage
(to: 
"Andres"
, shouting: 
true
)
 
iOS 
 Swift - functions
 
Parameters
 
19
func
 sendMessage(to recipient: 
String
, shouting: 
Bool
) {
    
let
 message = 
"Tere 
\
(
recipient
)!"
    
print
(shouting ? message.
uppercased
() : message)
}
sendMessage
(to: 
"Andres"
, shouting: 
true
)
 
iOS 
 Swift - Functions
 
Parameters 
 using underscore 
 no label
 
20
func
 sendMessage(message: 
String
, to recipient: 
String
, shouting: 
Bool
) {
    
let
 message = 
"
\
(
message
), 
\
(
recipient
)!"
    
print
(shouting ? message.
uppercased
() : message)
}
 
sendMessage
(message: 
"Hello"
, to: 
"Andres"
, shouting: 
true
)
func
 sendMessage(
_
 message: 
String
, to recipient: 
String
, shouting: 
Bool
) {
    
let
 message = 
"
\
(
message
), 
\
(
recipient
)!"
    
print
(shouting ? message.
uppercased
() : message)
}
 
sendMessage
(
"Hello"
, to: 
"Andres"
, shouting: 
true
)
 
iOS 
 Swift - Functions
 
Return values
 
21
func
 firstString(havingPrefix prefix: 
String
, in strings: [
String
]) -> 
String
? {
    
for
 string 
in
 strings {
        
if
 string.
hasPrefix
(prefix){
            
return
 string
        }
    }
    
return
 
nil
}
 
print
(
"Result 
\
(
firstString
(havingPrefix: 
"And"
, in: [
"Jüri"
, 
"Andres"
])
)"
)
 
iOS 
 Swift - Closures
 
Function type
(parameter types) -> return type
func sendMessage() {}
() -> Void
func firstString(havingPrefix prefix: String, in strings: [String]) -> String? {}
(String, [String]) -> String?
 
22
 
iOS 
 Swift - Closures
 
Functions as parameters
 
23
func
 filterInts(
_
 numbers: [
Int
], 
_
 includeNumber: (
Int
) -> 
Bool
) -> [
Int
] {
    
var
 result: [
Int
] = []
    
for
 number 
in
 numbers {
        
if
 includeNumber(number) {
            result.
append
(number)
        }
    }
    
return
 result
}
 
let
 numbers = [
4
, 
17
, 
34
, 
41
]
func
 divisibleByTwo(
_
 number: 
Int
) -> 
Bool
 {
    
return
 number % 
2
 == 
0
}
 
let
 evenNumbers = 
filterInts
(
numbers
, 
divisibleByTwo
)
print
(
evenNumbers
)
 
iOS 
 Swift - Closures
 
Closure expression 
 inline function definitions (not named functions)
 
24
func
 divisibleByTwo(
_
 number: 
Int
) -> 
Bool
 {
    
return
 number % 
2
 == 
0
}
 
let
 evenNumbers =
 
filterInts
(
numbers
, { (number: 
Int
) -> 
Bool
 
in
 
return
 number % 
2
 == 
0
})
 
iOS 
 Swift – Closures - definition
 
25
func
 filterInts(
_
 numbers: [
Int
], 
_
 includeNumber: (
Int
) -> 
Bool
) -> [
Int
] {……}
 
let
 numbers = [
4
, 
17
, 
34
, 
41
]
print
( 
filterInts
(
numbers
, { (number: 
Int
) -> 
Bool
 
in
 
return
 number % 
2
 == 
0
}) )
 
// can be inferred from filterIntents declaration
print
( 
filterInts
(
numbers
, { number 
in
 
return
 number % 
2
 == 
0
}) )
 
// if its single liner, no need for return keyword
print
( 
filterInts
(
numbers
, { number 
in
 number % 
2
 == 
0
}) )
 
// implicit arguments, no need for in keyword
print
( 
filterInts
(
numbers
, {$0 % 
2
 == 
0
}) )
 
// if closure is last argument, you can write as trailing closure
print
( 
filterInts
(
numbers
) {$0 % 
2
 == 
0
} )
 
iOS 
 Swift - Generics
 
 
26
let
 numbers = [
4
, 
17
, 
34
, 
41
]
let
 names = [
"Andres"
, 
"kala"
]
func
 filter<TElement>(
_
 source: [
TElement
],
      
 
_
 includeElement: (
TElement
) -> 
Bool
) -> [
TElement
] {
    
var
 result: [
TElement
] = []
    
for
 elem 
in
 source {
        
if
 includeElement(elem) {
            result.
append
(elem)
        }
 
 }
    
return
 result
}
 
let
 evenNumbers = 
filter
(
numbers
) { $0 % 
2
 == 
0
 }
let
 shortNames = 
filter
(
names
) { name 
in
 name.
characters
.
count
 < 
5
 }
 
iOS 
 Swift 
 Map, Filter
 
Closures and generics are widely used in Swift libraries
 
27
let
 names = [
"Lily"
, 
"Santiago"
, 
"Aadya"
, 
"Jack"
, 
"Anna"
, 
"Andrés"
]
let
 shortNames = 
names
.
filter
 { name 
in
 name.
characters
.
count
 < 
5
 }
print
(
shortNames
)
let
 capitalizedShortNames = 
shortNames
.
map
 { name 
in
 name.
uppercased
() }
print
(
capitalizedShortNames
)
Slide Note
Embed
Share

Explore the world of iOS app development using Swift programming language at TalTech IT College under the guidance of Andres Kver during the 2018-2019 Spring semester. Delve into topics like variables, strings, collections, and loops, gaining a solid foundation in iOS app development.

  • iOS Development
  • Swift Programming
  • TalTech IT College
  • Mobile Apps
  • App Development

Uploaded on Sep 12, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. 1 Native Mobile Applications - ICD0017 TalTech IT College, Andres K ver, 2018-2019, Spring semester Web: http://enos.Itcollege.ee/~akaver/MobileApps Skype: akaver Email: akaver@itcollege.ee

  2. iOS - Swift 2 SWIFT Started in July 2010 Chris Lattner 1.0 Sept 9, 2014 2.0 WWDC 2015 2.2 Open Source (swift.org) Dec 3, 2015 3.0 Sept 13, 2016 4.0 Sept 19 2017, 4.1 March 2018, 4.2 Sept 2018 5.0 March 3 2019 2015 Most loved programming language first place (Stack Overflow) 2016 Second place 2017-2018 In top 5

  3. iOS - Swift 3 Swift Playgrounds iPad app, 3D video game-like interface Xcode Web based - http://online.swiftplayground.run

  4. iOS Swift - variables 4 Constants let school: String = TalTech IT College" let founded: Int = 2000 let isAwesome: Bool = true Type inference let school = TalTech IT College" let founded = 2000 let isAwesome = true Variables var age = 16

  5. iOS Swift - Strings 5 Concatenation let school = TalTech IT College" let message = "Hello, " + school + "!" Interpolation let school = TalTech IT College" let message = "Hello, \(school)!" Full unicode support let = " "

  6. iOS Swift - Strings 6 Characters let = " print("\( ) is \( .characters.count) chars long") \\ 16 " Iteration let = " for character in .characters { print(character) } "

  7. iOS Swift - Collections 7 Array and Dictionary let names: [String] = ["J ri", "Mari", "Kalle"] let ages = ["Kalle": 17, "Malle": 18]

  8. iOS Swift - Loops 8 While and Repeat-While var name = "test" var i = 0 while i < name.characters.count { print("At pos \(i) is \(name[name.index(name.startIndex, offsetBy: i)])") i += 1 } i = 0 repeat { print("At pos \(i) is \(name[name.index(name.startIndex, offsetBy: i)])") i += 1 } while i < name.characters.count

  9. iOS Swift - Loops 9 For-In Loop closed ranges for number in 1...5 { print(number) } Half closed ranges let numbers = [7, 6, 42, 3, 9, 1] let maxCount = 4 for index in 0..<maxCount { print(numbers[index]) }

  10. iOS Swift - Loops 10 For-In Loop Array & Dictionary let ages = ["Kalle": 17, "Malle": 18] for (name, age) in ages { print("\(name) - \(age)") }

  11. iOS Swift - Array 11 Modifying var numbers = [7, 6, 42, 3, 9, 1] numbers.append(5) numbers[1] = 8 Append multiple numbers.append(contentsOf: [4, 13, -5]) Replace multiple numbers[3...5] = [1, 2]

  12. iOS Swift - Optionals 12 Maybe value is not there? let ages = ["Kalle": 17, "Malle": 18] let possibleAge = ages["Malle"] print("\(possibleAge)") let possibleAge: Int? = ages["Malle"] Checking if possibleAge == nil { print("Age not found.") }

  13. iOS Swift If-Let 13 If-Let let ages = ["Kalle": 17, "Malle": 18] if let age = ages["Kalle"] { print("Kalle's age is \(age)") }

  14. iOS Swift - Switch 14 Switch let age = 90 switch age { case 1: print( First year done") case 13...19: print( Teenager is hard period!") case let decade where decade % 10 == 0: print( Congrats on \(decade)*10 years") default: print( regular birthday") }

  15. iOS Swift - Switch 15 Switch over multiple values let userName = "admin" let passwordIsValid = true switch (userName, passwordIsValid) { case ("admin", true): print("admin you are") case ("guest", _): print("guests not allowed") case (_, let isValid): print(isValid ? "admin area granted" : "DENIED") }

  16. iOS Swift - Functions 16 Func keyword func sendMessage() { let message = Hello!" print(message) } sendMessage()

  17. iOS Swift - functions 17 Parameters func sendMessage(shouting: Bool) { var message = Hello!" message = shouting ? message.uppercased() : message print(message) } sendMessage(shouting: true)

  18. iOS Swift - functions 18 Multiple parameters (argument, label) func sendMessage(recipient: String, shouting: Bool) { let message = Hello \(recipient)!" print(shouting ? message.uppercased() : message) } sendMessage(recipient: "Andres", shouting: true) Better to read in usage, bad in function code func sendMessage(to: String, shouting: Bool) { let message = Hello \(to)!" print(shouting ? message.uppercased() : message) } sendMessage(to: "Andres", shouting: true)

  19. iOS Swift - functions 19 Parameters func sendMessage(to recipient: String, shouting: Bool) { let message = "Tere \(recipient)!" print(shouting ? message.uppercased() : message) } sendMessage(to: "Andres", shouting: true)

  20. iOS Swift - Functions 20 Parameters using underscore no label func sendMessage(message: String, to recipient: String, shouting: Bool) { let message = "\(message), \(recipient)!" print(shouting ? message.uppercased() : message) } sendMessage(message: "Hello", to: "Andres", shouting: true) func sendMessage(_ message: String, to recipient: String, shouting: Bool) { let message = "\(message), \(recipient)!" print(shouting ? message.uppercased() : message) } sendMessage("Hello", to: "Andres", shouting: true)

  21. iOS Swift - Functions 21 Return values func firstString(havingPrefix prefix: String, in strings: [String]) -> String? { for string in strings { if string.hasPrefix(prefix){ return string } } return nil } print("Result \(firstString(havingPrefix: "And", in: ["J ri", "Andres"]))")

  22. iOS Swift - Closures 22 Function type (parameter types) -> return type func sendMessage() {} () -> Void func firstString(havingPrefix prefix: String, in strings: [String]) -> String? {} (String, [String]) -> String?

  23. iOS Swift - Closures 23 Functions as parameters func filterInts(_ numbers: [Int], _ includeNumber: (Int) -> Bool) -> [Int] { var result: [Int] = [] for number in numbers { if includeNumber(number) { result.append(number) } } return result } let numbers = [4, 17, 34, 41] func divisibleByTwo(_ number: Int) -> Bool { return number % 2 == 0 } let evenNumbers = filterInts(numbers, divisibleByTwo) print(evenNumbers)

  24. iOS Swift - Closures 24 Closure expression inline function definitions (not named functions) func divisibleByTwo(_ number: Int) -> Bool { return number % 2 == 0 } let evenNumbers = filterInts(numbers, { (number: Int) -> Bool in return number % 2 == 0})

  25. iOS Swift Closures - definition 25 func filterInts(_ numbers: [Int], _ includeNumber: (Int) -> Bool) -> [Int] { } let numbers = [4, 17, 34, 41] print( filterInts(numbers, { (number: Int) -> Bool in return number % 2 == 0}) ) // can be inferred from filterIntents declaration print( filterInts(numbers, { number in return number % 2 == 0}) ) // if its single liner, no need for return keyword print( filterInts(numbers, { number in number % 2 == 0}) ) // implicit arguments, no need for in keyword print( filterInts(numbers, {$0 % 2 == 0}) ) // if closure is last argument, you can write as trailing closure print( filterInts(numbers) {$0 % 2 == 0} )

  26. iOS Swift - Generics 26 let numbers = [4, 17, 34, 41] let names = ["Andres", "kala"] func filter<TElement>(_ source: [TElement], var result: [TElement] = [] for elem in source { if includeElement(elem) { result.append(elem) } } return result } _ includeElement: (TElement) -> Bool) -> [TElement] { let evenNumbers = filter(numbers) { $0 % 2 == 0 } let shortNames = filter(names) { name in name.characters.count < 5 }

  27. iOS Swift Map, Filter 27 Closures and generics are widely used in Swift libraries let names = ["Lily", "Santiago", "Aadya", "Jack", "Anna", "Andr s"] let shortNames = names.filter { name in name.characters.count < 5 } print(shortNames) let capitalizedShortNames = shortNames.map { name in name.uppercased() } print(capitalizedShortNames)

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#