-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.swift
63 lines (50 loc) · 938 Bytes
/
main.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
61
62
63
// swift run
// 1 type declaration
struct Person {
var name: String
var age: Int
// if self mutate, need special modifier
mutating func haveBirthday() {
self.age += 1
}
}
struct LoggedIn {
var name: String
}
// 2 tagged union declaration
enum User {
case LoggedIn(LoggedIn)
case Guest
// 10 Switch
func getName() -> String {
switch self {
case let .LoggedIn(loggedIn):
return loggedIn.name
case .Guest:
return "guest"
}
}
}
var x: User = User.LoggedIn(LoggedIn(name: "sas"))
print(x.getName())
// 3 hello world
print("Hello, world!")
// 4 creating instance of type
//mutating function can be called only on var
var person = Person(name: "sas", age: 42)
person.haveBirthday()
// 8 loop
for i in 1...10 {
// print(i)
}
// 9 if
if (true) {
print(true)
}
// 11 list
var list = [2, 3]
list.append(4)
// 12 map
var map = ["a" : 1, "b" : 2]
map["c"] = 3
print(map["a"])