1

I'm currently trying to use User Defined Runtime Attributes in Xcode 6 with the following algorithm:

  1. Add custom properties to UIView class using Associated Objects

    #define ASSOCIATED_OBJECT_GETTER_AND_SETTER(propertyType, propertyName, propertyNameWithCapital, associationType)   \
    -(void)set##propertyNameWithCapital:(propertyType)_value                                                            \
    {                                                                                                                   \
        objc_setAssociatedObject(self, @selector(propertyName), _value, associationType);                               \
    }                                                                                                                   \
    -(propertyType)propertyName                                                                                         \
    {                                                                                                                   \
        return objc_getAssociatedObject(self, @selector(propertyName));                                                 \
    }                                                                                                                   \                                       
    
    @interface eMyTags : NSObject
    @property (nonatomic) NSString* name;
    @end
    
    @implementation eMyTags
    @synthesize name;
    @end
    
    @interface UIView (MyTags)
    @property (nonatomic) eMyTags* myTags;
    @property (nonatomic) NSString* myName;
    @end
    
    @implementation UIView (MyTags)
    @dynamic myTags, myName;
    ASSOCIATED_OBJECT_GETTER_AND_SETTER(eMyTags*, myTags, MyTags, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    ASSOCIATED_OBJECT_GETTER_AND_SETTER(NSString*, myName, MyName, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    @end
    
  2. Set values of these properties through Xcode storyboard enter image description here

  3. Access these properties in code during runtime

    -(void)viewDidLoad
    {
        [super viewDidLoad];
        NSLog(@"myName: %@", view.myName);
        NSLog(@"myTags.name: %@", view.myTags.name);
    }
    

When i compile and run the output is:

myName: bla
myTags.name: (null)

So why myTags.name is not set? What did i miss? Can't i set User Defined Runtime Attributes of custom types?

Alexander
  • 959
  • 5
  • 11
  • 29
  • did you manage to find a solution for this? – Dani Mar 17 '17 at 10:07
  • @DaniA I have found the solution for my specific problem, but i don't even remember what was the actual problem and how i solved this :) It was 2 years ago and i haven't code on objective-c for a long time since then. I'm sorry. – Alexander Mar 19 '17 at 09:04

1 Answers1

1

Well myTags is not allocated in the memory and nor initialised. On the other hand, the value @"bla" is a string literal which is actually an instance of an NSString and points to a location in memory.

Burhanuddin Sunelwala
  • 5,318
  • 3
  • 25
  • 51
  • Oh i see now... So when should i initialize `myTags`? Or is it impossible to use custom types like this? – Alexander Apr 06 '15 at 17:50
  • Runtime attributes in xcode are used to set values for UIElements which are in turn allocated and initialized when the nib is loaded. http://spin.atomicobject.com/2014/05/30/xcode-runtime-attributes/ – Burhanuddin Sunelwala Apr 06 '15 at 18:10
  • 1
    Loading and initializing custom objects is not possible through this approach. You need to write code for that :). – Burhanuddin Sunelwala Apr 06 '15 at 18:12