【问题标题】:Specifying an attribute for multiple Objective-C Methods declarations为多个 Objective-C 方法声明指定一个属性
【发布时间】:2020-12-01 07:51:44
【问题描述】:

通过咨询document,我知道您可以通过以下方式为C函数添加注释

#pragma clang attribute push (__attribute__((annotate("custom"))), apply_to = function)

void function(); // The function now has the annotate("custom") attribute
void function1(); 
void function2(); 
#pragma clang attribute pop

但是在Objective-C方法中会报错

错误信息:

@implementation STAppDelegate

#pragma clang attribute push (__attribute__((annotate("custom"))), apply_to = objc_method)

- (void)a {
    NSLog(@"%s", __func__);
}

- (void)b {
    NSLog(@"%s", __func__);
}

- (void)c {
    NSLog(@"%s", __func__);
}

#pragma clang attribute pop

@end

【问题讨论】:

  • 图片告诉我问题是你需要在 Objective-C 中将字符串写成@"string" 而不是"string"。与注释无关,但与 @ 修饰符无关。
  • 或使用fprintf(stderr,"string"); 而不是NSLog()
  • 对不起,我上传了错误的图片,现在我上传了正确的图片,我还是报错
  • 你:......上传了错误的图片......我:只是惩罚使用图片而不是代码。无论如何,它应该工作。检查您的编译器标志 - 必须是铿锵声。此外,push 和 pop 需要平衡。使用粘贴到我的代码中的确切推送和弹出行,这方面工作正常。
  • 我知道问题的原因了,我用的Xcode版本比较低,clang版本比较低,不支持

标签: c++ ios objective-c clang llvm


【解决方案1】:

它将适用于 Objective-C 类 interface 级别,例如

#pragma clang attribute push (__attribute__((annotate("custom"))), apply_to = objc_method)
@interface AppDelegate : UIResponder <UIApplicationDelegate>
-(void)yourMethod;
@end
#pragma clang attribute pop

#pragma clang attribute push(__attribute__((annotate("custom2"))), apply_to = objc_method)

@interface AppDelegate ()
-(void)yourMethod2;
@end

@interface AppDelegate ()
-(void)yourMethod3;
@end
#pragma clang attribute pop

【讨论】:

    猜你喜欢
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    • 2015-10-04
    • 1970-01-01
    • 1970-01-01
    • 2011-09-11
    相关资源
    最近更新 更多