0

I must be overthinking this simple parent and subclass communication in Objective-C.

I am creating and filling an NSMutableArray in the parent View Controller, then in the subclass View Controller I want to access the objects in that array. Xcode is not throwing any errors, but in the subclass it is not finding any objects in the array. What am I doing wrong?

Parent.h:

@interface ParentViewController : UIViewController
@property (strong, nonatomic) NSMutableArray *myArray;
@end

Parent.m:

[self.myArray addObject:myString]; //add objects to array in a method

Subclass.h:

#import "ParentViewController.h"
@interface SubclassViewController : ParentViewController
@end

Subclass.m:

#import "SubclassController.h"
@implementation SubclassViewController
- (void)myMethod {
    NSLog(@"%d in parent array", [super.myArray count]); //always returns 0
}
@end
1
  • myArray is never initialised. Commented Jan 27, 2014 at 0:28

3 Answers 3

3

It depends on where you put this line in your code (in which method):

[self.myArray addObject:myString]; //add objects to array in a method

You can for example put it in your parentViewController's -viewDidLoad method and if you call [super viewDidLoad] in your SubclassViewController (or you do not implement the viewDidLoad method in your SubclassViewController at all) it will actually work.

The problem is that you are mixing up a class and an object: What you see in the end on screen is an instance of your class parentViewControllerand an instance of your class SubclassViewController i.e. they are two distinct objects. Just like Sam and Bob are two different "objects" of the class "Human". Now they both have a property myArray - like a list that Sam and Bob both carry. But as they are two different individuals they also carry different lists. And if Sam writes some notes on his list it does not mean that those notes automagically appear on Bob's list as well.

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

1 Comment

Thank you for the explanation. :) I now realize subclasses inherit the properties but they are different. Inheritance wasn't what I wanted then, and instead I used the prepareForSegue method to send my mutable array to the other view controller as an immutable array (it's own property) which is great, since that view controller shouldn't change it anyways.
2

You need to allocate memory to your mutable array by allocing in inside your Parent.m init method:

self.myArray = [NSMutableArray array];

Comments

0

There is no need to use "super.myArray". All properties defined in a parent class are properties of the subclass. You could just as well use "self.myArray" They are the same object.

My guess is that you are actually dealing with 2 different objects. Post the code that creates your object, as well as the complete method that contains the line

[self.myArray addObject:myString]; 

Also add a log of self to both sections of code:

[self.myArray addObject:myString]; 
NSLog(@"In %s for %@, self.myArray = %@", __PRETTY_FUNCTION__, self, self.myArray);

And

- (void)myMethod 
{
  NSLog(@"In %s for %@ array = %@", __PRETTY_FUNCTION__, self, self.myArray); //always returns 0
}

Comments

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.