无法添加持久存储

Unable to add persistent store

我正在尝试在 Library/Application-Support 文件夹中添加一个 sqlite 持久存储,如下所示:

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

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (!_persistentStoreCoordinator)
    {
        @synchronized(self)
        {
            if (!_persistentStoreCoordinator)
            {
                NSError *error = nil;
                NSURL *storeURL = [[[[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask] lastObject] @"delete.sqlite"];
                NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];
                NSPersistentStore * persistentStore = [persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error];            
                _persistentStoreCoordinator = persistentStoreCoordinator;
            }
        }
    }
    return _persistentStoreCoordinator;
}

失败并出现以下错误:

CoreData: error: -addPersistentStoreWithType:SQLite configuration:(null) 
URL:file:///Users/harshithg/Library/Developer/CoreSimulator/Devices/D9A32558-558A-4E0E-915D-FEF25C772669/data/Containers/Data/Application/9517BBBB-E383-467E-B0E2-8960B734B239/Library/Application%20Support/delete.sqlite 
options:(null) ... 
returned error NSCocoaErrorDomain(512) with userInfo dictionary { reason = "Failed to create file; code = 2";

我在 iOS 13.3 和 Xcode 11.3。有人知道这里发生了什么吗?

Application Support 文件夹不存在,除非您明确创建它。 None 的代码创建了它。有一个 NSFileManager 方法可以为您提供 NSApplicationSupportDirectory URL 同时创建它(如果它尚不存在)。

https://developer.apple.com/documentation/foundation/nsfilemanager/1407693-urlfordirectory?language=objc