-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
60 lines (47 loc) · 1.07 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//Error Handling
//Syntax:
/*
do{
//Create audio player object
audioPlayer = try AVAudioPlayer(contentsOf: soundURL)
}
catch{
//Didnt work
print("Couldnt create for file")
}
*/
//if try was used like try? then the error wont be thrown rather the variable audioPlayer will be nil
//try! will forcibly store the stuff in the variable even if its nill but now auto throwing
//Creating your own Swift error type
import Darwin
enum BankAccount: Error{
case insufficientFunds
case invalidTransaction
case duplicateTransaction
case unkown
}
var funds: Int = 0
func withDrawCash(amount: Int)throws{
if (funds - amount < 0){
throw BankAccount.insufficientFunds
}
else{
funds -= amount
}
}
do{
try withDrawCash(amount: 100)
}
catch BankAccount.insufficientFunds{
print("Insufficient funds")
}
catch{
print("Unexpected error!")
}
//Guard statements
func sqrt(number: Int) -> Int?{
guard number >= 0 else{
return nil
}
return Int(pow(Double(number), 1/2))
}