我試圖閱讀很多問題/教程,但我找不到答案! 我試圖從一個文本框得到一個字符串,並將其保存在我創建了一個datas.plist文件(默認鍵根目錄下) 我試過這段代碼:添加一個NSString到.plist(屬性列表)文件
//Save Name to plist
if([name.text length] > 0){
NSString *string = name.text;
[array addObject:string];
//plist path
NSString *path = [[NSBundle mainBundle] pathForResource:@"datas" ofType:@"plist"];
//save object
[array writeToFile: path atomically:YES];
}
其中陣列是
@interface DetailViewController(){
NSMutableArray* array;
}
但是當我嘗試應用程序崩潰,以節省給這個錯誤(我連接到一個按鈕之前寫的動作):
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot create an NSPersistentStoreCoordinator with a nil model'
我認爲WR代碼它是不正確的,因爲我從來沒有鍵入鑰匙保存(如@「根」)或類似的東西。 在此先感謝
編輯:
我想這代碼,但不寫什麼:
- (void)addToMyPlist {
NSString *destPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
destPath = [destPath stringByAppendingPathComponent:@"dati"];
// If the file doesn't exist in the Documents Folder, copy it.
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:destPath]) {
NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"dati" ofType:@"plist"];
[fileManager copyItemAtPath:sourcePath toPath:destPath error:nil];
}
// Load the Property List.
array = [[NSMutableArray alloc] initWithContentsOfFile:destPath];
//NSString *path = [[NSBundle mainBundle] pathForResource:@"dati" ofType:@"plist"];
NSString *string = self.name.text;
NSArray *values = [[NSArray alloc] initWithObjects:string, nil];
NSArray *keys = [[NSArray alloc] initWithObjects:@"Root", nil];
NSDictionary *dict = [[NSDictionary alloc] initWithObjects:values forKeys:keys];
[array addObject:dict];
[array writeToFile:destPath atomically:YES];
}
有什麼不對?
如果我每天都這樣被要求對堆棧溢出... – 2013-10-18 11:15:09
你的plist中的問題和您的例外(核心數據)時拿到一分,似乎是無關... – Wain
@Bigood是啊,我做了謝謝 – r4id4