Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding CGPoint struct with NSCoder

Tags:

cocoa

How do you encode and decode a CGPoint struct using NSCoder?

like image 722
mikechambers Avatar asked Jan 15 '09 19:01

mikechambers


2 Answers

To encode:

CGPoint point = /* point from somewhere */
NSValue *pointValue = [NSValue value:&point withObjCType:@encode(CGPoint)];
[coder encodeObject:pointValue forKey:@"point"];

To decode:

NSValue *decodedValue = [decoder decodeObjectForKey:@"point"];
CGPoint point;
[decodedValue getValue:&point];
like image 96
sbooth Avatar answered Oct 07 '22 22:10

sbooth


Just an update for iOS developers. You can do the following in Cocoa Touch (but not in Cocoa):

[coder encodeCGPoint:myPoint forKey:@"myPoint"];
like image 28
Eric G Avatar answered Oct 07 '22 23:10

Eric G