最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Objective

运维笔记admin32浏览0评论

Objective

Objective

Objective-C中的swift类:未知接收器(swift class in Objective-C: unknown receiver)

我在swift文件中写了一个类:

class UtilityMethods { class func userId() -> Integer { ... } class func setUserId(userId : Int) { ... }}

我正在导入-swift.h-header编译好,但是我无法使用

[UtilityMethods userId];

在我的Objective-C代码中:

Unknown receiver 'UtilityMethods'; did you mean 'UtilMethods'?

UtilMethods是一个我想要替换的Objective-C类。 我错过了什么吗?

编辑在Lance的帮助下,该类现在已被识别,但不幸的是,getter方法不是头文件如下所示:

SWIFT_CLASS("_TtC15...14UtilityMethods")@interface UtilityMethods : NSObject+ (void)setUserId:(NSInteger)userId;- (instancetype)init OBJC_DESIGNATED_INITIALIZER;@end

为什么吸气剂会丢失?

I wrote a class in a swift-file:

class UtilityMethods { class func userId() -> Integer { ... } class func setUserId(userId : Int) { ... }}

I'm importing the -swift.h-header which compiles fine, but I can't use

[UtilityMethods userId];

in my Objective-C code:

Unknown receiver 'UtilityMethods'; did you mean 'UtilMethods'?

UtilMethodsis an Objective-C class I'd like to replace. Am I missing something?

EDIT With the help of Lance, the class is now recognized, but the getter method isn't, unfortunately, the header files looks like the following:

SWIFT_CLASS("_TtC15...14UtilityMethods")@interface UtilityMethods : NSObject+ (void)setUserId:(NSInteger)userId;- (instancetype)init OBJC_DESIGNATED_INITIALIZER;@end

Why is the getter missing?

最满意答案

为了让Objective C可以使用Swift类,您有两个选择:

选项1:子类NSObject(或其他一些Objective C类)

class UtilityMethods : NSObject { class func userId() -> Int { ... } class func setUserId(userId: Int) { ... }}

选项2:将@objc属性添加到类中,告诉Swift编译器创建一个Objective C对象,该对象使用动态分派而不是静态分派方法调用

@objc class UtilityMethods { class func userId() -> Int { ... } class func setUserId(userId: Int) { ... }}

In order to have a Swift class available to Objective C you have two options:

Option 1: Subclass NSObject (or some other Objective C class)

class UtilityMethods : NSObject { class func userId() -> Int { ... } class func setUserId(userId: Int) { ... }}

Option 2: Add the @objc attribute to your class telling the Swift compiler to make an Objective C object that uses dynamic dispatch rather than static dispatch for method calls

@objc class UtilityMethods { class func userId() -> Int { ... } class func setUserId(userId: Int) { ... }}

发布评论

评论列表(0)

  1. 暂无评论