我试图在OSX上编写和读取CoreData,使用applescript-objc组合可可类。 我编写了处理我的CoreData的方法,如教程(youtube, Cocoa Tutorial: Core Data Introduction in iOS and Mac OS Programming Part 2)< - 那些是我项目中的可可类
我有一个名为CoreDataHelper的类,它定义了操作Core Data的方法。以下是其中一些:
+(NSManagedObjectContext *) managedObjectContext{
NSError*error;
[[NSFileManager defaultManager] createDirectoryAtPath:[CoreDataHelper directoryForDatabaseFilename] withIntermediateDirectories:YES attributes:nil error:&error];
if(error){
NSLog(@"%@", [error localizedDescription]);
return nil;
}
NSString* path = [NSString stringWithFormat:@"%@/%@",[CoreDataHelper directoryForDatabaseFilename],[CoreDataHelper databaseFilename]];
NSURL *url = [NSURL fileURLWithPath:path];
NSManagedObjectModel* managedModel = [NSManagedObjectModel mergedModelFromBundles:nil];
NSPersistentStoreCoordinator* storeCordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedModel];
if(![storeCordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:&error]){
NSLog(@"%@",[error localizedDescription]);
return nil;
}
NSManagedObjectContext* managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
return managedObjectContext;
}
+(id)insertManagedObjectOfClass:(Class) aClass inManagedObjectContext:(NSManagedObjectContext *)managedObjectContext{
NSManagedObject* managedObject = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass(aClass) inManagedObjectContext:managedObjectContext];
return managedObject;
}
我在我的模型中使用.xcdatamodeld。 我有2个实体,每个实体都有一个类:
Tab.h看起来像这样
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class Service;
NS_ASSUME_NONNULL_BEGIN
@interface Tab : NSManagedObject
// Insert code here to declare functionality of your managed object subclass
@end
NS_ASSUME_NONNULL_END
#import "Tab+CoreDataProperties.h"
和Tab.m看起来像这样:
#import "Tab.h"
#import "Service.h"
@implementation Tab
// Insert code here to add functionality to your managed object subclass
@end
标签+ CoreDataProperties.h:
#import "Tab.h"
NS_ASSUME_NONNULL_BEGIN
@interface Tab (CoreDataProperties)
@property (nullable, nonatomic, retain) NSString *nZakladka;
@property (nullable, nonatomic, retain) NSString *uZakladka;
@property (nullable, nonatomic, retain) Service *podstronaSerwisu;
@end
NS_ASSUME_NONNULL_END
标签+ CoreDataProperties.m: #import&#34; Tab + CoreDataProperties.h&#34;
@implementation Tab (CoreDataProperties)
@dynamic nZakladka;
@dynamic uZakladka;
@dynamic podstronaSerwisu;
@end
现在,我的AppDelegate.applescript看起来像这样
...
property CoreDataHelper: class "CoreDataHelper"
property cService: class "Service"
property cTab: class "Tab"
script AppDelegate
on applicationWillFinishLaunching_(aNotification)
set theContext to CoreDataHelper's managedObjectContext()
set ccTab to cTab's alloc()'s init()
//the one below cause an error "CoreData: error: Failed to call designated initializer on NSManagedObject class 'Tab'"
set theTab to CoreDataHelper's insertManagedObjectOfClass_inManagedObjectContext_(ccTab, theContext)
end applicationWillFinishLaunching_
...
end Script
当我运行整件事时,我在AppleScriptObjC代码的注释行中发布了一条错误消息。导致错误的方法是NSManagedObject* managedObject = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass(aClass) inManagedObjectContext:managedObjectContext];
有什么问题?