2

I am trying the Core Data Tutorial and I have copied the code as given in this Apple's tutorial on Core Data here:

http://developer.apple.com/library/ios/#documentation/DataManagement/Conceptual/iPhoneCoreData01/Articles/02_RootViewController.html

It asks us to compile and run after implementing the application delegate. When I do so, Xcode 4 compiler gives this error "window undeclared (first use in this function)". I can see that there is window declared as a property and there is a synthesize line in the code like so:

@synthesize window=_window;

so even though there is a window property declared in the program, why am I getting this error?

Here are the .h and .m files:

.h: #import

@interface LocationsAppDelegate : NSObject <UIApplicationDelegate> {
    UINavigationController *navigationController;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) UINavigationController *navigationController;

@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;

- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;

@end

Now for the .m file:

#import "LocationsAppDelegate.h"
#import "RootViewController.h"

@implementation LocationsAppDelegate

@synthesize navigationController;

@synthesize window=_window;

@synthesize managedObjectContext=__managedObjectContext;

@synthesize managedObjectModel=__managedObjectModel;

@synthesize persistentStoreCoordinator=__persistentStoreCoordinator;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    RootViewController *rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];

    NSManagedObjectContext *context = [self managedObjectContext];
    if(!context) {
        //handle the error you dummy!!
    }

    rootViewController.managedObjectContext = context;


    UINavigationController *aNavigationController = [[UINavigationController alloc]initWithRootViewController:rootViewController];
    self.navigationController = aNavigationController;


    [self.window addSubview:[navigationController view]];
    [self.window makeKeyAndVisible];

     [rootViewController release];
 [aNavigationController release];


    return YES;
}


- (void)applicationWillTerminate:(UIApplication *)application
{
    [self saveContext];
}

- (void)dealloc
{
    [_window release];
    [__managedObjectContext release];
    [__managedObjectModel release];
    [__persistentStoreCoordinator release];
    [super dealloc];
}

- (void)saveContext
{
    NSError *error = nil;
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    if (managedObjectContext != nil)
    {
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
        {

            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        } 
    }
}

#pragma mark - Core Data stack

- (NSManagedObjectContext *)managedObjectContext
{
    if (__managedObjectContext != nil)
    {
        return __managedObjectContext;
    }

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil)
    {
        __managedObjectContext = [[NSManagedObjectContext alloc] init];
        [__managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return __managedObjectContext;
}

- (NSManagedObjectModel *)managedObjectModel
{
    if (__managedObjectModel != nil)
    {
        return __managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Locations" withExtension:@"momd"];
    __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];    
    return __managedObjectModel;
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (__persistentStoreCoordinator != nil)
    {
        return __persistentStoreCoordinator;
    }

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Locations.sqlite"];

    NSError *error = nil;
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
    {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }    

    return __persistentStoreCoordinator;
}

#pragma mark - Application's Documents directory

- (NSURL *)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

@end
user709903
  • 195
  • 1
  • 3
  • 10

2 Answers2

3

Try to use

self.window

instead of

window
Nekto
  • 17,837
  • 1
  • 55
  • 65
0

Just to expand on that answer a bit:

You can see from the @synthesize statement that the property window is backed by the instance variable _window. So you use window to access it via the getter (self.window or [self window]), but if you're trying to access the instance variable directly, it would be _window.

Flyingdiver
  • 2,142
  • 13
  • 18
  • Thanks! I should have added this bit. I did use self.window. It still says "window undeclared". If I do not use self.window, I also get the error that says "use of undeclared identifier 'window'. I get two of these errors and one error of "window undeclared". The moment I change it to self.window, the number of errors reduce to one, which is "window undeclared (first use in this function". What am I missing? – user709903 Aug 26 '11 at 18:15
  • You'll have to post the sections of the .h and .m files where you're declaring the objects and using the window variable. – Flyingdiver Aug 26 '11 at 18:26
  • I added the .m and .h files to the question above. Once again, thank you for your help! – user709903 Aug 29 '11 at 10:20