Swift生成的标头缺少外部swift框架

时间:2016-01-24 17:03:53

标签: ios objective-c xcode

我遇到了Swift生成标题的一些问题。我在Swift生成的标题中的Swift类不包括通过Cocoapods安装的任何Swift框架中的任何属性或方法。

这就是我正在做的事情。

  1. 我有一个纯Objective-C的XCode项目。

  2. 我在项目中添加了一个Swift类(Model.swift)来处理我的网络调用并将JSON响应同步到CoreData。 Swift课程正在使用Alamofire进行所有网络通话。

  3. 我设置XCode构建设置以正确生成ProjectName-Swift.h

    • 定义模块是
    • 添加了产品模块名称
  4. 当我在Swift生成的头文件中查看我的Model.swift类时,缺少函数调用。缺少的函数调用返回来自Alamofire的请求对象。因此,当Xcode构建Swift标头时,它似乎无法看到Alamofire。

  5. 以下是我检查过的一些事情。

    1. 我的Model.swift是公开的。类被标记为公共,我想要公开的函数是公共的。生成的Swift标头将包含标记为public的任何内容。
    2. 我从Pods构建设置检查了Alamofire,框架是公共的,Define Modules设置为YES。它也有自己生成的Alamofire-Swift.h'。
    3. 我正在运行XCode 7.2,其最低部署目标是iOS 8

      我正在使用cocoapods 0.39

      以下是我的Cocoapods的样本。

      $notifications = Notifications::return_new()->getAttributes();
      

      为了让Swift头从外部Swift框架生成代码,还有什么我不知道的吗?

      我的下一步将是创建一个较小的项目进行测试。但是,如果这是不可能的,或者我想错过一些我想知道的事情。

      全部谢谢

      更新 - 我刚刚在Objective-C中创建了一个较小的Xcode项目,因此您可以看到我的Model.swift的缩小测试版本和生成的标题。

      source 'https://github.com/CocoaPods/Specs.git'
      platform :ios, '8.0' 
      use_frameworks!
      pod 'Alamofire', '3.1.2'
      

      这是从Xcode生成的swift标头。如果滚动到底部,您将看到生成的Model.swift将缺少2个需要Alamofire依赖的函数。缺少simpleRequest和requestJSON。

      import Foundation
      import Alamofire
      
      public class Model: NSObject
      {
          public var myPropertyA: String?
          public var myPropertyB: Int = 0
      
          public override init()
          {
              super.init()
          }
      
          deinit
          {
      
          }
      
          //MARK: public methods
      
          public func myPublicFunctionA() -> String?
          {
              return nil
          }
      
          public func myPublicFunctionB() -> Int
          {
              return 0
          }
      
          public func simpleRequest() -> Request?
          {
              return nil
          }
      
          public func requestJSON(method: Alamofire.Method, url: String?, parameters: [String: AnyObject]?, headers: [String: String]?, complete: ((json: AnyObject?, error: NSError?, errorOccurred: Bool) -> Void)?) -> Request?
          {
              var request: Request?
              request = Alamofire.request(method, url!, parameters: parameters, encoding: .URL, headers: headers).validate().responseJSON { response in
      
                  let json: AnyObject? = response.result.value
      
                  switch response.result
                  {
                  case .Success:
      
                      if (complete != nil)
                      {
                          complete?(json: json, error: nil, errorOccurred: false)
                      }
      
                      break
      
                  case .Failure(let error):
      
                      if (complete != nil)
                      {
                          complete?(json: json, error: error, errorOccurred: true)
                      }
      
                      break
                  }
              }
      
              return request
          }
      }
      

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,解决问题的方法是在公共函数之前添加指令 @objc 以在Objective C中使用。

例如:

import Foundation
import Alamofire

public class Model: NSObject
{
    public var myPropertyA: String?
    public var myPropertyB: Int = 0

public override init()
{
    super.init()
}

deinit
{

}

//MARK: public methods

@objc public func myPublicFunctionA() -> String?
{
    return nil
}

@objc public func myPublicFunctionB() -> Int
{
    return 0
}

@objc public func simpleRequest() -> Request?
{
    return nil
}

...

我希望能提供帮助。