ブロックをシリアライズすることはできないと思います。
データをクラスにカプセル化し、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
。次に、特定のタイプ(X、Y、Z)を参照せずにプロモーションオブジェクトを復活させることができます
NSData* data = ... somehow get the data from the file / CoreData etc...
Promotion* promotion = [NSKeyedUnarchiver unarchiveObjectWithData:data];
[promotion calculatePromotion];
一般的なシリアル化については、この Apple docをお読みください。