24159

Undefined symbols for architecture armv7 when working with objc_setAssociatedObject

Question:

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

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

this is my code:

this is how I am declaring a var:

extern const char MyConstantKey; @interface LayoutViewController : UIViewController

my implementation file :

#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,

Answer1:

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);

Answer2:

Define your custom property like this way

#define kCustomProperty @"CustomProperty"

Associate your object with that custom property like below

objc_setAssociatedObject(myObj,kCustomProperty , myData, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

Get your data using the same property and object like below

NSObject *aObj = objc_getAssociatedObject(myObj, kCustomProperty);

Recommend

  • How to call GetGUIThreadInfo in c#
  • Explicit instantiation declaration with extern
  • C# Getting window's title by handle in Hebrew return question marks
  • Programmatically set PIN/Password used to unlock device
  • C++ reference variable across files
  • why my app based on boost::asio didn't accept new connection
  • Why are “sc.addFile” and “spark-submit --files” not distributing a local file to all workers?
  • Comparing user's facebook/twitter friends to site's users in Python/Django
  • Opaque reference instead of PImpl. Is it possible?
  • Why can't UI components be accessed from a backgroundworker?
  • Delphi: Where is the shortcut that started the application? [duplicate]
  • Is there a parser equivalent of 'fragment' marking in ANTLR4?
  • What's the syntax to inherit documentation from another indexer?
  • How do I mock an exported typescript function in a jasmine test?
  • abstracting over a collection
  • Can't remove headers after they are sent
  • Invalid Date on validation Date of js
  • C++ pointer value changes with static_cast
  • WPF ICommand CanExecute(): RaiseCanExecuteChanged() or automatic handling via DispatchTimer?
  • Firefox Extension - Monitor refresh and change of tab
  • C: Incompatible pointer type initializing
  • C# - Serializing and deserializing static member
  • AES padding and writing the ciphertext to a disk file
  • VS2008 Enable C++ Exception with SEH
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Google cloud sdk not working when python points python3
  • Matplotlib draw Spline from multiple points
  • Why winpcap requires both .lib and .dll to run?
  • Return words with double consecutive letters
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • How to delete a row from a dynamic generate table using jquery?
  • Python: how to group similar lists together in a list of lists?
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • using HTMLImports.whenReady not working in chrome
  • coudnt use logback because of log4j
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • Busy indicator not showing up in wpf window [duplicate]