gpt4 book ai didi

ios - iOS教程中的"window"undeclared (first use in this function)错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:18:29 25 4
gpt4 key购买 nike

我正在尝试 Core Data 教程,我已经复制了 Apple 的 Core Data 教程中给出的代码:

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

它要求我们在实现应用委托(delegate)后编译运行。当我这样做时,Xcode 4 编译器给出了这个错误“窗口未声明(首次在此函数中使用)”。我可以看到有一个窗口被声明为一个属性,并且在代码中有一个合成行,如下所示:

@synthesize window=_window;

所以即使在程序中声明了窗口属性,为什么我会收到此错误?

这是 .h 和 .m 文件:

.h: #导入

@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

现在是 .m 文件:

#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

最佳答案

尝试使用

self.window

代替

window

关于ios - iOS教程中的"window"undeclared (first use in this function)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7207771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com