[英]iOS 5 NSNumber Bug
I will become crazy with this bug ! 这个虫子我会变得疯狂! I have this piece of code which works well on iOS 4.3 我有这段代码在iOS 4.3上运行良好
newRegistered.ticket_id = [NSNumber numberWithInt:
[curRegistered objectForKey:@"ticket_id"] intValue]];
If I do : 如果我做 :
NSLog(@"ID before : %d, ID after : %d",
[curRegistered objectForKey:@"ticket_id"] intValue], [newRegistered.ticket_id intValue]);
I Have on iOS 4.3 : 我在iOS 4.3上:
ID before : 35459, ID after : 35459 ID之前:35459,ID之后:35459
And on iOS 5.0 : 在iOS 5.0上:
ID before : 35459, ID after : -30077 ID之前:35459,ID之后:-30077
Please help ! 请帮忙 ! Thank you for advance. 谢谢你提前。
##EDIT :## I have investigated a bit more the problem. ##编辑:##我已经调查了一下这个问题。 It seems the problem comes from assigning a NSNumber in CoreData. 似乎问题来自于在CoreData中分配NSNumber。 I should have specified that the piece of code above is used with CoreData (newRegistered is a NSManagedObject, and curRegistered is a NSDictionary containing JSON Data (only strings, never int or long...)). 我应该指定上面的代码段与CoreData一起使用(newRegistered是一个NSManagedObject,而curRegistered是一个包含JSON数据的NSDictionary(只有字符串,永远不是int或long ...))。
Here is the specific problem : 这是具体问题:
long long test = 789654;
Registereds *a = [NSEntityDescription insertNewObjectForEntityForName:@"Registereds" inManagedObjectContext:self.managedObjectContext];
a.ticket_id = [NSNumber numberWithLongLong:test];
NSLog(@"%lld -> %@", test, a.ticket_id); // Displays 789654 -> 3222 on iOS 5 and 789654 -> 789654 on iOS 4.3
NSNumber *ticket_id = [NSNumber numberWithLongLong:test];
NSLog(@"%lld -> %@", test, ticket_id); // Displays 789654 -> 789654
So it seems the problem comes from the Registereds object (which is a NSManagedObject, like the newRegistered in the previous example). 所以似乎问题来自Registereds对象(它是一个NSManagedObject,就像上一个例子中的newRegistered)。
Do you have any idea why this happens? 你知道为什么会这样吗? I have never seen something like that in more than 2 years of coding, this bug really makes me think I don't know how to code... 我在2年多的编码中从未见过这样的东西,这个bug真的让我觉得我不知道怎么编码......
A hint: 789654 truncated to 16 bits is 3222. 提示:截断为16位的789654为3222。
I have tried to replicate your problem with iOS 5. The only way I can get NSLog(@"%lld -> %@", test, a.ticket_id)
to display '789654 -> 3222' is if I set the ticket_id attribute type to Integer 16
. 我试图用iOS 5复制你的问题。我可以获得NSLog(@"%lld -> %@", test, a.ticket_id)
显示'789654 - > 3222'的唯一方法是设置ticket_id属性输入到Integer 16
。 If I set it to Integer 32
or Integer 64
then the expected value is displayed. 如果我将其设置为Integer 32
或Integer 64
则会显示预期值。
Check the attribute type for ticket_id
in the data model editor. 在数据模型编辑器中检查ticket_id
的属性类型。 Because you are using long long
I assume it should be set to Integer 64
. 因为你使用long long
我认为它应该设置为Integer 64
。 From what you are showing I would suspect it is set to Integer 16
. 从你所展示的我怀疑它被设置为Integer 16
。 If not, then the value is getting truncated to 16 bits somewhere else. 如果没有,则该值在其他地方被截断为16位。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.