因此,我已经编码了一堆用于编写的对象,但是我在文件管理方面遇到了问题。我似乎不能打开现有的文件,或者创建一个新的文件。
我尝试创建一个同名的空文件,或者只是创建一个新文件。它似乎没有回应(也没有为此做任何事情)。
编辑,所以我已经遵循了这里的一些建议,现在我的访问崩溃很糟糕。
我在这里找到了道路:
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
filePath = [documentsDirectory stringByAppendingPathComponent:@"/bucketListData.dat"];
这是改进的方法。
-(void) writeFile
{
NSData *encodedObject;
encodedObject = [NSKeyedArchiver archivedDataWithRootObject: bucketItems];
NSFileHandle *file;
file = [NSFileHandle fileHandleForReadingAtPath:filePath];
if(file == nil)
{
NSLog(@"Failed to open a file handle for writing. Attempting to create a file.");
[[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
file = [NSFileHandle fileHandleForReadingAtPath:filePath];
if(file == nil)
{
NSLog(@"Unable to create new file.");
}
}
[file writeData: encodedObject];
[file closeFile];
}
发布于 2013-04-01 21:51:13
您没有写入根用户的权限,您只能访问您的沙箱、和,您正在创建一个只读句柄。试着这样做:
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [docDir stringByAppendingPathComponent:@"bucketListData.dat"];
NSFileHandle *file = [NSFileHandle fileHandleForWritingAtPath:filePath];
if (!file) {
NSLog(@"Attempting to create the file");
if (![[NSFileManager defaultManager] createFileAtPath:filePath
contents:nil
attributes:nil]) {
NSLog(@"Failed to create file");
}
else {
file = [NSFileHandle fileHandleForWritingAtPath:filePath];
}
}
NSLog(@"File handle: %@", file);
发布于 2013-04-01 21:50:24
您试图在文件系统的根目录上打开一个文件,但是您在iOS上没有访问该文件的权限。相反,请在documents目录中创建文件:
NSData* encodedObject = [NSKeyedArchiver archivedDataWithRootObject: bucketItems];
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString* path = [basePath stringByAppendingPathComponent:@"bucketListData.dat"];
NSFileHandle* file = [NSFileHandle fileHandleForReadingAtPath:path];
发布于 2013-04-01 21:55:27
您需要在documents目录中写入文件。然后使用stringByAppendingPathComponent tu创建完整的路径。这应该管用我希望这能帮上忙..。
-(void) writeFile
{
NSData *encodedObject;
encodedObject = [NSKeyedArchiver archivedDataWithRootObject: bucketItems];
NSFileHandle *file;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory;
if(paths && [paths count]>0){
documentsDirectory=[paths objectAtIndex:0];
file = [NSFileHandle fileHandleForReadingAtPath:[documentsDirectory stringByAppendingPathComponent:@"/bucketListData.dat"]];
if(file == nil)
{
NSLog(@"Failed to open a file handle for writing. Attempting to create a file.");
[[NSFileManager defaultManager] createFileAtPath:[documentsDirectory stringByAppendingPathComponent:@"/bucketListData.dat"] contents:nil attributes:nil];
file = [NSFileHandle fileHandleForReadingAtPath:[documentsDirectory stringByAppendingPathComponent:@"/bucketListData.dat"]];
if(file == nil)
{
NSLog(@"Unable to create new file.");
}
}
[file writeData: encodedObject];
[file closeFile];
}
}
https://stackoverflow.com/questions/15752417
复制相似问题