The NSKeyedArchiverRootObject archive always returns NO

I am new to C object and am writing some simple programs to get to know each other.

Trying to save / read information using NSKeyedArchiver, but every time I do this, I cannot create a .plist file. I looked around and could not find the answer. Can someone help me understand why โ€œsomething went wrong ...โ€ is always the final result?

Thanks!

// main.m #import <Foundation/Foundation.h> #import "Domicile.h" void CreateAndArchive(NSMutableArray* objs, NSURL *saveLocation) { NSLog(@"Creating DOMICILE objects..."); Domicile *now = [[Domicile alloc] init]; Domicile *then = [[Domicile alloc] init]; now.HouseName = @"place1; now.Address = @"506 99th street"; now.Residents = [[NSMutableArray alloc] initWithObjects: @"Erik", @"Jeremiah", @"Alex", @"Ryan", @"Kyle", @"Connor", nil]; now.City = @"aCity"; now.State = @"CA"; now.Zip = @"12345"; then.HouseName = @"place2"; then.Address = @"1011 E Fairmont street"; then.Residents = [[NSMutableArray alloc] initWithObjects: @"Erik", @"Asa", @"Ryan", @"Gabe", @"Josh", nil]; then.City = @"anotherCity"; then.State = @"VA"; then.Zip = @"54321"; NSMutableArray *saveThis = [[NSMutableArray alloc] initWithObjects: now, then, nil]; NSLog(@"saving to %@", [saveLocation absoluteString]); if ([NSKeyedArchiver archiveRootObject:saveThis toFile:[saveLocation absoluteString]]) { NSLog(@"SAVED!"); } else{ NSLog(@"Something went wrong..."); } } NSMutableArray* Load(NSURL *location){ NSLog(@"Loading data from %@", [location absoluteString]); return [NSKeyedUnarchiver unarchiveObjectWithFile: [location absoluteString]]; } int main(int argc, const char * argv[]) { @autoreleasepool { NSURL *dir = [[NSFileManager defaultManager] URLForDirectory:NSDesktopDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; NSURL *file = [dir URLByAppendingPathComponent:@"data.plist"]; NSMutableArray *domiciles = [[NSMutableArray alloc] init]; if ([[NSFileManager defaultManager] fileExistsAtPath:[file absoluteString]]) { domiciles = Load(file); NSLog(@"data loaded!"); NSLog(@"displaying for your convenience:"); for (Domicile *dom in domiciles) { NSLog(@"\n\n%@", dom); } } else { CreateAndArchive(domiciles, file); } } return 0; } 

 // Domicile.m #import "Domicile.h" @implementation Domicile -(instancetype)initWithCoder:(NSCoder *)coder { self = [super init]; if (self) { _HouseName = [coder decodeObjectForKey:@"name"]; _Residents = [coder decodeObjectForKey:@"residents"]; _Address = [coder decodeObjectForKey:@"address"]; _City = [coder decodeObjectForKey:@"city"]; _State = [coder decodeObjectForKey:@"state"]; _Zip = [coder decodeObjectForKey:@"zip"]; } return self; } -(void) encodeWithCoder:(NSCoder *)aCoder{ [aCoder encodeObject:self.HouseName forKey:@"name"]; [aCoder encodeObject:self.Residents forKey:@"residents"]; [aCoder encodeObject:self.Address forKey:@"address"]; [aCoder encodeObject:self.City forKey:@"city"]; [aCoder encodeObject:self.State forKey:@"state"]; [aCoder encodeObject:self.Zip forKey:@"zip"]; } -(NSString *) description{ NSString *desc = [[NSString alloc] initWithFormat: @"House : %@\n%@\n%@, %@\n%@\n", self.HouseName, self.Address, self.City, self.State, self.Zip]; return desc; } @end 
+6
source share
1 answer

If you want to convert the NSURL file for use in a method that needs a path, you should use the path method, not the absoluteString method. As stated in the path method documentation:

If the URL object contains the URL of the file or file (as determined using isFileURL ), the return value of this [ path ] method is suitable for input to the NSFileManager or NSPathUtilities . If the path has a trailing slash, it loses.

The absoluteString method returns a string that looks like file:///Users/... , but you do not want the file:// schema prefix in these methods to look for a path for some resource. The path method returns your file path, not encumbered with the file:// prefix.

+22
source

Source: https://habr.com/ru/post/973794/


All Articles