簡體   English   中英

無法在iOS中創建文件(可可錯誤514.)

[英]Cannot create a file in iOS (cocoa error 514.)

在我的應用程序中,我要創建一個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.) Error: The operation couldn't be completed. (Cocoa error 514.) 所以我在網上搜索了什么意思是錯誤514,我發現了這個 ,我在其中搜索了錯誤,我發現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);
    }
}

改變這一行

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM