-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
239 lines (167 loc) · 10.2 KB
/
index.html
File metadata and controls
239 lines (167 loc) · 10.2 KB
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
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8">
<title>Swift-2.0 by AnilVarghese</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="stylesheets/normalize.css" media="screen">
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="stylesheets/stylesheet.css" media="screen">
<link rel="stylesheet" type="text/css" href="stylesheets/github-light.css" media="screen">
</head>
<body>
<section class="page-header">
<h1 class="project-name">Swift-2.0</h1>
<h2 class="project-tagline">Migrate to Swift-2.0</h2>
<a href="https://github.com/AnilVarghese/swift-2.0" class="btn">View on GitHub</a>
<a href="https://github.com/AnilVarghese/swift-2.0/zipball/master" class="btn">Download .zip</a>
<a href="https://github.com/AnilVarghese/swift-2.0/tarball/master" class="btn">Download .tar.gz</a>
</section>
<section class="main-content">
<h3>
<a id="1-use-xcode-7-migration-tool" class="anchor" href="#1-use-xcode-7-migration-tool" aria-hidden="true"><span class="octicon octicon-link"></span></a>1. Use Xcode 7 migration tool</h3>
<p>When you open an existing project in Xcode 7, it will ask you <strong>convert to Latest Swift Syntax?</strong> or you can manually convert using the migration tool <em><code>Edit menu->Convert-> To Latest Swift Syntax</code></em>.</p>
<h3>
<a id="2-error-handeling" class="anchor" href="#2-error-handeling" aria-hidden="true"><span class="octicon octicon-link"></span></a>2. Error handeling</h3>
<p>API's are updated to take advantage of new error handeling mechanism. Any api which throw error should be handled using <code>try catch</code>. or you can us <code>try?</code> or <code>try!</code></p>
<pre><code>//swift 1.2
var error: NSError?
let result = NSJSONSerialization.JSONObjectWithData(inputData, options: NSJSONReadingOptions.MutableContainers, error: &error) as! Dictionary<String,AnyObject>
//swift 2.0
var result:Dictionary<String,AnyObject>
do{
result = try NSJSONSerialization.JSONObjectWithData(inputData, options: NSJSONReadingOptions.MutableContainers) as!Dictionary<String,AnyObject>
}catch{
print(error)
}
</code></pre>
<h3>
<a id="3-availabilty" class="anchor" href="#3-availabilty" aria-hidden="true"><span class="octicon octicon-link"></span></a>3. Availabilty</h3>
<p>Before we have used <code>objc_getClass("UIAlertController")</code> to check the availabilty. now its more easier by using <code>#available</code>. You can even mark your functions specificaly using <code>@available</code></p>
<pre><code>if #available(iOS 8.0, *) {
let alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
} else {
let alert = UIAlertView(title: "Accounts", message: "Please login to a Facebook account to share.", delegate: nil, cancelButtonTitle: "OK")
alert.show()
}
</code></pre>
<h3>
<a id="4-removed--from-function-parameter-name" class="anchor" href="#4-removed--from-function-parameter-name" aria-hidden="true"><span class="octicon octicon-link"></span></a>4. Removed # from function parameter name.</h3>
<p><code>#</code> has been removed from swift, you should double up the parameter name to have the same argument label </p>
<pre><code>//swift 1.2
func startGameWith(#player:String, #level:Int){
}
startGameWith(player:"anil", level: 5)
//swift 2.0
func startGameWith(player player:String, level:Int){
}
startGameWith(player:"anil",level: 5)
</code></pre>
<h3>
<a id="5-unnamed-parameter-should-be-marked-by-_" class="anchor" href="#5-unnamed-parameter-should-be-marked-by-_" aria-hidden="true"><span class="octicon octicon-link"></span></a>5. Unnamed parameter should be marked by <code>_</code>
</h3>
<pre><code>//swift 1.2
func add(Int, Int){
}
add(5, 6)
//swift 2.0
func add(_: Int,_: Int){
}
add(5, 6)
</code></pre>
<h3>
<a id="6-optionsettype" class="anchor" href="#6-optionsettype" aria-hidden="true"><span class="octicon octicon-link"></span></a>6. OptionSetType</h3>
<p><code>OptionSetType</code> is a new protocol available in Swwift2.0 . Things like <code>UIViewAutoresizing</code> and <code>NSCalendarUnit</code> are of type <code>OptionSetType</code></p>
<pre><code>//swift 1.2
let autoresizingMask = .FlexibleWidth | .FlexibleHeight
//swift 2.0
let autoresizingMask = [.FlexibleWidth , .FlexibleHeight]
</code></pre>
<p>Many API's also has changed to take advantage of this protocol, especialy option/settings flags. </p>
<pre><code>UIView.animateWithDuration(1.0, delay: 0, options: nil, animations: { () -> Void in
}, completion: nil)
//swift2.0
UIView.animateWithDuration(1.0, delay: 0, options: [], animations: { () -> Void in
}, completion: nil)
</code></pre>
<h3>
<a id="7-new-is-not-available" class="anchor" href="#7-new-is-not-available" aria-hidden="true"><span class="octicon octicon-link"></span></a>7. new() is not available</h3>
<p>You have to use the object constructer instead.</p>
<pre><code>CABasicAnimation.new()
//swift2.0
CABasicAnimation()
</code></pre>
<h3>
<a id="8-println-is-removed" class="anchor" href="#8-println-is-removed" aria-hidden="true"><span class="octicon octicon-link"></span></a>8. println() is removed</h3>
<p>Now there is only <code>print()</code>, additionaly you can have <code>separator</code> and <code>terminator</code>.</p>
<pre><code>print("Hello")
// Writes the textual representations of `items`, separated by `separator` and terminated by `terminator`
print(items, separator: ",", terminator: "")
</code></pre>
<h3>
<a id="9-mutability-warning" class="anchor" href="#9-mutability-warning" aria-hidden="true"><span class="octicon octicon-link"></span></a>9. Mutability warning</h3>
<p>Use let and var appropriately. If the value of your variable is never changing, dont use <code>var</code>for that, should be <code>let</code>.</p>
<pre><code>var x = 1
var y = 2
let z = x+y
</code></pre>
<p>Above code will give warning, x&y are immutable variables, so it should be marked with <code>let</code></p>
<h3>
<a id="10-string-is-not-collection-type" class="anchor" href="#10-string-is-not-collection-type" aria-hidden="true"><span class="octicon octicon-link"></span></a>10. String is not collection type</h3>
<p>String is not a collection type anymore. </p>
<pre><code>count(string) //is wrong
string.characters.count // right
</code></pre>
<p>Ref: <a href="https://developer.apple.com/swift/blog/?id=30">https://developer.apple.com/swift/blog/?id=30</a></p>
<h3>
<a id="11-contains-is-not-available" class="anchor" href="#11-contains-is-not-available" aria-hidden="true"><span class="octicon octicon-link"></span></a>11. contains() is not available</h3>
<p>Global function contains() is not available use it like <code>array.contains</code></p>
<h3>
<a id="12-to-get-dictionary-keys" class="anchor" href="#12-to-get-dictionary-keys" aria-hidden="true"><span class="octicon octicon-link"></span></a>12. To get dictionary keys</h3>
<p>Cannot use like <code>dict.keys.array</code> use <code>Array(dict.keys)</code></p>
<h3>
<a id="13--settranslatesautoresizingmaskintoconstraints-not-available" class="anchor" href="#13--settranslatesautoresizingmaskintoconstraints-not-available" aria-hidden="true"><span class="octicon octicon-link"></span></a>13. setTranslatesAutoresizingMaskIntoConstraints() not available</h3>
<pre><code>//swift 2.0
view.translatesAutoresizingMaskIntoConstraints = false
</code></pre>
<h3>
<a id="14-changed-sorting-method-name" class="anchor" href="#14-changed-sorting-method-name" aria-hidden="true"><span class="octicon octicon-link"></span></a>14. Changed sorting method name</h3>
<p>method <code>sorted</code> is changed to <code>sort</code>. </p>
<pre><code>array.sort{
}
</code></pre>
<h3>
<a id="15-find-is-not-available" class="anchor" href="#15-find-is-not-available" aria-hidden="true"><span class="octicon octicon-link"></span></a>15. find() is not available</h3>
<p>Global function <code>find()</code> is not available use <code>indexOf()</code></p>
<pre><code>//swift 1.2
let index = find(array, object)
//swift 2.0
let index = array.indexOf(object)
</code></pre>
<h3>
<a id="16-advance-is-not-available" class="anchor" href="#16-advance-is-not-available" aria-hidden="true"><span class="octicon octicon-link"></span></a>16. advance() is not available</h3>
<p>Use advanceBy() instead. </p>
<pre><code>//swift 1.2
status.substringToIndex(advance(status.startIndex, 100))
//swift 2.0
status.substringToIndex(status.startIndex.advancedBy(100))
</code></pre>
<h3>
<a id="join-is-unavailable-use-joinwithseparator" class="anchor" href="#join-is-unavailable-use-joinwithseparator" aria-hidden="true"><span class="octicon octicon-link"></span></a>join is unavailable use joinWithSeparator</h3>
<pre><code>//swift 1.2
join(",", array)
//swift 2.0
array.joinWithSeparator(",")
</code></pre>
<p><a href="http://stackoverflow.com/questions/32408722/array-is-unavailable-please-construct-an-array-from-your-lazy-sequence-array">http://stackoverflow.com/questions/32408722/array-is-unavailable-please-construct-an-array-from-your-lazy-sequence-array</a>
<a href="http://swiftdoc.org/swift-2/type/LazyMapCollection/">http://swiftdoc.org/swift-2/type/LazyMapCollection/</a>
major changes here <a href="https://www.hackingwithswift.com/swift">https://www.hackingwithswift.com/swift</a></p>
<footer class="site-footer">
<span class="site-footer-owner"><a href="https://github.com/AnilVarghese/swift-2.0">Swift-2.0</a> is maintained by <a href="https://github.com/AnilVarghese">AnilVarghese</a>.</span>
<span class="site-footer-credits">This page was generated by <a href="https://pages.github.com">GitHub Pages</a> using the <a href="https://github.com/jasonlong/cayman-theme">Cayman theme</a> by <a href="https://twitter.com/jasonlong">Jason Long</a>.</span>
</footer>
</section>
</body>
</html>