importing sqlite to coredata

前端 未结 3 815
庸人自扰
庸人自扰 2021-02-11 09:05

I have imported sqlite prepopulated dbs to my coredata projects before, but now I have created a project in the 3.2.5. xcode, wich changes nsurl for nstring in the AppDelegate,

3条回答
  •  无人共我
    2021-02-11 09:56

    You don't need to use NSString paths at all, it can all be done with NSURL methods.

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Database.sqlite"];
    
    // If the database doesn't exist copy in the default one
    if (![storeURL checkResourceIsReachableAndReturnError:NULL])
    {
        NSURL *defaultStoreURL = [[NSBundle mainBundle] URLForResource:@"Database" withExtension:@"sqlite"];
    
        if ([defaultStoreURL checkResourceIsReachableAndReturnError:NULL])
        {
            NSFileManager *fileManager = [NSFileManager defaultManager];
            [fileManager copyItemAtURL:defaultStoreURL toURL:storeURL error:NULL];
        }
    }
    

提交回复
热议问题