【发布时间】:2012-11-06 09:52:56
【问题描述】:
我有以下尝试将字符串写入桌面上的文件,但出现错误。
我写的代码是:
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
// Get array with first index being path to desktop
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES);
// Get the first element
NSString *desktopPath = [paths objectAtIndex:0];
// Append words.txt to path
NSString *theFilePath = [desktopPath stringByAppendingPathComponent:@"glutenFreeJson.txt"];
NSError *error = nil;
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"%@\n\n", jsonString);
NSLog(@"%@\n\n", theFilePath);
[jsonString writeToFile:theFilePath atomically:YES encoding:NSStringEncodingConversionAllowLossy error:&error];
NSLog(@"Write returned error: %@", [error localizedDescription]);
这会打印“Write returned error: The operation couldn’t be completed. (Cocoa error 4.)”
我不知道为什么。
更新:应用程序最初从服务器请求 json 数据,对其进行解析并显示。但是,服务器一直不稳定,并不总是及时响应,我只需要一次数据,所以我只想将它下载到我电脑上的桌面文件夹并将其移动到我的项目文件夹中。
【问题讨论】:
-
你理解的 iOs Desktop 路径是什么(当你使用
NSDesktopDirectory)?
标签: ios nsstring writetofile