2016-01-24 67 views
2

我遇到了一些Swift生成頭文件的問題。 Swift生成的頭文件中的Swift類不包含通過Cocoapods安裝的任何Swift框架的任何屬性或方法。Swift生成的頭文件丟失了外部的swift框架

這是我正在做的。

  1. 我有一個純粹的Objective-C的XCode項目。

  2. 我添加了一個Swift類(Model.swift)到處理我的網絡調用和同步JSON響應到CoreData的項目。 Swift班正在使用Alamofire來完成所有的網絡通話。

  3. 我安裝的XCode構建設置正確生成我的項目名 - Swift.h

    • 定義模塊是
    • 高附加值產品模塊名稱
  4. 當我查看我的Model.swift類在Swift生成的頭文件中,函數調用缺失。缺少的函數調用返回來自Alamofire的請求對象。所以看起來,當Xcode構建Swift頭文件時,它無法看到Alamofire。

這是我檢查過的一些東西。

  1. 我的Model.swift是公開的。類被標記爲公開的,我想要公開的功能是公開的。生成的Swift標題將包含標記爲公共的任何內容。
  2. 我從Pods構建設置中檢查了Alamofire,框架是公共的,Define Modules設置爲YES。它也有它自己生成的'Alamofire-Swift.h'。

我跑的XCode 7.2與iOS 8的最低部署目標

我使用的CocoaPods 0.39

這裏是我的CocoaPods的樣本。

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

是否還有什麼我缺少的Swift頭從外部的Swift框架生成代碼?

我的下一步將創建一個更小的項目來測試。但是,如果這是不可能的,或者我錯過了一些我想知道的東西。

感謝所有

更新 - 我只是在Objective-C創建一個較小的Xcode項目,所以你可以看到我的Model.swift的縮小測試版本和生成的頭。

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 
    } 
} 

這裏是從Xcode生成的swift頭文件。如果你滾動到底部,你會看到生成的Model.swift會丟失2個需要Alamofire依賴的函數。 simpleRequest和requestJSON丟失。

// Generated by Apple Swift version 2.1.1 (swiftlang-700.1.101.15 clang-700.1.81) 
#pragma clang diagnostic push 

#if defined(__has_include) && __has_include(<swift/objc-prologue.h>) 
# include <swift/objc-prologue.h> 
#endif 

#pragma clang diagnostic ignored "-Wauto-import" 
#include <objc/NSObject.h> 
#include <stdint.h> 
#include <stddef.h> 
#include <stdbool.h> 

#if defined(__has_include) && __has_include(<uchar.h>) 
# include <uchar.h> 
#elif !defined(__cplusplus) || __cplusplus < 201103L 
typedef uint_least16_t char16_t; 
typedef uint_least32_t char32_t; 
#endif 

typedef struct _NSZone NSZone; 

#if !defined(SWIFT_PASTE) 
# define SWIFT_PASTE_HELPER(x, y) x##y 
# define SWIFT_PASTE(x, y) SWIFT_PASTE_HELPER(x, y) 
#endif 
#if !defined(SWIFT_METATYPE) 
# define SWIFT_METATYPE(X) Class 
#endif 

#if defined(__has_attribute) && __has_attribute(objc_runtime_name) 
# define SWIFT_RUNTIME_NAME(X) __attribute__((objc_runtime_name(X))) 
#else 
# define SWIFT_RUNTIME_NAME(X) 
#endif 
#if defined(__has_attribute) && __has_attribute(swift_name) 
# define SWIFT_COMPILE_NAME(X) __attribute__((swift_name(X))) 
#else 
# define SWIFT_COMPILE_NAME(X) 
#endif 
#if !defined(SWIFT_CLASS_EXTRA) 
# define SWIFT_CLASS_EXTRA 
#endif 
#if !defined(SWIFT_PROTOCOL_EXTRA) 
# define SWIFT_PROTOCOL_EXTRA 
#endif 
#if !defined(SWIFT_ENUM_EXTRA) 
# define SWIFT_ENUM_EXTRA 
#endif 
#if !defined(SWIFT_CLASS) 
# if defined(__has_attribute) && __has_attribute(objc_subclassing_restricted) 
# define SWIFT_CLASS(SWIFT_NAME) SWIFT_RUNTIME_NAME(SWIFT_NAME) __attribute__((objc_subclassing_restricted)) SWIFT_CLASS_EXTRA 
# define SWIFT_CLASS_NAMED(SWIFT_NAME) __attribute__((objc_subclassing_restricted)) SWIFT_COMPILE_NAME(SWIFT_NAME) SWIFT_CLASS_EXTRA 
# else 
# define SWIFT_CLASS(SWIFT_NAME) SWIFT_RUNTIME_NAME(SWIFT_NAME) SWIFT_CLASS_EXTRA 
# define SWIFT_CLASS_NAMED(SWIFT_NAME) SWIFT_COMPILE_NAME(SWIFT_NAME) SWIFT_CLASS_EXTRA 
# endif 
#endif 

#if !defined(SWIFT_PROTOCOL) 
# define SWIFT_PROTOCOL(SWIFT_NAME) SWIFT_RUNTIME_NAME(SWIFT_NAME) SWIFT_PROTOCOL_EXTRA 
# define SWIFT_PROTOCOL_NAMED(SWIFT_NAME) SWIFT_COMPILE_NAME(SWIFT_NAME) SWIFT_PROTOCOL_EXTRA 
#endif 

#if !defined(SWIFT_EXTENSION) 
# define SWIFT_EXTENSION(M) SWIFT_PASTE(M##_Swift_, __LINE__) 
#endif 

#if !defined(OBJC_DESIGNATED_INITIALIZER) 
# if defined(__has_attribute) && __has_attribute(objc_designated_initializer) 
# define OBJC_DESIGNATED_INITIALIZER __attribute__((objc_designated_initializer)) 
# else 
# define OBJC_DESIGNATED_INITIALIZER 
# endif 
#endif 
#if !defined(SWIFT_ENUM) 
# define SWIFT_ENUM(_type, _name) enum _name : _type _name; enum SWIFT_ENUM_EXTRA _name : _type 
#endif 
typedef float swift_float2 __attribute__((__ext_vector_type__(2))); 
typedef float swift_float3 __attribute__((__ext_vector_type__(3))); 
typedef float swift_float4 __attribute__((__ext_vector_type__(4))); 
typedef double swift_double2 __attribute__((__ext_vector_type__(2))); 
typedef double swift_double3 __attribute__((__ext_vector_type__(3))); 
typedef double swift_double4 __attribute__((__ext_vector_type__(4))); 
typedef int swift_int2 __attribute__((__ext_vector_type__(2))); 
typedef int swift_int3 __attribute__((__ext_vector_type__(3))); 
typedef int swift_int4 __attribute__((__ext_vector_type__(4))); 
#if defined(__has_feature) && __has_feature(modules) 
@import ObjectiveC; 
#endif 

#pragma clang diagnostic ignored "-Wproperty-attribute-mismatch" 
#pragma clang diagnostic ignored "-Wduplicate-method-arg" 

SWIFT_CLASS("_TtC6MyTest5Model") 
@interface Model : NSObject 
@property (nonatomic, copy) NSString * __nullable myPropertyA; 
@property (nonatomic) NSInteger myPropertyB; 
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER; 
- (NSString * __nullable)myPublicFunctionA; 
- (NSInteger)myPublicFunctionB; 
@end 

#pragma clang diagnostic pop 
+0

我遇到了同樣的問題 - 您是否找到解決方案? –

回答

0

我有同樣的問題,解決它加入指令@objc公共功能之前目標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 
} 

...

我希望能提供幫助。