Ios – NSNotification userinfo example


I have an array of objects that are positioned using CGPoints . At certain times in my app, an object in the array needs to notify other non-arrayed objects of its position. I understand that NSNotification is the best way to go, but I cant find a decent example of a 'sender' and 'reciever' for the notification that wraps and unwraps a CGPoint as userinfo. Can anyone help?

Best Solution

In Cocoa Touch (but not Cocoa), CGPoints can be wrapped and unwrapped with

+ (NSValue *)valueWithCGPoint:(CGPoint)point
- (CGPoint)CGPointValue

NSValues can be stored in the NSDictionary passed as the userinfo parameter.

For example:

NSValue* value = [NSValue valueWithCGPoint:mypoint];
NSDictionary* dict = [NSDictionary dictionaryWithObject:value forKey:@"mypoint"];

And in your notification:

NSValue* value = [dict objectForKey:@"mypoint"];
CGPoint newpoint = [value CGPointValue];