CocoaPods:使用swift编写的本地pod

时间:2015-03-17 13:58:59

标签: ios swift cocoapods

我想使用Swift项目。作为其他Swift项目中的本地pod。

我的pod只有一个带有UIImageView扩展名的swift文件:

//
// UIImageView+loader.swift
//

import UIKit

extension UIImageView {

    func loadWithUrlString(urlString: String) {
        var url = NSURL(string: urlString)
        if url != nil {
            self.loadWithUrl(url!)
        }
    }

    func loadWithUrl(url : NSURL) {
        let session = NSURLSession.sharedSession()
        let urlRequest = NSURLRequest(URL: url)
        session.dataTaskWithRequest(urlRequest, completionHandler: { (data:NSData!, response:NSURLResponse!, error: NSError!) -> Void in
            println("response")
        })
    }
}

我的Podfile:

# Podfile
platform :ios, '8.0'
pod 'WSImageLoader', :path=>'~/untitled folder/WSImageLoader'

use_frameworks!

我的ViewController

//
//  ViewController.swift
//

import UIKit
import WSImageLoader

class ViewController: UIViewController {

    @IBOutlet weak var imageView: UIImageView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // does not work here ('UIImageView' does not have a member named 'loadWithUrl')
        self.imageView.loadWithUrl("http://www.axialis.com/tutorials/iw/down.ico")
    }
}

Project Screenshot

Pod安装并编译proj。工作,但如何使用 来自pod的 loadWithUrl

pod --version
0.36.0

Podspec

# WSImageLoader.podspec
Pod::Spec.new do |s|

  s.name         = "WSImageLoader"
  s.version      = "0.0.1"
  s.summary      = "A short description of WSImageLoader."
  s.description  = ""
  s.homepage     = "http://EXAMPLE/WSImageLoader"
  s.license      = "MIT (example)"
  s.author             = { "" => "" }
  s.platform     = :ios, "8.0"
  s.source       = { :git => "http://EXAMPLE/WSImageLoader.git", :tag => "0.0.1" }
  s.source_files  = "WSImageLoader/Classes/**/*.{swift}"
  s.exclude_files = "Classes/Exclude"

  s.public_header_files = "Classes/**/*.swift"

end

1 个答案:

答案 0 :(得分:0)

您已在swift中创建了扩展程序。 Swift目前不支持Swift文件,如下所述:https://github.com/CocoaPods/CocoaPods/issues/2218

因此,要使用以swift(UIImageView + loader.swift)编写的扩展文件,只需将其写入目标c,下面的过程将帮助您访问该函数。

您必须从Local Pod导入头文件。本教程帮助我成功导入其他Pods文件。

https://medium.com/@kirualex/cocoapods-with-swift-e6f8ba8f0afc

希望这也有助于你。