Question

I am very new to objective C and came across something I am very confused about. I have 2 classes:

@implementation Class1

NSFetchedResultsController *fetchedResultsController;
NSString* name;
....
- (NSFetchedResultsController *) fetchedResultsController {    
  if (fetchedResultsController != nil) {
    return fetchedResultsController;
  }
  .....
}
@end

@implementation Class2

NSFetchedResultsController *fetchedResultsController;
NSString* name;
....
- (NSFetchedResultsController *) fetchedResultsController {    
  if (fetchedResultsController != nil) {
    return fetchedResultsController;
  }
  .....
}
@end

Class1 gets instantiated 1st, by the time I get to use an instance of Class2 (notices variables are with the same name) Class2 will use fetchedResultsController and name of Class1. Basically fetchedResultsController function called by [self fetchedResultsController] in Class2 will never create a new instance of NSFetchedResultsController, also if I attempt print name during Class2 creating, it would contain value set in Class1? Aren't these private of each class? WHat am I doing wrong?

Was it helpful?

Solution

You have not created private members, you created globals. Try this instead:

@implementation Class1 {
    NSFetchedResultsController *fetchedResultsController;
    NSString* name;
}
....

@end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top