核心数据Macos应用程序以及与OS X 10.7的兼容性

时间:2012-10-28 23:09:16

标签: xcode macos core-data

您好我已经创建了一个包含Core Data和OS X 10.8的简单应用程序,它是xcode创建的简单模板,但如果我将Base SDK更改为10.7 xcode,请在App Controller中为此方法提供此错误:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (_persistentStoreCoordinator) {
    return _persistentStoreCoordinator;
}

NSManagedObjectModel *mom = [self managedObjectModel];
if (!mom) {
    NSLog(@"%@:%@ No model to generate a store from", [self class], NSStringFromSelector(_cmd));
    return nil;
}

NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *applicationFilesDirectory = [self applicationFilesDirectory];
NSError *error = nil;

NSDictionary *properties = [applicationFilesDirectory resourceValuesForKeys:@[NSURLIsDirectoryKey] error:&error];

if (!properties) {
    BOOL ok = NO;
    if ([error code] == NSFileReadNoSuchFileError) {
        ok = [fileManager createDirectoryAtPath:[applicationFilesDirectory path] withIntermediateDirectories:YES attributes:nil error:&error];
    }
    if (!ok) {
        [[NSApplication sharedApplication] presentError:error];
        return nil;
    }
} else {
    if (![properties[NSURLIsDirectoryKey] boolValue]) {
        // Customize and localize this error.
        NSString *failureDescription = [NSString stringWithFormat:@"Expected a folder to store application data, found a file (%@).", [applicationFilesDirectory path]];

        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setValue:failureDescription forKey:NSLocalizedDescriptionKey];
        error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:101 userInfo:dict];

        [[NSApplication sharedApplication] presentError:error];
        return nil;
    }
}

NSURL *url = [applicationFilesDirectory URLByAppendingPathComponent:@"SimpleApp.storedata"];
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mom];
if (![coordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:url options:nil error:&error]) {
    [[NSApplication sharedApplication] presentError:error];
    return nil;
}
_persistentStoreCoordinator = coordinator;

return _persistentStoreCoordinator;
}

我收到此错误:

enter image description here

我该怎么做?

1 个答案:

答案 0 :(得分:2)

这实际上不是Core Data的问题,但这与object subscripting有关。为了使这些文字正常工作,您的SDK必须至少是OS X 10.8或iOS 6。

如果没有其他方法,您仍然可以使用下标并通过向NSObject上实现所需方法的类别添加存根标头来将SDK保持为10.7。你可以看到一个这样的例子here。不过,我建议你将SDK保存在10.8上。