前言:
Swift项目,为适配iOS10,无奈只能更新Xcode 8 ,可是发现一入3.0深似海,从此幸福是路人.于是边摸索边修改,在节前的完成代码迁移.节后在完成手头工作后,整理思路,把swift3迁移的心得分享大家.
废话不多说分享下心得:
1、代码篇:(一定要做好备份!!!)
1>升级Xcode8正式版(8A218a).
2>使用系统自带的桥接工具.(原装的就是好,直接打开2.2项目也会提醒->步骤:3)
Edit > Convert > To Current Swift Syntax..
3>不用说就是为3.0来的,选择Swift3.
4>上面的框架就不用选了,只选择.app的.(这个是检测代码,不然你会哭的)
5>检测完后.
在左边的选择页中选择好要转移的代码,左边的代码是swift3.0代码,右侧代码时swift2.2代码->点Save.
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.
在pod的配置文件里面的Use Legacy Swift Language Version选择YES错误就消除了
Pods->TARGETS->指定框架 -> Build Settings -> Use Legacy Swift Language Version
PROJRCT-> Build Settings -> Build Active Architecture only->YES
其他问题后续收集再整理...
2>发现框架过时,需要更新框架.
由于3.0的更新,swift各个主流框架都已经及时的推出了新的版本,例如:Alamofire 4.0.0 , SnapKit 3.0.0 , Kingfisher 3.1.0 , SwiftyJSON 也已经更新了专供Swift3的分支.
所以,要更新框架以供使用.
<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>
可能会出现以下问题:
看到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心得体会:
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
本文分享了从Swift 2.2迁移到Swift 3.0的过程中遇到的问题及解决办法,包括使用Xcode 8进行代码转换、解决第三方框架兼容性问题等。
5990

被折叠的 条评论
为什么被折叠?



