importing sqlite to coredata

前端 未结 3 1824
遇见更好的自我
遇见更好的自我 2021-02-11 09:23

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:36

    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];
        }
    }
    

提交回复
热议问题