gpt4 book ai didi

ios - 无法在 iOS 中创建文件(cocoa 错误 514。)

转载 作者:行者123 更新时间:2023-12-03 16:57:23 25 4
gpt4 key购买 nike

在我的应用程序中,我必须创建一个 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 文件。当我查看控制台时,它告诉我:错误:操作无法完成。 ( cocoa 错误 514。)。于是我在网上搜索了错误514是什么意思,我发现this ,我在其中搜索了错误,发现 NSFileWriteInvalidFileNameError = 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"];

BOOL success = [target writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:&error];

if (!success) {
NSLog(@"Errore: %@", error.localizedDescription);
}
}

最佳答案

更改此行

BOOL success = [target writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:&error];

BOOL success = [target writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:&error];

关于ios - 无法在 iOS 中创建文件(cocoa 错误 514。),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21984599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com