簡體   English   中英

保存自定義對象的NSArray

[英]Saving an NSArray of custom objects

我已經創建了UIImage(UIImageExtra)的子類,因為我想包括其他屬性和方法。

我有一個包含該自定義類的實例的數組。但是,當我保存該數組時,似乎沒有保存UIImageExtra類中的額外數據。

UIImageExtra符合NSCoding,但不會調用initWithCoder或encodeWithCoder,因為不會打印我添加的NSLog語句。

我保存數組的方法如下所示:

- (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];
}

UIImageExtra具有以下委托保存方法:

    #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;
}

我最初創建數組的代碼如下(如果有任何線索)

        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];
    }

還是我會以錯誤的方式保存此數據? 非常感謝。

您用來初始化數組的代碼實際上並未創建UIImageExtra子類的實例。

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

返回一個UIImage。 投射不符合您的預期。

UIImageExtra *scaledIllustration = [illustration adjustForResolution];

仍然只是一個UIImage。

解決這個問題的一種直接但冗長的方法是使UIImageExtra成為UIImage的包裝 包裝器將具有用於從UIImage初始化的類方法:

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

然后,要調用的每個UIImage方法都必須轉發到包裝的UIImage實例-還應小心地重新包裝例如-adjustForResolution的結果,以免再次遇到未包裝的UIImage實例。

一種更Objective-C復雜的方法是將所需的功能添加到UIImage的“ 類別 ”中,然后使用“ 方法模糊處理”將NSCoding方法替換為您的類別實現。 其中的棘手部分(除了必需的Objective-C運行時體操之外)是用於存儲“額外”數據的位置,因為您不能在類別中添加實例變量。 [標准答案是使用由UIImage實例的某些適當表示(例如包含指針值的NSValue)作為鍵的后備字典,但是您可以想象簿記會很快變得復雜。]

退后一會,我對新的Cocoa程序員的建議是:“想想一種更簡單的方法。如果您想做的就是這么復雜,請嘗試其他方法。” 例如,編寫一個簡單的ImageValue類,該類具有-image方法和-extraInfo方法(並實現NSCoding等),並將其實例存儲在數組中。

初始化后不能將對象添加到NSArray。 使用NSMutableArray,這可能是問題所在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM