-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.html
265 lines (229 loc) · 7.05 KB
/
index.html
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="Description" content="A quick cheat sheet and reference guide for Apple's Swift language. This guide intends to cover all the key features of Swift, including Strings, Arrays and Dictionaries.">
<title>Swift Cheat Sheet</title>
<link rel="stylesheet" href="stylesheets/styles.css">
<link rel="stylesheet" href="stylesheets/pygment_trac.css">
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.0/styles/solarized_light.min.css">
<script src="javascripts/scale.fix.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.0/highlight.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.0/languages/javascript.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<div class="wrapper">
<header>
<h3 class="header">Jump To:</h3>
<p class="header"></p>
<ul>
<li><a href="#variables">Variables</a></li>
<li><a href="#constants">Constants</a></li>
<li><a href="#strings">Strings</a></li>
<li><a href="#logical">Logical Operators</a></li>
<li><a href="#printing">Printing</a></li>
<li><a href="#arrays">Arrays</a></li>
<li><a href="#dictionaries">Dictionaries</a></li>
<li><a href="#conditionals">Conditionals</a></li>
<li><a href="#forloops">For Loops</a></li>
<li><a href="#whileloops">While Loops</a></li>
<li><a href="#functions">Functions</a></li>
<li><a href="#classes">Classes</a></li>
<li><a href="#closures">Closures</a></li>
</ul>
</header>
<section>
<h1>Swift Cheat Sheet</h1>
<p>A quick cheat sheet and reference guide for Apple's Swift language. This guide intends to cover all the key features of Swift, including Strings, Arrays, Dictionaries and Flow Control.</p>
<p>Swift is a new programming language for developing iOS and OS X apps that was introduced by Apple in June 2014.</p>
<h3 id="variables">Variables</h3>
<pre><code>var myInt = 1
var myExplicitInt: Int = 1 // explicit type
var x = 1, y = 2, z = 3 // declare multiple integers
myExplicitInt = 2 // set to another integer value
</code></pre>
<h3 id="constants">Constants</h3>
<pre><code>let myInt = 1
myInt = 2 // compile-time error!
</code></pre>
<h3 id="strings">Strings</h3>
<pre><code>var myString = "a"
let myImmutableString = "c"
myString += "b" // ab
myString = myString + myImmutableString // abc
myImmutableString += "d" // compile-time error!
let count = 7
let message = "There are \(count) days in a week"
</code></pre>
<h3 id="logical">Logical Operators</h3>
<pre><code>var happy = true
var sad = !happy // logical NOT, sad = false
var everyoneHappy = happy && sad // logical AND, everyoneHappy = false
var someoneHappy = happy || sad // logical OR, someoneHappy = true
</code></pre>
<h3 id="printing">Printing</h3>
<pre><code>let name = "swift"
println("Hello")
println("My name is \(name)")
print("See you ")
print("later")
/* Hello
My name is swift
See you later */
</code></pre>
<h3 id="arrays">Arrays</h3>
<pre><code>var colors = ["red", "blue"]
var moreColors: String[] = ["orange", "purple"] // explicit type
colors.append("green") // [red, blue, green]
colors += "yellow" // [red, blue, green, yellow]
colors += moreColors // [red, blue, green, yellow, orange, purple]
var days = ["mon", "thu"]
var firstDay = days[0] // mon
days.insert("tue", atIndex: 1) // [mon, tue, thu]
days[2] = "wed" // [mon, tue, wed]
days.removeAtIndex(0) // [tue, wed]
</code></pre>
<h3 id="dictionaries">Dictionaries</h3>
<pre><code>var days = ["mon": "monday", "tue": "tuseday"]
days["tue"] = "tuesday" // change the value for key "tue"
days["wed"] = "wednesday" // add a new key/value pair
var moreDays: Dictionary<String, String> = ["thu": "thursday", "fri": "friday"]
moreDays["thu"] = nil // remove thu from the dictionary
moreDays.removeValueForKey("fri") // remove fri from the dictionary
</code></pre>
<h3 id="conditionals">Conditionals</h3>
<pre><code>//IF STATEMENT
let happy = true
if happy {
println("We're Happy!")
} else {
println("We're Sad :('")
}
// We're Happy!
let speed = 28
if speed <= 0 {
println("Stationary")
} else if speed <= 30 {
println("Safe speed")
} else {
println("Too fast!")
}
// Safe speed
//SWITCH STATEMENT
let n = 2
switch n {
case 1:
println("It's 1!")
case 2...4:
println("It's between 2 and 4!")
case 5, 6:
println("It's 5 or 6")
default:
println("Its another number!")
}
// It's between 2 and 4!
</code></pre>
<h3 id="forloops">For Loops</h3>
<pre><code>for var index = 1; index < 3; ++index {
// loops with index taking values 1,2
}
for index in 1..3 {
// loops with index taking values 1,2
}
for index in 1...3 {
// loops with index taking values 1,2,3
}
let colors = ["red", "blue", "yellow"]
for color in colors {
println("Color: \(color)")
}
// Color: red
// Color: blue
// Color: yellow
let days = ["mon": "monday", "tue": "tuesday"]
for (shortDay, longDay) in days {
println("\(shortDay) is short for \(longDay)")
}
// mon is short for monday
// tue is short for tuesday
</code></pre>
<h3 id="whileloops">While Loops</h3>
<pre><code>var count = 1
while count < 3 {
println("count is \(count)")
++count
}
// count is 1
// count is 2
count = 1
while count < 1 {
println("count is \(count)")
++count
}
//
count = 1
do {
println("count is \(count)")
++count
} while count < 3
// count is 1
// count is 2
count = 1
do {
println("count is \(count)")
++count
} while count < 1
// count is 1
</code></pre>
<h3 id="functions">Functions</h3>
<pre><code>func iAdd(a: Int, b: Int) -> Int {
return a + b
}
iAdd(2, 3) // returns 5
func eitherSide(n: Int) -> (nMinusOne: Int, nPlusOne: Int) {
return (n-1, n+1)
}
eitherSide(5) // returns the tuple (4,6)
</code></pre>
<h3 id="classes">Classes</h3>
<pre><code>class Counter {
var count: Int = 0
func inc() {
count++
}
func add(n: Int) {
count += n
}
func printCount() {
println("Count: \(count)")
}
}
var myCount = Counter()
myCount.inc()
myCount.add(2)
myCount.printCount() // Count: 3
</code></pre>
<h3 id="closures">Closures</h3>
<pre><code>
</code></pre>
</section>
<footer>
</footer>
</div>
<!--[if !IE]><script>fixScale(document);</script><![endif]-->
<script>hljs.initHighlightingOnLoad();</script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-51587235-1', 'kpbp.github.io');
ga('send', 'pageview');
</script>
</body>
</html>