我正在尝试编写逻辑测试来调试一个将图像数据保存到磁盘的方法。我使用以下代码将图像数据写入文档目录:
docspath = [[NSString alloc] initWithString:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, TRUE) objectAtIndex:0]];
fullpath = [[NSString alloc] initWithFormat:@"%@/%@", docspath, _name];
BOOL written = [fileData writeToFile:fullpath options:NSDataWritingAtomic error:&dataerror];
但是,数据没有被写入,错误描述返回“没有这样的文件或目录”。我在iOS 5.1模拟器中的测试目标下运行此代码。我需要创建中间目录才能到达文档目录,还是在测试目标下写入磁盘存在其他问题?
返回的错误信息:
2012-06-29 16:13:31.819 otest[4597:7b03] /Users/myaccount/xcode_projects/myproject/myproject/myfolder/MMFileManager.m - 285: Error Domain=NSCocoaErrorDomain Code=4 "The operation couldn’t be completed. (Cocoa error 4.)" UserInfo=0x1e1e3a0 {NSUserStringVariant=Folder, NSFilePath=/Users/myaccount/Library/Application Support/iPhone Simulator/5.1/Documents/mmLogo.png, NSUnderlyingError=0x1e1b430 "The operation couldn’t be completed. No such file or directory"}
[NSString alloc]
,而应该使用NSString
。不要手动附加路径组件,使用stringByAppendingPathComponent:
来确保路径组件被正确附加。 - Anne