拉我的頭髮試圖解決這個問題。我想在我的項目中讀取和寫入一個數字列表到一個txt文件。然而[字符串writeToFile:原子路徑:是編碼:NSUTF8StringEncoding錯誤:&錯誤]似乎沒有寫入任何文件。我可以看到路徑字符串返回一個文件路徑,所以它似乎找到了它,但似乎沒有寫入任何文件。將字符串寫入目標c中的txt文件
+(void)WriteProductIdToWishList:(NSNumber*)productId {
for (NSString* s in [self GetProductsFromWishList]) {
if([s isEqualToString:[productId stringValue]]) {
//exists already
return;
}
}
NSString *string = [NSString stringWithFormat:@"%@:",productId]; // your string
NSString *path = [[NSBundle mainBundle] pathForResource:@"WishList" ofType:@"txt"];
NSError *error = nil;
[string writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];
NSLog(@"%@", error.localizedFailureReason);
// path to your .txt file
// Open output file in append mode:
}
編輯:路徑示出了如/var/mobile/Applications/CFC1ECEC-2A3D-457D-8BDF-639B79B13429/newAR.app/WishList.txt所以確實存在。但是回頭看看:
NSString *path = [[NSBundle mainBundle] pathForResource:@"WishList" ofType:@"txt"];
只返回一個空字符串。
是否有任何特定的要求來將信息寫入txt本身。如果你把它寫成plist以便使用,這會更好。 – Anupdas 2013-05-10 17:03:08
是否要將這些文件存儲在與項目主目錄相關的子目錄中? – nzs 2013-05-10 18:08:40