【问题标题】:CocoaPods: use local pod written in swiftCocoaPods:使用 swift 编写的本地 pod
【发布时间】:2015-03-18 20:39:32
【问题描述】:

我想使用 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
platform :ios, '8.0'
pod 'WSImageLoader', :path=>'~/untitled folder/WSImageLoader'

use_frameworks!

我的视图控制器

//
//  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")
    }
}

Pod 安装和编译项目。有效,但如何使用 loadWithUrl 来自 pod?

pod --version
0.36.0

Pod 规格

# 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

【问题讨论】:

  • 你能解决这个问题吗?我遇到了同样的问题。

标签: ios swift cocoapods


【解决方案1】:

您已经在 swift 中创建了一个扩展程序。 Swift 中目前不支持 Swift 文件,这里也有讨论:https://github.com/CocoaPods/CocoaPods/issues/2218

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

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

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

希望这对你也有帮助。

【讨论】:

  • 我没有任何头文件。
  • 头文件表示 UIImageView+loader.swift。将此文件导入到您的头文件中,如教程所示。
  • 在教程中,他们使用用 ObjC 编写的 pod。
  • 但是你可以使用 swift 并包含 swift 文件吗?你试过包括它吗
  • 我只能这样做:导入 WSImageLoader,因为它是我的 pod proj 中的一个框架。 (见截图)。我不能这样做:导入 UIImageView+loader.swift
猜你喜欢
  • 2014-06-07
  • 1970-01-01
  • 2020-01-17
  • 2013-05-21
  • 1970-01-01
  • 2015-06-22
  • 2019-12-31
  • 1970-01-01
相关资源
最近更新 更多