tags:

views:

95

answers:

1

How can I cast the object in a NSNotification to an integer and to a string? When I log the notification to the console I get...

NSConcreteNotification 0x20af70 {name = kMessageCountNotification; object = 1}

But when I set the text value of a UILabel to that value, I get 72855952

A: 

Documentation of class that has sent this notification should contain information what type of notification is this and what does it send in its object.

You can access object sent using object method:

ClassOfObjectSent *obj = [notification_you_have_received object];

Note that it is a pointer to an object, not a scalar value. If object is NSNumber, you can get int this way:

int i = [obj intValue];

If it's a NSString you could use it directly or – safer – create your own copy (if you use Cocoa Touch or build non-GC Mac app, you'll have to release the copy).

NSString *txt = [obj copy];
porneL
You'll also have to `release` the copy if you're targeting a system without garbage collection, or building an application extension that supports but doesn't require garbage collection.
dreamlax