Swift 3.0一些Api变动
What's new in Swift 3.0Swift3.0 is changingpretty much everything,and your code will almost certainly refuse to build until you make the necessary changes. Seriously,if you thought the jump from Swift 1.2 to 2.0 was big,you ain't seen nothing yet. In this article I'm going to explain some of the most important changes with as many code examples as I can,and hopefully this will give you some chance to be prepared to update your code when Swift 3.0 goes final. There are many more changes than the ones listed below,but the changes below are the ones that are most likely to hit you. If you liked this article,you might also enjoy these:
ADVANCE WARNING #1:Swift 3.0 is still under development. This article will be updated as new changes are announced. ADVANCE WARNING #2:There are lots and lots of changes,some of which might seem petty. However,the hope is that these changes are a once-off event that makes the language better for years to come,and it ought to mean that changes in later versions are significantly smaller. ADVANCE WARNING #3:If you have not already read mywhat's new in Swift 2.2article,you should do so now –everything that I said was deprecated there has been removed,including ++,--,C-style for loops,tuple splat syntax,and more. All function parameters have labels unless you request otherwiseThe way we call functions and methods already changed in Swift 2.0,but it's changing again and this time it's going to breakeverything. In Swift 2.x and earlier,method names did not require a label for their first parameter,so the name of the first parameter was usually built into the method name. For example: <code class=" language-swift" style="font-family: Consolas,monospace; padding: 0px; direction: ltr; word-spacing: normal; word-break: normal; word-wrap: normal; line-height: 1.5; background-image: none; background-position: initial initial; background-repeat: initial initial;">names<span class="token punctuation" style="color:#999999;">.</span><span class="token function" style="color:#dd4a68;">indexOf</span><span class="token punctuation" style="color:#999999;">(</span><span class="token string" style="color:#66990;">"Taylor"</span><span class="token punctuation" style="color:#999999;">)</span> <span class="token string" style="color:#66990;">"Taylor"</span><span class="token punctuation" style="color:#999999;">.</span><span class="token function" style="color:#dd4a68;">writeToFile</span><span class="token punctuation" style="color:#999999;">(</span><span class="token string" style="color:#66990;">"filename"</span><span class="token punctuation" style="color:#999999;">,</span> atomically<span class="token punctuation" style="color:#999999;">:</span> <span class="token boolean" style="color:#99055;">true</span><span class="token punctuation" style="color:#999999;">,</span> encoding<span class="token punctuation" style="color:#999999;">:</span> <span class="token builtin" style="color:#66990;">NSUTF8StringEncoding</span><span class="token punctuation" style="color:#999999;">)</span> <span class="token builtin" style="color:#66990;">SKAction</span><span class="token punctuation" style="color:#999999;">.</span><span class="token function" style="color:#dd4a68;">rotateByAngle</span><span class="token punctuation" style="color:#999999;">(</span><span class="token function" style="color:#dd4a68;">CGFloat</span><span class="token punctuation" style="color:#999999;">(</span><span class="token builtin" style="color:#66990;">M_PI_2</span><span class="token punctuation" style="color:#999999;">)</span><span class="token punctuation" style="color:#999999;">,</span> duration<span class="token punctuation" style="color:#999999;">:</span> <span class="token number" style="color:#99055;">10</span><span class="token punctuation" style="color:#999999;">)</span> <span class="token builtin" style="color:#66990;">UIFont</span><span class="token punctuation" style="color:#999999;">.</span><span class="token function" style="color:#dd4a68;">preferredFontForTextStyle</span><span class="token punctuation" style="color:#999999;">(</span><span class="token builtin" style="color:#66990;">UIFontTextStyleSubheadline</span><span class="token punctuation" style="color:#999999;">)</span> <span class="token keyword" style="color:#077aa;">override</span> <span class="token keyword" style="color:#077aa;">func</span> <span class="token function" style="color:#dd4a68;">numberOfSectionsInTableView</span><span class="token punctuation" style="color:#999999;">(</span>tableView<span class="token punctuation" style="color:#999999;">:</span> <span class="token builtin" style="color:#66990;">UITableView</span><span class="token punctuation" style="color:#999999;">)</span> <span class="token operator" style="color:#a67f59;">-</span><span class="token operator" style="color:#a67f59;">></span> <span class="token builtin" style="color:#66990;">Int</span> <span class="token keyword" style="color:#077aa;">func</span> <span class="token function" style="color:#dd4a68;">viewForZoomingInScrollView</span><span class="token punctuation" style="color:#999999;">(</span>scrollView<span class="token punctuation" style="color:#999999;">:</span> <span class="token builtin" style="color:#66990;">UIScrollView</span><span class="token punctuation" style="color:#999999;">)</span> <span class="token operator" style="color:#a67f59;">-</span><span class="token operator" style="color:#a67f59;">></span> <span class="token builtin" style="color:#66990;">UIView</span><span class="token operator" style="color:#a67f59;">?</span> <span class="token builtin" style="color:#66990;">NSTimer</span><span class="token punctuation" style="color:#999999;">.</span><span class="token function" style="color:#dd4a68;">scheduledTimerWithTimeInterval</span><span class="token punctuation" style="color:#999999;">(</span><span class="token number" style="color:#99055;">0.35</span><span class="token punctuation" style="color:#999999;">,</span> target<span class="token punctuation" style="color:#999999;">:</span> <span class="token keyword" style="color:#077aa;">self</span><span class="token punctuation" style="color:#999999;">,</span> selector<span class="token punctuation" style="color:#999999;">:</span> #<span class="token function" style="color:#dd4a68;">selector</span><span class="token punctuation" style="color:#999999;">(</span>createEnemy<span class="token punctuation" style="color:#999999;">)</span><span class="token punctuation" style="color:#999999;">,</span> userInfo<span class="token punctuation" style="color:#999999;">:</span> <span class="token constant" style="color:#99055;">nil</span><span class="token punctuation" style="color:#999999;">,</span> repeats<span class="token punctuation" style="color:#999999;">:</span> <span class="token boolean" style="color:#99055;">true</span><span class="token punctuation" style="color:#999999;">)</span></code> Swift 3 makes all labels required unless you specify otherwise,which means the method names no longer detail their parameters. In practice,this often means the last part of the method name gets moved to be the name of the first parameter. To show you how that looks,here is that Swift 2.2 code followed by its equivalent in Swift 3:
Got that? It's no surprise that Swift's rules are expressed using lingustic terminology –it is after all a language! – but this at least gives me a chance to feel smug that I did a second degree in English. What it means is that many methods are changing names in subtle and sometimes confusing ways. Let's start with a couple of simple examples:
|
- 机房收费系统之上机扣费
- Ruby String#to_class
- objective-c – IBOutlet Work的readonly属性是否优先?
- c# – UrlHelper.Route和UrlHelper.Link方法有什么区别?
- 安装Oracle时出现环境变量Path的值大于1023的解决办法
- cocos2d-x多分辨率适配方案:setDesignResolutionSize详解
- 什么是约束布局在Flutter中的等价物?
- ruby-on-rails – 如何使用RSpec和Rails 4测试子域约束
- Item 31 减小文件之间的编译依赖
- .net – 为什么try / catch没有捕获访问冲突?