我不認爲這是可能的序列化塊。
我會將數據封裝到一個類中,並實現NSCoding
協議。例如。
@interface Promotion :NSObject<NSCoding> { // protocol might be better
}
-(void)calculatePromotion;
@end
然後
@interface PromotionX : Promotion {
... data needed for a promotion of type X ...
}
-initWithDataA: (A*)a andDataB:(B*) b
@end
現在您需要實現各種事情
@implementation PromotionX
-initWithDataA: (A*)a and DataB:(B*)b{
... save a and b to the ivars ...
}
-(void)calculatePromotion{
... do something with a and b
}
#pragma mark Serialization support
-initWithCoder:(NSCoder*)coder{
... read off a and b from a coder ...
}
-(void)encodeWithCoder:(NSCoder*)coder{
... write a and b to a coder ...
}
@end
同樣的促銷類型Y,Z,等現在可以保存到一個文件中,或NSData
,使用NSKeyedArchiver
。 然後你就可以
NSData* data = ... somehow get the data from the file/CoreData etc...
Promotion* promotion = [NSKeyedUnarchiver unarchiveObjectWithData:data];
[promotion calculatePromotion];
復活晉級對象,而不參照特定類型(X,Y,Z)序列化一般,讀this Apple doc。
非常感謝您的建議,這似乎是一個很好的選擇。 – glenc 2010-07-25 15:32:44