Cocoa 错误 (256)

Cocoa error (256)

我在我的源代码中使用核心数据来实现数据持久化。该框架用于在所有链接到同一个 iCloud 帐户的不同设备上保留我的 RSS 源应用程序中的一些偏好设置。

    -(id)init{
       if (self= [super init]) {
           model= [NSManagedObjectModel mergedModelFromBundles:nil];

           NSPersistentStoreCoordinator *psc= [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];

              NSError *error= nil;
              NSString *dbPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
             [dbPath stringByAppendingPathComponent:@"feed.db"];
              NSLog(@"%@",dbPath);
              NSURL *dbURL= [NSURL fileURLWithPath:dbPath];

            if (![psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:dbURL options:nil error:&error]) {
               [NSException raise:@"Open failed" format:@"Reason: %@",[error localizedDescription]];
             }

             context= [[NSManagedObjectContext alloc] init];
             [context setPersistentStoreCoordinator:psc];

             [context setUndoManager:nil];
          }
          return self;
    }

控制台出错—

  2015-05-08 23:15:03.963 Nerdfeed4[1023:22906] CoreData: error: -     addPersistentStoreWithType:SQLite configuration:(null) 

  URL:file:///Users/Rahul/Library/Developer/CoreSimulator/Devices/28EDC37B-E3AA-  442F-A1B1-72AD385563D5/data/Containers/Data/Application/BF11CCC0-0B2C-4537-BAB6-  E27E2C84CC4C/Documents/ options:(null) ... returned error Error    Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa     error 256.)"

    UserInfo=0x7a092c70 {NSUnderlyingException=unable to open database   file,      

    NSSQLiteErrorDomain=14} with userInfo dictionary {
    NSSQLiteErrorDomain = 14;
    NSUnderlyingException = "unable to open database file";
  }
  2015-05-08 23:15:03.977 Nerdfeed4[1023:22906] *** Terminating app due to  uncaught exception 'Open failed', reason: 'Reason: The operation couldn’t be   completed. (Cocoa error 256.)

从错误中可以清楚地看出,执行在"if(![psc addPersistentStoreWithType:.. configuration:.. URL:.. options:..error]) "处停止,因为此消息returns 0 表示将抛出异常。我该如何解决这个问题.. 请协助。每次项目 运行.

之前,我都会进行干净的构建

从模拟器中删除应用程序并重新启动。正如其他发帖人所指出的,您很可能更改了数据库架构,现在有一个不兼容的数据库文件。

stringByAppendingPathComponent 方法不会改变接收者 (dbPath),它 return 是新值,所以这一行:

         [dbPath stringByAppendingPathComponent:@"feed.db"];

什么都不做。您需要将 return 值分配给新的路径字符串,并在后续代码中使用它:

    NSString *newPath = [dbPath stringByAppendingPathComponent:@"feed.db"];