在我的應用我已經創建一個CSV文件,所以我創建這個方法:無法建立的iOS文件(可可錯誤514)
+ (void)saveFileFromArray:(NSMutableArray *)promoArray {
NSMutableString *target = [@"Nome,Cognome,E-mail,Data di nascita,Indirizzo,Città,Cap,Telefono\n" mutableCopy];
for (NSDictionary *dict in promoArray) {
[target appendFormat:@"%@,%@,%@,%@,%@,%@,%@,%@\n",dict[@"name"],dict[@"surname"],dict[@"email"],dict[@"birthdate"],dict[@"address"],dict[@"city"],dict[@"zip"],dict[@"phone"]];
}
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"profiles.csv"];
NSURL *url = [NSURL URLWithString:fileName];
BOOL success = [target writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (!success) {
NSLog(@"Errore: %@", error.localizedDescription);
}
}
但是當我運行應用程序,現在它保存.plist文件但不保存.csv文件。當我看着控制檯時,它告訴我:Error: The operation couldn’t be completed. (Cocoa error 514.)
。所以我在網上搜索了什麼意思錯誤514,我發現this,其中我搜索了錯誤,我發現NSFileWriteInvalidFileNameError = 514
,我該如何解決這個錯誤?在我的方法中有什麼問題。 我希望你能幫助我解決這個問題
解決BY MYSELF:
+ (void)saveFileFromArray:(NSMutableArray *)promoArray {
NSMutableString *target = [@"Nome,Cognome,E-mail,Data di nascita,Indirizzo,Città,Cap,Telefono\n" mutableCopy];
for (NSDictionary *dict in promoArray) {
[target appendFormat:@"%@,%@,%@,%@,%@,%@,%@,%@\n",dict[@"name"],dict[@"surname"],dict[@"email"],dict[@"birthdate"],dict[@"address"],dict[@"city"],dict[@"zip"],dict[@"phone"]];
}
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"profiles.csv"];
BOOL success = [target writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (!success) {
NSLog(@"Errore: %@", error.localizedDescription);
}
}
這就是做這件事的一個昂貴的方式;爲什麼不把每行寫入文件而不是在內存中構建文件? – trojanfoe
如果使用:NSURL * url = [NSURL fileURLWithPath:fileName];'?會發生什麼? – trojanfoe
請,請Stackoverflow不是論壇!將您的解決方案作爲*答案*發佈,而不是作爲問題的更新!回答你自己的問題完全可以。 – dandan78