accessing property in another class
ios
Solution
1. Here, your are simply creating a new instance of A, instead of accessing the existing instance of A to fetch the array inside B instance. So that only it is showing null value.
2. See this code for 2nd question....
ClassA.h
@interface ClassA:UIViewController
@property (nonatomic, retain) NSMutableArray *arrayOfPaths;
@end
ClassA.m
@implementation ClassA
@synthesize arrayOfPaths = _arrayOfPaths;
-(id)init
{
self = [super init];
if(self)
{
self.arrayOfPaths = [[NSMutableArray alloc] initWithObjects:@"1",@"2", nil];
//Delegation process
ClassB *bInstance = [[ClassB alloc] init];
[bInstance setAReference:self];
//Do your stuff of presenting ClassB instance here.....
}
}
@end
ClassB.h
#import "ClassA.h"
@interface ClassB:UITableViewController
@property (nonatomic, copy) ClassA *aReference;
@end
ClassB.m
@implementation ClassB
@synthesize aReference = _aReference;
-(void)viewDidLoad
{
NSLog(@"Array from class ClassA %@", self.aReference.arrayOfPaths);
}
@end
ClassB ViewController should be instantiated by ClassA only here.
3. You need to use the delegates concept to acheive your requirement. See the above example code
4. objective -c won't support multiple inheritance.
Problem
I have 2 classes, say for A and B. I have a mutable array _arrayOfPaths in A class now I am trying to access that array in class b. Like below: ``` A *testModel = [[A alloc]init]; NSMutableArray *array = [testModel.arrayOfPaths mutableCopy]; ``` Now when I `NSLog` this newly made array then it shows null. if I print `testModel.arrayOfPaths` then it shows null too. My questions are: - Why its showing null? - How can I access `_arrayOfPaths` in B class? - Do I need to use inheritance for it? B is already inheriting `UITableViewController`. - Is Multiple inheritance supported by objective c?