在我的应用中我创建了一个csv文件,所以我创建了这个方法:
+ (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
,我怎么能解决这个错误?在我的方法中,问题是什么。
我希望你能帮助我解决这个问题
由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);
}
}
答案 0 :(得分:2)
更改此行
BOOL success = [target writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:&error];
到
BOOL success = [target writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:&error];