2012-01-04 90 views
2

可能還有許多其他問題,我甚至不知道要問,因爲我是新手應用程序編程。捆綁sqlite數據庫與應用程序和stringByAppendingPathComponent錯誤

我最初從應用程序中創建的數據庫,它複製到我的工作文件夾(這可能不是它應該最終身居),然後將其附加我的記錄(其中約1000),從文本文件中。

首先想到的兩個問題是: - 數據庫應該在哪個文件夾中? - 它如何與應用程序一起部署?

我發現相當幾個例子使用persistentStoreCoordinator函數下列行:

NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"myDatabase.sqlite"]; 
NSURL *storeUrl = [NSURL fileURLWithPath:storePath]; 

但第一行給出我預編譯錯誤:「接收器類型‘NSURL’例如消息不聲明一個方法與選擇「stringByAppendingPathComponent:」爲什麼不是爲我工作

這其實是捆綁我的數據庫與應用程序的其餘部分的最佳方式

感謝

01?!
+0

http://stackoverflow.com/questions/5002250/importing-sqlite-to-coredata – Kjuly 2012-02-16 07:22:11

回答

1

最簡單的解決方案是使用NSUrl而不是NSString。 SO user @ trapper已經在下面的鏈接中提供了一個解決方案。

importing sqlite to coredata

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]; 
    } 
} 
相關問題