Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从有效负载获取徽章值_Ios_Objective C_Xcode - Fatal编程技术网

Ios 从有效负载获取徽章值

Ios 从有效负载获取徽章值,ios,objective-c,xcode,Ios,Objective C,Xcode,我尝试从有效负载(在有效负载中:badge=1;)获取badge的值,但该值看起来更像内存地址: int badgeValue = (int)[notification objectForKey:@"badge"]; NSLog(@"Value of badge(middle) is : %d", badgeValue); Value of badge(middle) is : 392626528 (this is from console) 你知道为什么吗?提前感谢要将NSNumber对象转

我尝试从有效负载(在有效负载中:badge=1;)获取badge的值,但该值看起来更像内存地址:

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

你知道为什么吗?提前感谢

要将
NSNumber
对象转换为
int
,您应该使用
intValue
选择器:

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

当您仅使用
(int)
强制转换它时,您正在检索对象指针的整数表示形式(如您所怀疑的)。

这是因为您正在将对象(
NSNumber
)强制转换为
int
。所有这一切都是将对象的内存引用放入int

你需要的是这样的东西

NSNumber *badgeValue = [notification objectForKey:@"badge"];
NSLog(@"Value of badge(middle) is : %@", badgeValue);
NSInteger badgeValue = [[notification objectForKey:@"badge"] integerValue];
NSLog(@"Value of badge(middle) is : %ld", (long)badgeValue);
如果你想使用
int
(注意,你不应该使用
int
,你应该使用
NSInteger
,那么类似的东西

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

谢谢,我已经修改了代码。肯定是个好答案。