search

Home  >  Q&A  >  body text

Objective-c - About the file system of iOS

I added a sql file to xcode myself, and added it to a directory similar to Pods/Frameworks/ below , but when the code is executed, it seems that the file I added cannot be found

- (instancetype)initWithDatabaseFilename:(NSString *)dbFilename {
    self = [super init];
    if (self) {
        self.databaseFilename = dbFilename;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        self.databasePath = [documentsDirectory stringByAppendingPathComponent:self.databaseFilename];
        
        [self copyDatabaseIntoDocumentsDirectory];
    }
    return self;
}

- (void)copyDatabaseIntoDocumentsDirectory {
    if (![[NSFileManager defaultManager] fileExistsAtPath:_databasePath]) {
        NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:_databaseFilename];
        NSError *error;
        [[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:_databasePath error:&error];
        if (error != nil) {
            NSLog(@"error: %@", [error localizedDescription]);
        }
    }
}
给我你的怀抱给我你的怀抱2884 days ago543

reply all(0)I'll reply

No reply
  • Cancelreply