2011-02-11 42 views
4

我用xcode 3.2.5開始一個新的coredata項目後出現了一些問題...我之前的項目與核心數據(在以前的xcode)工作正常,所以我不知道什麼是區別??coredata問題nsurl可能不會響應stringByAppendingPathComponent

所以我得到的,當我建立和去調用核心數據視圖的誤差>

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter' 

奇怪的是,在我的* AppDelegate.m,在(編輯感謝羅格,但仍不工作!)

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { 

if (persistentStoreCoordinator_ != nil) { 
    return persistentStoreCoordinator_; 
} 

NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"staff.sqlite"]; 

NSURL *storeUrl = [NSURL fileURLWithPath:storePath]; //new position for the storeUrl!  

// Put down default db if it doesn't already exist 
NSFileManager *fileManager = [NSFileManager defaultManager]; 
if (![fileManager fileExistsAtPath:storePath]) { 
    NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"staff" ofType:@"sqlite"]; 
    if (defaultStorePath) { 
     [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:NULL]; 
    } 
} 

NSString *storePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"staff.sqlite"]; 

我收到警告

NSURL may not respond to '-stringByAppendingPathComponent' 

我選項+單擊此stringByAppendingPathComponent並得到(找不到符號!!!!)

,但在其他項目中,我做的選擇+點擊相同,並得到了定義!

  • 所以這個警告與我的錯誤有關?
  • 如何解決?

編輯, 在我viewDidLoad中

的NSLog包括在本(@ 「路徑=%@」,[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)lastObject]);

這使我在控制檯:

path= /Users/mkss9/Library/Application Support/iPhone Simulator/4.2/Applications/2F364C20-2B87-4ABB-AA3E-FB6F7C15096F/Documents 

請!即時得到瘋了!

謝謝!

回答

7

某些SDK版本之前(我不知道他們是否確定)蘋果在其項目模板中更改了返回類型applicationDocumentsDirectory

當你創建一個新的項目,它看起來是這樣的:

/** 
Returns the URL to the application's Documents directory. 
*/ 
- (NSURL *)applicationDocumentsDirectory { 
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; 
} 

老年模板,它是這樣的:

/** 
Returns the path to the application's documents directory. 
*/ 
- (NSString *)applicationDocumentsDirectory { 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil; 
    return basePath; 
} 

,並在這兩個之間它是這樣的:

/** 
Returns the path to the application's Documents directory. 
*/ 
- (NSString *)applicationDocumentsDirectory { 
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
} 

所以你必須小心,因爲所有依賴於applicationDocumentsDirect的舊代碼ory返回一個NSString將不適用於較新的模板。

而且您不能只用新版本替換舊版本,因爲這會導致您的核心數據方法發生變化。

所以我建議你編寫你自己的方法來返回文檔目錄。蘋果經常改變它們的applicationDocumentsDirectory

1

我會想象這是因爲-applicationDocumentsDirectory返回NSURL *而不是NSString *

+0

我明白了,但爲什麼這個確切的代碼在我以前的項目中工作?即使是蘋果的coredatabooks例子也使用這個> NSString * storePath = [[self applicationDocumentsDirectory] ​​stringByAppendingPathComponent:@「CoreDataBooks.sqlite」]; 那會是什麼呢?謝謝! – MaKo 2011-02-11 02:18:14

0

首先你需要確保你的applicationDocumentsDirectory方法返回一個NSString。

一旦出現這種情況,後續崩潰是因爲您傳遞的路徑和文件名尚不存在。

因此,如果您將NSURL *storeUrl = [NSURL fileURLWithPath:storePath];移動到檢查現有文件並放入默認文件以防萬一它不存在的代碼之後,它應該可以解決您的問題。

+0

嗨,做了你的建議,但還沒有工作,請看編輯的變化,謝謝 – MaKo 2011-02-11 03:38:14