For the following code snippet from ViewController.m:
- (IBAction)buttonAction:(id)sender
{
CustomButton *button = (CustomButton *)sender;
button.associatedObject = [self.sourceOfObjects.objectArray lastObject];
[self.foo.arrayOfObjects addObject:button.associatedObject]; // this does not work?
}
• CustomButton is subclass of UIButton and has @property (nonatomic, strong) associatedObject that is a pointer to an object of type NSObject.
• sourceOfObjects is a @property (nonatomic, strong) of self of type MyCustomObject, a subclass of NSObject.
• objectArray is a @property (nonatomic, strong) of sourceOfObjects of type NSMutableArray.
• foo is a @property (nonatomic, strong) of the ViewController of type MyCustomObject, a subclass of NSObject.
• arrayOfObjects is a @property (nonatomic, strong) of foo of type NSMutableArray.
QUESTION: Any idea why I cannot add the pointer from button.associatedObject to self.foo.arrayOfObjects so that I have a pointer in self.foo.arrayOfObjects that points to the same object as button.associatedObject?
WHAT I EXPECT TO HAPPEN: If I subsequently ask for [self.foo.arrayOfObjects lastObject], it should return the object that button.associatedObject also points to.
WHAT ACTUALLY HAPPENS: Subsequently asking for self.foo.arrayOfObjects.count returns 0. I do not believe it is an issue with initialization of arrayOfObjects as I have lazy instantiation in place.
I hope I phrased this question accurately, tried to be precise. :)
NSLog(@"foo: %@", self.foo.arrayOfObjects);andNSLog(@"sourceOfObjects: %@", self.sourceOfObjects.objectArray);to the beginning of the method to make sure that it is not nil. Since you "do not believe it is an issue with initialization", why don't we make sure since this should work. :)self.foo.arrayOfObjectsis not being initialized properly whileself.sourceOfObjects.arrayOfObjectsis just fine.