1

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. :)

5
  • What exactly do you mean by "does not work"? Explain (1) what you expected to happen and (2) what actually did happen. Commented May 5, 2012 at 23:40
  • 3
    Add: NSLog(@"foo: %@", self.foo.arrayOfObjects); and NSLog(@"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. :) Commented May 5, 2012 at 23:54
  • Cool, I did not know you could do that. Of course, you are correct :) I did not know that null objects have a count of 0 instead a count of null. However, I do not understand why self.foo.arrayOfObjects is not being initialized properly while self.sourceOfObjects.arrayOfObjects is just fine. Commented May 6, 2012 at 18:54
  • What is the StackOverflow practice if you discover you are asking the wrong question? Should you delete the question and re-ask the proper question? Should you edit the question to note? Or should you just leave the question in place even if it doesn't seem very helpful? Commented May 6, 2012 at 18:57
  • @KenHaggerty it depends. If your question is still a good question then leave it up and accept the best answer. Think of SO as more than a simple Q/A site, its more of a community driven programming wiki. Even if your question isn't what you intended to ask it may be useful for someone sometime in the future. Commented May 6, 2012 at 23:41

2 Answers 2

1

I forgot to add lazy instantiation for self.foo.arrayOfObjects within the definition for class MyCustomObject. :S What that means is that within file MyCustomObject.m the following lines were missing:

- (void)setArrayOfObjects:(NSMutableArray *)arrayOfObjects
{
    _arrayOfObjects = arrayOfObjects;
}

- (NSMutableArray *)arrayOfObjects
{
    if (!_arrayOfObjects) _arrayOfObjects = [[NSMutableArray alloc] init];
    return _arrayOfObjects;
}

Because arrayOfObjects was not lazily instantiated within the MyCustomObject class, adding an object to self.foo.arrayOfObjects from within ViewController.m did nothing because self.foo.arrayOfObjects was still null, and adding an object to null procures a null object.

Sign up to request clarification or add additional context in comments.

Comments

0

I might be wrong here, but NSObject and all it's subclasses are actually pointers. So if you explicitly create a pointer to an NSObject, and then pass that pointer to addObject, you are passing the pointer to the pointer, and not the pointer to the Object that the method is expecting.

How do you define associatedObject?

3 Comments

Hmm, I don't see anything in his question to lead us to believe he is trying to store an NSObject**. What brought you to this conclusion?
I interpreted the explanation of the associatedObject property in this way. I'm not used to people referring to NSObject as being "a pointer to an object of type NSObject."
Yeah, I suppose it would have been better for the author to say "sourceOfObjects is a @property (nonatomic, strong) of self of type NSObject*" instead. Nevertheless, I think he meant NSObject* (instead of NSObject), so I doubt he's trying to use multiple levels of pointers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.