Saving an NSArray of custom objects

2019-02-19 14:49发布

问题:

I've created a subclass of UIImage (UIImageExtra) as I want to include extra properties and methods.

I have an array that contains instances of this custom class.However when I save the array, it appears the extra data in the UIImageExtra class is not saved.

UIImageExtra conforms to NSCoding, but neither initWithCoder or encodeWithCoder are called, as NSLog statements I've added aren't printed.

My method to save the array looks like this:

- (void)saveIllustrations {
if (_illustrations == nil) {
    NSLog(@"Nil array");
    return;
}

[self createDataPath];
//Serialize the data and write to disk
NSString *illustrationsArrayPath = [_docPath stringByAppendingPathComponent:kIllustrationsFile];
NSMutableData *data = [[NSMutableData alloc] init];
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
[archiver encodeObject:_illustrations forKey:kIllustrationDataKey];
[archiver finishEncoding];
[data writeToFile:illustrationsArrayPath atomically: YES];
}

And the UIImageExtra has the following delegate methods for saving:

    #pragma mark - NSCoding

- (void)encodeWithCoder:(NSCoder *)aCoder {
    NSLog(@"Encoding origin data!");
    [super encodeWithCoder:aCoder];
    [aCoder encodeObject:originData forKey:kOriginData];
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:(NSCoder *) aDecoder]) {
        NSLog(@"Decoding origin data");
        self.originData = [aDecoder decodeObjectForKey:kOriginData];
    }
    return self;
}

My code to create the array in the first place looks like this (in case that offers any clues)

        for (NSDictionary *illustrationDict in illustrationDicts) {
        NSString *illustrationString = [illustrationDict objectForKey:@"Filename"];
        NSNumber *xCoord = [illustrationDict objectForKey:@"xCoord"];
        NSNumber *yCoord = [illustrationDict objectForKey:@"yCoord"];
        UIImageExtra *illustration = (UIImageExtra *)[UIImage imageNamed:illustrationString];

        //Scale the illustration to size it for different devices
        UIImageExtra *scaledIllustration = [illustration adjustForResolution];
        NSValue *originData = [NSValue valueWithCGPoint:CGPointMake([xCoord intValue], [yCoord intValue])];
        [scaledIllustration setOriginData:originData];
        [self.illustrations addObject:scaledIllustration];
    }

Or am I just going about saving this data the wrong way? Many thanks.

回答1:

Your code to initialize the array is not actually creating instances of your UIImageExtra subclass.

UIImageExtra *illustration = (UIImageExtra *)[UIImage imageNamed:illustrationString];

returns a UIImage. Casting it doesn't do what you were intending.

UIImageExtra *scaledIllustration = [illustration adjustForResolution];

is still just a UIImage.

One straightforward-but-verbose way to approach this would be to make UIImageExtra a wrapper around UIImage. The wrapper would have a class method for initializing from a UIImage:

+ (UIImageExtra)imageExtraWithUIImage:(UIImage *)image;

And then every UIImage method you want to call would have to forward to the wrapped UIImage instance-- also being careful to re-wrap the result of e.g. -adjustForResolution lest you again end up with an unwrapped UIImage instance.

A more Objective-C sophisticated approach would be to add the functionality you want in a Category on UIImage, and then use method swizzling to replace the NSCoding methods with your category implementations. The tricky part of this (apart from the required Objective-C runtime gymnastics) is where to store your "extra" data, since you can't add instance variables in a category. [The standard answer is to have a look-aside dictionary keyed by some suitable representation of the UIImage instance (like an NSValue containing its pointer value), but as you can imagine the bookkeeping can get complicated fast.]

Stepping back for a moment, my advice to a new Cocoa programmer would be: "Think of a simpler way. If what you are trying to do is this complicated, try something else." For example, write a simple ImageValue class that has an -image method and an -extraInfo method (and implements NSCoding, etc.), and store instances of that in your array.



回答2:

You can't add objects to an NSArray after init. Use NSMutableArray, that might be the issue.