【问题标题】:Swift functions are not included into autogenerated -Swift.h umbrella fileSwift 函数不包含在自动生成的 -Swift.h 伞形文件中
【发布时间】:2018-03-21 09:24:27
【问题描述】:

我正在尝试按照本指南从 Objective-C 访问 Swift 类方法 https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

这是我的 Swift 课程:

import Foundation

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

这里是自动生成的“伞”文件 MyProductModuleName-Swift.h 的摘录

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

XCode 似乎完全忽略了 MySwiftClass 的方法,因此我无法从 Objective-C 访问它们。 Xcode 版本是 9.2。 是错误还是我遗漏了什么?

【问题讨论】:

    标签: objective-c swift xcode


    【解决方案1】:

    您需要在要从 Objective-C 访问的每个函数之前添加@objc

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

    【讨论】:

    • 这有帮助。谢谢!
    猜你喜欢
    • 2016-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    相关资源
    最近更新 更多