NSNotification userinfo example?

I have an array of objects that are located using CGPoints. At a certain time in my application, an object in an array should notify other unarmored objects of its position. I understand that NSNotification is the best way to go, but I cannot find a decent example of a "sender" and a "receiver" for a notification that wraps and deploys CGPoint as userinfo. Can anyone help?

+5
ios objective-c cocoa-touch xcode cocoa
source share
2 answers

In Cocoa Touch (but not Cocoa), CGPoints can be wrapped and expanded using

+ (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 notice:

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

The userinfo object passed along with the notification is simply an NSDictionary. Probably the easiest way to pass CGPoint to userinfo would be to wrap the X and Y coordinates in NSNumbers using -numberWithFloat :. Then you can use setObject: forKey: in the userinfo dictionary, using Xpos and Ypos as keys, for example.

Perhaps you could translate this into a pretty category in NSMutableDictionary using methods like setFloat: forKey or something ...

+1
source share

All Articles