0
이 코드는 오류를 나타내지 않지만 "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;
}
NSLog
표시 :
Creating objects manually
Objects created and archived
그러나 어떤 파일이 생성되지 않습니다.