Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save RockinPaul/5ac8a2fecad8b883ccc99ec0017cdffa to your computer and use it in GitHub Desktop.
Save RockinPaul/5ac8a2fecad8b883ccc99ec0017cdffa to your computer and use it in GitHub Desktop.
Storing CGPoint, CGSize and CGRect in Collections using NSValue
// CGPoint converted to NSValue
CGPoint point = CGPointMake(9, 9);
NSValue *pointObj = [NSValue valueWithCGPoint:point];
// CGSize converted to NSValue
CGSize size = CGSizeMake(100, 100);
NSValue *sizeObj = [NSValue valueWithCGSize:size];
// CGRect from CGPoint and CGSize converted to NSValue
CGRect rect = CGRectMake(point.x, point.y, size.width, size.height);
NSValue *rectObj = [NSValue valueWithCGRect:rect];
// Add the objects to a collection
NSArray *array = [NSArray arrayWithObjects:pointObj, sizeObj, rectObj, nil];
// Print to console the objects in the collection
NSLog(@"array content: %@", array);
// Restore from NSValue to C structures
CGPoint pointRestored = [pointObj CGPointValue];
CGSize sizeRestored = [sizeObj CGSizeValue];
CGRect rectRestored = [rectObj CGRectValue];
// Print restored values
NSLog(@"point x:%f y:%f", pointRestored.x, pointRestored.y);
NSLog(@"size w:%f h:%f", sizeRestored.width, sizeRestored.height);
NSLog(@"rect x:%f y:%f w:%f h:%f", rectRestored.origin.x, rectRestored.origin.y,
rectRestored.size.width, rectRestored.size.height);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment