NSCoding Protocol Question

I want to add the archiving protocol (NSCoding) to my model class, and then I implement both encodeWithCoder:(NSCoder*)coderand methods initWithCoder:(NSCoder*)coder. MyModelClass has 2 instance variables (NSString and NSImage), so I use the method encodeObject:(id)object forKey:(NSString*)stringto encode the object and value for a specific key. But I keep getting the error:

*** -encodeObject:forKey: only defined for abstract class. Define -[NSArchiver encodeObject:forKey:]!

here is my code for NSCoding methods:

- (id)initWithCoder:(NSCoder *)coder {
 [super init];
 mainPath = [[coder decodeObjectForKey:@"mainPath"] retain];
 icon = [[coder decodeObjectForKey:@"icon"] retain];

 return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
 NSLog(@"encode with coder is called");
 [coder encodeObject:mainPath forKey:@"mainPath"];
 [coder encodeObject:icon forKey:@"icon"];

}

And here is what I call them in the controller class:

id object = [assetArray objectAtIndex: [[rows lastObject] intValue]];

 if ([object isKindOfClass:[ItemAssetModel class]])
  NSLog(@"object is correct");
 else
  return NO;

 NSData *data = [NSArchiver archivedDataWithRootObject: object];

if I change encodeObject:(id)obj forKey:(NSString*)strto encodeObject:(id)obj, the error stops, but as a result, the archived data does not copy the value of the instance variable (cmiiw). Did I miss something?

thank. Hebba

+3
source share
1 answer

NSKeyedArchiver NSArchiver.

+7

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


All Articles