classname 与 class 本身的 @objc 注释
@objc annotation on the classname vs the class itself
假设我有一个名为 ExampleClass
的 class。
假设我这样写代码:
@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.
假设我有一个名为 ExampleClass
的 class。
假设我这样写代码:
@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.