我正在使用Swift开发一个框架,并且在尝试在Obj-C文件中使用我的.swift类时遇到了麻烦。
Product Module Name
确实是SDK NSObject
,因此包括必要的@objc
即使我已根据Apple的文档完成了所有必要的步骤,我仍然会收到编译错误Use of undeclared identifier 'GGNetworking'
。
实施和项目本身不会更简单。
#import <UIKit/UIKit.h>
#import "SDK-Swift.h"
@interface GGHomie : NSObject
@end
@implementation GGHomie
- (id)init
{
self = [super init];
if (self)
{
GGNetworking *network = [[GGNetworking alloc] init]; // <-- Compiler error here
}
return self;
}
@end
我知道我并不是唯一一个在摔跤Obj-C到Swift代码的人;)有人可以权衡这个吗?
SDK-Swift.h
// Generated by Swift version 1.0 (swift-600.0.51.4)
#if defined(__has_include) && __has_include(<swift/objc-prologue.h>)
# include <swift/objc-prologue.h>
#endif
#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 __cplusplus < 201103L
typedef uint_least16_t char16_t;
typedef uint_least32_t char32_t;
#endif
#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(SWIFT_CLASS_EXTRA)
# define SWIFT_CLASS_EXTRA
#endif
#if !defined(SWIFT_PROTOCOL_EXTRA)
# define SWIFT_PROTOCOL_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
# else
# define SWIFT_CLASS(SWIFT_NAME) SWIFT_RUNTIME_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
#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
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wproperty-attribute-mismatch"
#if defined(__has_feature) && __has_feature(modules)
#endif
#pragma clang diagnostic pop
GGNetworking.swift
import UIKit
import Alamofire
public class GGNetworking: NSObject {
public class var baseUrl:NSString! {
get {
return "http://www.google.com/mobile/api/v1/"
}
}
public class func getInImageAd(url:GGInImageAdUrl!, completionHandler:(AnyObject?, NSError?) -> Void) {
Alamofire.request(.GET, url.absoluteString!).response { (request, response, data, error) in
if (error != nil) {
println("An error occurred while getting an in-image ad (\(url.absoluteString)): \(error)")
completionHandler(nil, error)
} else {
completionHandler(data, nil)
}
}
}
public class func getInScreenAd(url:GGInScreenAdUrl!, completionHandler:(AnyObject?, NSError?) -> Void) {
Alamofire.request(.GET, url.absoluteString!).response { (request, response, data, error) in
if (error != nil) {
println("An error occurred while getting an in-screen ad (\(url.absoluteString)): \(error)")
completionHandler(nil, error)
} else {
completionHandler(data, nil)
}
}
}
}
最后,我的Build Phases设置的屏幕截图:
答案 0 :(得分:3)
GGNetworking
是非私人的(即internal
(默认)还是public
?GGNetworking
是否有非私有初始化程序?SDK-Swift.h
的内容是什么? Xcode实际上会在DerivedData
文件夹(~/Library/Developer/Xcode/DerivedData/...
)中的某个位置生成此文件。让Finder搜索它。GGNetworking
课程粘贴相关代码也可以帮助我们。在提供更多信息后更新:
将public override init() { super.init() }
添加到GGNetworking
是否可以解决问题?