如何使用 Swift 从 Objective-C 启动实例类型
How can I initiate an instancetype from Objective-C with Swift
我有一个 api,我必须将它从 Objective-C 翻译成 Swift。
我被某种我真的不知道的构造函数或初始化所困。
.h 文件是这样的:
+ (instancetype) newProductionInstance;
+ (instancetype) newDemoInstance;
.m 文件是这样的:
+ (instancetype) newProductionInstance
{
return [[self alloc] initWithBaseURLString:productionURL];
}
+ (instancetype) newDemoInstance
{
return [[self alloc] initWithBaseURLString:demoURL];
}
- (instancetype)initWithBaseURLString:(NSString *)urlString
{
if (self = [self init])
{
_apiURL = [NSURL URLWithString:urlString];
}
return self;
}
这是他们对我正在翻译的主文件的调用:
mobileApi = [MobileAPI newDemoInstance];
所以我只想将最后一行转换为 Swift 2.
var mobileApi = MobileAPI.newDemoInstance()
或
let mobileApi = MobileAPI.newDemoInstance()
如果您不打算修改它。
简直就是MobileAPI.newDemoInstance()
.
let mobileApi = MobileAPI.newDemoInstance()
注意:不要忘记在 Bridging-Header.h
文件中导入 MobileAPI.h
。
希望对您有所帮助
class YourClass: NSObject {
//Class level constants
static let productionURL = "YourProductionURL"
static let demoURL = "YourDemoURL"
//Class level variable
var apiURL : String!
//Static factory methods
static func newProductionInstance() -> YourClass {
return YourClass(with : YourClass.productionURL)
}
static func newDemoInstance() -> YourClass {
return YourClass(with : YourClass.demoURL)
}
// Init method
convenience init(with baseURLString : String) {
self.init()
self.apiURL = baseURLString
//Calling
let yourObject : YourClass = YourClass.newDemoInstance()
}
}
在 objective-c 中创建适用于 objective-c 和 Swift 的实例类型的最佳选项您应该使用 "default"关键词。此关键字已用于 Apple 的标准库中。例如 NSNotificationCenter.default 或 NSFileManager.default。要在 .h 文件中声明它,你应该写
+(instancetype) default;
并在您的 .m 文件中
static YOUR_CLASS_NAME *instance = nil;
+(instancetype) default {
if instance == nil { instance = [[super allocWithZone:NULL] init];}
return instance;
}
我有一个 api,我必须将它从 Objective-C 翻译成 Swift。 我被某种我真的不知道的构造函数或初始化所困。
.h 文件是这样的:
+ (instancetype) newProductionInstance;
+ (instancetype) newDemoInstance;
.m 文件是这样的:
+ (instancetype) newProductionInstance
{
return [[self alloc] initWithBaseURLString:productionURL];
}
+ (instancetype) newDemoInstance
{
return [[self alloc] initWithBaseURLString:demoURL];
}
- (instancetype)initWithBaseURLString:(NSString *)urlString
{
if (self = [self init])
{
_apiURL = [NSURL URLWithString:urlString];
}
return self;
}
这是他们对我正在翻译的主文件的调用:
mobileApi = [MobileAPI newDemoInstance];
所以我只想将最后一行转换为 Swift 2.
var mobileApi = MobileAPI.newDemoInstance()
或
let mobileApi = MobileAPI.newDemoInstance()
如果您不打算修改它。
简直就是MobileAPI.newDemoInstance()
.
let mobileApi = MobileAPI.newDemoInstance()
注意:不要忘记在 Bridging-Header.h
文件中导入 MobileAPI.h
。
希望对您有所帮助
class YourClass: NSObject {
//Class level constants
static let productionURL = "YourProductionURL"
static let demoURL = "YourDemoURL"
//Class level variable
var apiURL : String!
//Static factory methods
static func newProductionInstance() -> YourClass {
return YourClass(with : YourClass.productionURL)
}
static func newDemoInstance() -> YourClass {
return YourClass(with : YourClass.demoURL)
}
// Init method
convenience init(with baseURLString : String) {
self.init()
self.apiURL = baseURLString
//Calling
let yourObject : YourClass = YourClass.newDemoInstance()
}
}
在 objective-c 中创建适用于 objective-c 和 Swift 的实例类型的最佳选项您应该使用 "default"关键词。此关键字已用于 Apple 的标准库中。例如 NSNotificationCenter.default 或 NSFileManager.default。要在 .h 文件中声明它,你应该写
+(instancetype) default;
并在您的 .m 文件中
static YOUR_CLASS_NAME *instance = nil;
+(instancetype) default {
if instance == nil { instance = [[super allocWithZone:NULL] init];}
return instance;
}