使用 objc_setAssociatedObject 时架构 armv7 的未定义符号 [英] Undefined symbols for architecture armv7 when working with objc_setAssociatedObject

查看:47
本文介绍了使用 objc_setAssociatedObject 时架构 armv7 的未定义符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我得到的运行时错误日志:

This is the run time error log I am getting:

Undefined symbols for architecture armv7:
  "_MyConstantKey", referenced from:
      -[LayoutViewController addLabelsAndButtonInBaseView:withLayoutObject:] in LayoutViewController.o
ld: symbol(s) not found for architecture armv7

我正在尝试将一个对象与 UIButton 选择器一起传递.

I am trying to pass an object along with UIButton selector.

这是我的代码:

这就是我声明 var 的方式:

this is how I am declaring a var:

extern const char MyConstantKey;
@interface LayoutViewController : UIViewController

我的实现文件:

#import <objc/runtime.h>

以及设置关联对象的相关代码:

and relevant code for setting associated object:

UIButton *componentButton = [[UIButton alloc] initWithFrame:baseView.bounds];
objc_setAssociatedObject(componentButton,
                         MyConstantKey,
                         layout,
                         OBJC_ASSOCIATION_RETAIN);
[componentButton addTarget:self
                action:@selector(componentButtonPressed:)
      forControlEvents:UIControlEventTouchUpInside];
[componentButton setTitle:@"" forState:UIControlStateNormal];

有什么建议吗?有没有我遗漏的框架,

any suggestion? Is there any framework which I am missing,

推荐答案

将变量声明为 extern 只会提示编译器该变量可能会在当前编译单元之外的某个地方创建.所以你需要在你的实现文件中实际创建它.另外我认为在这里使用 char* 而不是 char 会更合适:

Declaring variable as extern only prompts to compiler that variable will be created somewhere possibly outside of current compilation unit. So you need to actually create it in your implementation file. Also I think using char* instead of char here will be more appropriate:

// .h
extern const char* MyConstantKey;

// .m 
const char* MyConstantKey = "MyConstantKey"; 

…
objc_setAssociatedObject(componentButton,
                         (void*)MyConstantKey,
                         layout,
                         OBJC_ASSOCIATION_RETAIN);

这篇关于使用 objc_setAssociatedObject 时架构 armv7 的未定义符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆