在 Objective-C 中使用 Swift 对象 class
Using Swift object class in Objective-C
您好,我一直在研究与此相关的不同问题,none 的解决方案似乎对我有用。我有一个试图在 objective C 中使用的对象,但是在尝试初始化该对象时我一直得到 'No visible interface for object'。我已经将我的模块正确导入到 objective C 文件中并添加了 @objc 注释,我是否遗漏了什么?提前谢谢你!
代码:
Swift-
import UIKIT
@objc class SaveFile: NSOBject {
var name: String?
var location: String?
//@objc(initId:andSize:) does not work
init(id: String, size: String) {
super.init()
//other code
}
}
Objective C -
import "testProject-Swift.h"
// functions and initializers
-(void) saveFile {
SaveFile *fileToSave = [[SaveFile alloc] initWithId:self.currentId size:self.size] //No visible @interface for 'SaveFile' declares the selector 'initWithId:Size:'
}
试试这个:
@objcMembers
class SaveFile: NSObject {//<-NSObject, not NSOBject
var name: String?
var location: String?
init(id: String, size: String) {
super.init()
//other code
}
}
当您编写 Objective-C 代码中使用的 class 时,使用 @objcMembers
.
进行注释是一种简单的方法
否则,您可以使用 @objc
:
注释包括初始值设定项在内的所有属性和方法
class SaveFile: NSObject {
@objc var name: String?
@objc var location: String?
//@objc(initWithId:size:) do work
@objc init(id: String, size: String) {
super.init()
//other code
}
}
您好,我一直在研究与此相关的不同问题,none 的解决方案似乎对我有用。我有一个试图在 objective C 中使用的对象,但是在尝试初始化该对象时我一直得到 'No visible interface for object'。我已经将我的模块正确导入到 objective C 文件中并添加了 @objc 注释,我是否遗漏了什么?提前谢谢你!
代码:
Swift-
import UIKIT
@objc class SaveFile: NSOBject {
var name: String?
var location: String?
//@objc(initId:andSize:) does not work
init(id: String, size: String) {
super.init()
//other code
}
}
Objective C -
import "testProject-Swift.h"
// functions and initializers
-(void) saveFile {
SaveFile *fileToSave = [[SaveFile alloc] initWithId:self.currentId size:self.size] //No visible @interface for 'SaveFile' declares the selector 'initWithId:Size:'
}
试试这个:
@objcMembers
class SaveFile: NSObject {//<-NSObject, not NSOBject
var name: String?
var location: String?
init(id: String, size: String) {
super.init()
//other code
}
}
当您编写 Objective-C 代码中使用的 class 时,使用 @objcMembers
.
否则,您可以使用 @objc
:
class SaveFile: NSObject {
@objc var name: String?
@objc var location: String?
//@objc(initWithId:size:) do work
@objc init(id: String, size: String) {
super.init()
//other code
}
}