0
votes

I'm trying to access Swift class methods from Objective-C, following this guide https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

This is my Swift class:

import Foundation

@objc public class MySwiftClass: NSObject {
    // modifiers public, open do not help
    func Hello()->String {
        return "Swift says hello!";
    }
}

And here is an excerpt from the autogenerated "umbrella" file MyProductModuleName-Swift.h

SWIFT_CLASS("_TtC25_MyProductModuleName12MySwiftClass")
@interface MySwiftClass : NSObject
- (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
@end

It seems like XCode completely ignores methods of MySwiftClass, and as a result I can't access them from Objective-C. Xcode version is 9.2. Is it a bug or I missed anything?

1

1 Answers

3
votes

You need to add @objc before each function you want to access from Objective-C:

@objc public class MySwiftClass: NSObject {
    // modifiers public, open do not help
    @objc func Hello() -> String {
        return "Swift says hello!";
    }
}