CocoaPods with Swift

作者: Mcccc | 来源:发表于2016-11-02 14:16 被阅读223次

    前言

    最近,经小组讨论,打算使用Swift3.0开发下一个新项目,碍于很多第三方类库都是OC的类库,所以类库方面只能慢慢从OC类库过渡到Swift类库。
    下面会简述pods中导入Swift库的过程,Let’s go!

    更新pods

    以ObjectMapper第三方开源类库为例
    ObjectMapper是一个基于Swfit语言实现JSON和Model间互转的类库
    传送门:https://github.com/Hearst-DD/ObjectMapper

    ObjectMapper 官方说明,类库最新版本是pod 'ObjectMapper', '~> 2.2'
    CocoaPods版本需要 0.36 or later

    然后我打开终端搜索ObjectMapper,输入pod search objectmapper
    结果显示最新的ObjectMapper版本号是1.3.0
    (LOL显然不对,官方说最新的2.2,但是对CocoaPods版本需求是0.36 or later)

    应该是CocoaPods版本问题,升级CocoaPods!
    终端输入:$ sudo gem update cocoapods 
    

    pod install

    终端输入:
    1、cd 你的项目profile文件路径
    2、pod install
    到这里,发现返回

    Analyzing dependencies
    [!] The dependency `ObjectMapper (~> 2.2.0)` is not used in any concrete target.
    
    [!] Your Podfile has had smart quotes sanitised. To avoid issues in the future, you should not use TextEdit for editing it. If you are not using TextEdit, you should turn off smart quotes in your editor of choice.
    

    网上资料说,Podfile不要使用TextEdit(文本形式编辑),会出现问题,请尽量使用xcode或者终端编辑。

    • 可能是语法问题?

    解决方法:
    还是使用TextEdit打开Podfile,语法更改如下

    platform :ios,’8.0’
    
    #兼容swift库
    use_frameworks!
    
    #导入所需库
    target 'SmartHome' do
    pod 'AFNetworking'
    pod 'ObjectMapper', '~> 2.2.0'
    end
    
    #适配当前xcode8.1 swift3.0版本,更新库指定swift3.0
        installer.pods_project.targets.each do |target|
            target.build_configurations.each do |config|
                config.build_settings['SWIFT_VERSION'] = '3.0'
            end
        end
    end
    

    ok,pod install !

    终端输入: pod install
    

    其中可能遇到会遇到的问题:

    1、pods兼容swift库

    [!] Pods written in Swift can only be integrated as frameworks; add `use_frameworks!` to your Podfile or target to opt into using it. The Swift Pods being used are: Alamofire and SwiftyJSON
    

    去stackoverflow搜了发现要在Podfile加上

    use_frameworks!
    

    2、swift类库版本问题

    更新swift类库后,运行xcode报错如下

    “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.
    

    原因:swift类库的版本,与当前xcode的swift版本不一致
    解决方法:

    #适配当前xcode8.1 swift3.0版本,更新库指定swift3.0
        installer.pods_project.targets.each do |target|
            target.build_configurations.each do |config|
                config.build_settings['SWIFT_VERSION'] = '3.0'
            end
        end
    end
    

    ha

    相关文章

      网友评论

        本文标题:CocoaPods with Swift

        本文链接:https://www.haomeiwen.com/subject/yvsvuttx.html