简体   繁体   中英

Get the badge value from payload

i try to get the value of badge from payload (in payload: badge = 1;) but the value looks more like a memory address:

int badgeValue = (int)[notification objectForKey:@"badge"];
NSLog(@"Value of badge(middle) is : %d", badgeValue);
Value of badge(middle) is : 392626528 (this is from console)

Do you have some ideas why? Thanks in advance

In order to convert an NSNumber object to an int , you should be using the intValue selector:

int badgeValue = [[notification objectForKey:@"badge"] intValue];

When you cast it using just (int) , you are retrieving the integer representation of the object pointer (as you suspected).

This is because you are casting an object ( NSNumber ) to an int . All this does is put the memory reference of the object into the int.

What you need is something like this...

NSNumber *badgeValue = [notification objectForKey:@"badge"];
NSLog(@"Value of badge(middle) is : %@", badgeValue);

If you want to use an int (note, you shouldn't use int you should use NSInteger ) then something like this...

NSInteger badgeValue = [[notification objectForKey:@"badge"] integerValue];
NSLog(@"Value of badge(middle) is : %ld", (long)badgeValue);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM