-
Notifications
You must be signed in to change notification settings - Fork 0
/
Contents.swift
81 lines (55 loc) · 1.44 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import Foundation
// this is a comment
print("Hello World")
/* This is a multi
line comment here */
// interporlation
print("The result of 2 + 3 = \(2 + 3)")
// variables
var a = 5
var b = 8
print("the value of a is \(a)")
print("the value of b is \(b)")
(a, b) = (b, a)
print("the value of a is \(a)")
print("the value of b is \(b)")
// Swift arrays
let arr = [1, 2, 3]
let firstItem = arr[0]
var numbers = [45, 73, 195, 53]
var computedNumbers: [Int] = numbers.enumerated().map({ (index, element) in
return element * numbers[((index + 1) % numbers.count)]
})
print(computedNumbers)
// Constants
let constant: Int = 1 // cannot be reassigned
// datatypes
// string
let str: String = "Hello world"
// int
let number: Int = 1
// float
let float: Float = 1.0
// Double
let double: Double = 2.0
// Bool
let bool: Bool = true
// Array
var array: [Int] = [1, 2, 3]
// Dictionaries
let dict: [String:Int] = ["People": 1]
// Random number
let rand: Int = Int.random(in: 1 ... 100)
// Range operator
// 1 ... 3 => closed range operator includes upper bound
// 1 ..< 3 => half open range excludes upper bound
// Random element in array
array.randomElement()
// Shuffle array
array.shuffle()
var password: String = ""
let alphabet: [String] = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
for _ in 1...6 {
password += alphabet.randomElement()!
}
print(password)