Objective c 无法将文件存档和取消存档到文档文件夹
这段代码没有给出任何错误,但它既没有创建“plist”文件,也没有从中读取Objective c 无法将文件存档和取消存档到文档文件夹,objective-c,nskeyedarchiver,nskeyedunarchiver,Objective C,Nskeyedarchiver,Nskeyedunarchiver,这段代码没有给出任何错误,但它既没有创建“plist”文件,也没有从中读取 [NSKeyedArchiver archiveRootObject:employees toFile:@"/Users/Documents/employees.plist"]; 我编写了一个代码,指定文件夹的直接路径,它创建并写入文件,尽管它无法从中读取 [NSKeyedArchiver archiveRootObject:employees toFile:@"/Users/Documents/employees.p
[NSKeyedArchiver archiveRootObject:employees toFile:@"/Users/Documents/employees.plist"];
我编写了一个代码,指定文件夹的直接路径,它创建并写入文件,尽管它无法从中读取
[NSKeyedArchiver archiveRootObject:employees toFile:@"/Users/Documents/employees.plist"];
下面的代码无法写入或读取文件
我一行一行地按照上面的说明去做
我复制了准确的Employee.h
和Employee.m
文件,也没有错误
谢谢你的帮助
#import <Foundation/Foundation.h>
#import "Employee.h"
NSString* getPropertyListPath() {
NSURL *documentDir = [[NSFileManager defaultManager]
URLForDirectory:NSDocumentationDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create:NO
error:nil];
NSURL *plist = [documentDir URLByAppendingPathComponent:@"EmployeeFile.plist"];
return plist.path;
}
void createAndArchiveObjects (NSString *filePath) {
NSLog(@"Creating objects manually");
Employee *john = [[Employee alloc] init];
[john setFirstName:@"A"];
[john setLastName:@"J"];
[john setEmployeeNumber:12345];
[john setHireDate:[NSDate date]];
NSMutableArray *employees = [[NSMutableArray alloc] init];
[employees addObject:john];
[NSKeyedArchiver archiveRootObject:employees toFile:filePath];
NSLog(@"Objects created and archived");
}
void unarchiveSavedObjects(NSString *filePath) {
NSMutableArray *employees = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
for (Employee *e in employees) {
NSLog(@"The unarchived, reconstituted object is %@", e);
}
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString *path = getPropertyListPath();
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
unarchiveSavedObjects(path);
} else {
createAndArchiveObjects(path);
}
}
return 0;
}
但是没有创建任何文件。您使用的是NSDocumentationDirectory而不是NSDocumentDirectory 使用
NSURL *documentDir = [[NSFileManager defaultManager]
URLForDirectory:NSDocumentDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create:NO
error:nil];