前言:
Swift项目,为适配iOS10,无奈只能更新Xcode 8 ,可是发现一入3.0深似海,从此幸福是路人.于是边摸索边修改,在节前的完成代码迁移.节后在完成手头工作后,整理思路,把swift3迁移的心得分享大家.
废话不多说分享下心得:
1、代码篇:(一定要做好备份!!!)
1>升级Xcode8正式版(8A218a).
![](https://i-blog.csdnimg.cn/blog_migrate/f6e9f1a1c8da273a4a0781b353ef316d.webp?x-image-process=image/format,png)
2>使用系统自带的桥接工具.(原装的就是好,直接打开2.2项目也会提醒->步骤:3)
Edit > Convert > To Current Swift Syntax..
![](https://i-blog.csdnimg.cn/blog_migrate/5b74a2e7aec592351c99ce559ce88871.webp?x-image-process=image/format,png)
3>不用说就是为3.0来的,选择Swift3.
![](https://i-blog.csdnimg.cn/blog_migrate/e193273a04e9f9731aa0946f84a6ffdd.webp?x-image-process=image/format,png)
4>上面的框架就不用选了,只选择.app
的.(这个是检测代码,不然你会哭的)
![](https://i-blog.csdnimg.cn/blog_migrate/fe3de63d2ec61e2612d4469b65cb60c5.webp?x-image-process=image/format,png)
5>检测完后.
在左边的选择页中选择好要转移的代码,左边的代码是swift3.0代码,右侧代码时swift2.2代码->点Save
.
![](https://i-blog.csdnimg.cn/blog_migrate/e540fa4e6172f734ac621313a072d2c6.webp?x-image-process=image/format,png)
2、第三方框架篇:(使用的cocoapods管理的第三方框架)
1>发现框架报错.(不要怕,按照错误一样一样来)
“Use Legacy Swift Language Version” (SWIFT_VERSION) is required to be configured correctly for targets which use Swift. Use the [Edit > Convert > To Current Swift Syntax…] menu to choose a Swift version or use the Build Settings editor to configure the build setting directly.
![](https://i-blog.csdnimg.cn/blog_migrate/98a11f11fd6b89f0fecaebbca054498d.webp?x-image-process=image/format,png)
在pod的配置文件里面的Use Legacy Swift Language Version选择YES
错误就消除了
Pods
->TARGETS->指定框架
-> Build Settings
-> Use Legacy Swift Language Version
![](https://i-blog.csdnimg.cn/blog_migrate/f4f462c09909e9a55e008109413aaeee.webp?x-image-process=image/format,png)
![](https://i-blog.csdnimg.cn/blog_migrate/5351028ef82de27f2ba55f3cba59c70b.webp?x-image-process=image/format,png)
PROJRCT
-> Build Settings
-> Build Active Architecture only
->YES
![](https://i-blog.csdnimg.cn/blog_migrate/686d221a479f09579f236ecbdf47d207.webp?x-image-process=image/format,png)
其他问题后续收集再整理...
2>发现框架过时,需要更新框架.
由于3.0的更新,swift各个主流框架都已经及时的推出了新的版本,例如:Alamofire 4.0.0 , SnapKit 3.0.0 , Kingfisher 3.1.0 , SwiftyJSON 也已经更新了专供Swift3的分支.
所以,要更新框架以供使用.
![](https://i-blog.csdnimg.cn/blog_migrate/32315116525824d18bb4c112f3c2f6e0.webp?x-image-process=image/format,png)
<code class="php">pod <span class="hljs-string" style="color: rgb(0, 136, 0);">'SnapKit'</span>, <span class="hljs-string" style="color: rgb(0, 136, 0);">'~> 3.0.0'</span>
pod <span class="hljs-string" style="color: rgb(0, 136, 0);">'SwiftyJSON'</span>, :git => <span class="hljs-string" style="color: rgb(0, 136, 0);">'https://github.com/acegreen/SwiftyJSON.git'</span>, :branch => <span class="hljs-string" style="color: rgb(0, 136, 0);">'swift3'</span>
pod <span class="hljs-string" style="color: rgb(0, 136, 0);">'Alamofire'</span>,<span class="hljs-string" style="color: rgb(0, 136, 0);">'~> 4.0.0'</span>
pod <span class="hljs-string" style="color: rgb(0, 136, 0);">'Kingfisher'</span>,<span class="hljs-string" style="color: rgb(0, 136, 0);">'~> 3.1.0'</span></code>
可能会出现以下问题:
![](https://i-blog.csdnimg.cn/blog_migrate/f3ffcee72d039a6faf3e939063285d4f.webp?x-image-process=image/format,png)
看到200多个框架错误后凌乱了.都是3.0语法了,为什么还会报错,不在框架,在自己安装的cocoapods环境有关系.
解决方案:
需要更新最新环境到cocoapods 1.1.0.rc.2
<code class="sql">sudo gem <span class="hljs-operator"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">install</span> cocoapods <span class="hljs-comment" style="color: rgb(136, 0, 0);">--pre</span></span></code>
可以参照SnapKit的官方issues:
https://github.com/SnapKit/SnapKit/issues/300
如果没有安装 cocoapods可以参照之前写的博客.
安装 cocoapods 1.1.0心得体会:
![](https://i-blog.csdnimg.cn/blog_migrate/92acfb8537506da8a73d36028b05235f.webp?x-image-process=image/format,png)
Alamofire/Source/ServerTrustPolicy.swift:243:41: Use of unresolved identifier 'kSecTrustResultInvalid'
Alamofire的官方issues:
https://github.com/Alamofire/Alamofire/issues/1541
解决方案:
上面也提到了,更新最新的Alamofire框架.
<code class="bash">pod <span class="hljs-string" style="color: rgb(0, 136, 0);">'Alamofire'</span>,<span class="hljs-string" style="color: rgb(0, 136, 0);">'~> 4.0.0'</span></code>
可能还遇到的问题:
<code class="scheme">[<span class="hljs-keyword" style="color: rgb(0, 0, 136);">!</span>] Unable to satisfy the following requirements: - `Alamofire <span class="hljs-list">(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">~></span> <span class="hljs-number" style="color: rgb(0, 102, 102);">4.0</span>.0)</span>` required by `Podfile` Specs satisfying the `Alamofire <span class="hljs-list">(<span class="hljs-keyword" style="color: rgb(0, 0, 136);">~></span> <span class="hljs-number" style="color: rgb(0, 102, 102);">4.0</span>.0)</span>` dependency were found, but they required a higher minimum deployment target.</code>
需要在Podfile文件中指定最低系统9.0项目版本.platform :iOS,'9.0'
Alamofire的官方issues:
https://github.com/Alamofire/Alamofire/issues/1639
![](https://i-blog.csdnimg.cn/blog_migrate/5c016e9cfcff77588ed01759bd3bcd9e.webp?x-image-process=image/format,png)