Compiler error “use of undeclared identifier” when

2019-07-04 11:33发布

问题:

With the latest LLVM build, the requirement for synthesizing properties has been removed.

Therefore I was able to remove all my @synthesize statements except for the ones for NSFetchedResultsController. Does anyone know why the compiler is warning me when I remove the @synthesize fetchedResultsController; line?

Error:

Use of undeclared identifier "fetchedResultsController", did you mean _fetchedResultsController?

This is my code:

@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;

@synthesize fetchedResultsController;

- (NSFetchedResultsController *)fetchedResultsController {
    if (fetchedResultsController) {
        return fetchedResultsController;
    }

    if (!self.managedObjectContext) {
        self.managedObjectContext = [(MyAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Session" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    [fetchRequest setPredicate: self.predicate];

    [fetchRequest setFetchBatchSize:20];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    fetchedResultsController= [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
    fetchedResultsController.delegate = self;

    NSError *error = nil;
    if (![fetchedResultsController performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return fetchedResultsController;
}

回答1:

When you don't put an @synthesize in your code, the instance variable created to back the property is named _propertyName. You are referring to the instance variable fetchedResultsController which no longer exists after you remove the @synthesize. Instead, change all references to fetchedResultsController to _fetchedResultsController.



回答2:

Because the default synthesized variable is _fetchedResultsController not fetchedResultsController



回答3:

The property fetchedResultsController is automatically synthesized to _fetchedResultsController, and this happens for every synthesized variable.

You should synthesize it explicitly to change its name.