我有一個包含10個表的數據庫。因爲我需要在不同的視圖控制器中訪問這個數據庫,所以我必須在每個視圖控制器中聲明下面顯示的兩個方法。有沒有一種方法可以通過在應用程序委託中聲明這些方法來避免這種情況。如果是的話,我怎麼能在不同的課程中使用這些方法。如何在不同的類中調用appdelegete方法iphone
- (NSString *) getWritableDBPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:DATABASE_NAME];
}
-(void)createEditableCopyOfDatabaseIfNeeded
{
// Testing for existence
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:DATABASE_NAME];
NSLog(@"%@",writableDBPath);
success = [fileManager fileExistsAtPath:writableDBPath];
if (success)
return;
// The writable database does not exist, so copy the default to
// the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:DATABASE_NAME];
success = [fileManager copyItemAtPath:defaultDBPath
toPath:writableDBPath
error:&error];
if(!success)
{
NSAssert1(0,@"Failed to create writable database file with Message : '%@'.",
[error localizedDescription]);
}
}
然後我想cretae我會強烈建議,象這樣的定義創建數據庫控制器像這樣[appdelegate getWritableDBPath]; [appdelegate createEditableCopyOfDatabaseIfNeeded]; – Rocky
是什麼意思?沒有得到你 – Maulik
是的maulik因爲我試試這爲什麼我說你 – Rocky