Objective-C fast class: unknown receiver

I wrote a class in a swift file:

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

    class func setUserId(userId : Int) {
        ...
    }
}

I am importing the -swift.h header, which compiles fine, but I cannot use

[UtilityMethods userId];

in my Objective-C code:

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

UtilMethodsis the Objective-C class that I would like to replace. Did I miss something?

EDIT With Lance, the class is now recognized, but the getter method is not, unfortunately, the header file as follows:

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

Why is there a missing getter?

+4
source share
2 answers

To have a Swift class for 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) {
        ...
    }
}

2: @objc , Swift, Objective C, ,

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

    class func setUserId(userId: Int) {
        ...
    }
}
+3

, @objc , Objective-C.h , , .

0

Source: https://habr.com/ru/post/1544795/


All Articles