gpt4 book ai didi

objective-c - 类名上的 @objc 注释与类本身

转载 作者:可可西里 更新时间:2023-11-01 01:08:54 25 4
gpt4 key购买 nike

假设我有一个名为 ExampleClass 的类。

假设我这样写代码:

@objc class ExampleClass: NSObject {}

像这样的 ObjectiveC 文件头:

#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>

@interface RCT_EXTERN_MODULE(ExampleClass, NSObject)
@end

然后我在我的 React Native 应用程序中使用它,如下所示:console.log('exampleClass', React.NativeModules.ExampleClass);

现在使用 react-native run-ios 编译会产生以下异常:

❌  Undefined symbols for architecture x86_64
> Symbol: `_OBJC_CLASS_$_ExampleClass`
> Referenced from: `l_OBJC_$_CATEGORY_ExampleClass_$_RCTExternModule in ExampleClass-2517576D7E90370.o



❌ ld: symbol(s) not found for architecture x86_64



❌ clang: error: linker command failed with exit code 1 (use -v to see invocation)


** BUILD FAILED **

虽然只改变 swift 以便它读取:

@objc(ExampleClass)
class ExampleClass: NSObject {}

编译成功。这两种语法有什么区别?为什么后者有效而前者无效?

最佳答案

@objc 属性的参数指定了 Swift 将导出到 Objective C 的名称。

Configuring Swift Interfaces in Objective-C

In some cases, you need finer grained control over how your Swift API is exposed to Objective-C. You can use the @objc(name) attribute to change the name of a class, property, method, enumeration type, or enumeration case declaration in your interface as it’s exposed to Objective-C code.

For example, if the name of your Swift class contains a character that isn’t supported by Objective-C, you can provide an alternative name to use in Objective-C. If you provide an Objective-C name for a Swift function, use Objective-C selector syntax. Remember to add a colon (:) wherever a parameter follows a selector piece.

来自 Using Swift with Cocoa and Objective-C (Swift 4.1) > Swift Type Compatibility > Configuring Swift Interfaces in Objective-C

关于objective-c - 类名上的 @objc 注释与类本身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50139526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com