1 сент. 2016 г.

Objectve C: немножно макросов для облегчения имплементации протокола NSCoding

Для поддержки протокола NSCoding в Objective C класс должен реализовать два метода для (де)сериализации каждого поля объекта. Ниже привожу набор макросов, которые упрощают этот процесс:

In order to support NSCoding protocol in Objective C class must implement 2 methods to (de)serialize every object's field. Below is set of macro to make it little easy:


#define encObj(__name__) {if(__name__) {[aCoder encodeObject:__name__ forKey:@#__name__];} }
#define encPoint(__name__) [aCoder encodeCGPoint:__name__ forKey:@#__name__];
#define encSize(__name__) [aCoder encodeCGSize:__name__ forKey:@#__name__];

#define encInt(__name__) [aCoder encodeInt:__name__ forKey:@#__name__];
#define encFloat(__name__) [aCoder encodeFloat:__name__ forKey:@#__name__];
#define encDouble(__name__) [aCoder encodeDouble:__name__ forKey:@#__name__];
#define encBool(__name__) [aCoder encodeBool:__name__ forKey:@#__name__];

////////////////////
////////////////////
#define decObj(__name__) __name__ = [[aDecoder decodeObjectForKey:@#__name__] retain];
#define decInt(__name__) __name__ = [aDecoder decodeIntForKey:@#__name__];
#define decFloat(__name__) __name__ = [aDecoder decodeFloatForKey:@#__name__];
#define decDouble(__name__) __name__ = [aDecoder decodeDoubleForKey:@#__name__];
#define decBool(__name__) __name__ = [aDecoder decodeBoolForKey:@#__name__];
#define decPoint(__name__) __name__ = [aDecoder decodeCGPointForKey:@#__name__];
#define decSize(__name__) __name__ = [aDecoder decodeCGSizeForKey:@#__name__];