[英]Can't get correct int value from fetching core data in iOS
I found a weird problem when fetching data from core data, the data i insert into core data is an Event object, the title, desc and localTime
can be shown correctly. 我从核心数据中获取数据时发现了一个奇怪的问题,我插入到核心数据中的数据是一个Event对象,标题,desc和localTime
可以正确显示。
However, the localUTC
, an int type value, for example, I insert -2 into core data, but after fetching, it becomes -30. 但是, localUTC
是一个int类型的值,例如,我在核心数据中插入了-2,但在获取后变为-30。 The more weirder thing is, in the instance of Event class, the value of localUTC
is -2, but when displaying it in UITextField
, it becomes -30 更奇怪的是,在Event类的实例中, localUTC
值为-2,但是在UITextField
显示时,它变为-30
the code i'm using is list below: 我使用的代码如下:
Event *event = [[utility getAllEvents] lastObject];
NSLog(@"%@", event); // log 1
_eventTitle.text = event.title;
_eventDesc.text = event.desc;
_eventTime.text = [NSString stringWithFormat:@"%@", event.localTime];
NSLog(@"%d", event.localUTC); // log 2
_eventUTC.text = [NSString stringWithFormat:@"%d", event.localUTC];
NSLog(@"%@", event); // log 3
NSLog(@"%d", event.localUTC); // log 4
The logs: 日志:
2015-01-21 20:13:21.186 EventTest[3695:248822] <NSManagedObject: 0x7fe22a445c60> (entity: Event; id: 0xd000000000200000 <x-coredata://BC13045B-499A-4DE6-828F-3BACCFF56CF0/Event/p8> ; data: <fault>) // log 1: NSLog(@"%@", event);
2015-01-21 20:13:21.188 EventTest[3695:248822] -30 // log 2: NSLog(@"%d", event.localUTC);
2015-01-21 20:13:21.189 EventTest[3695:248822] <NSManagedObject: 0x7fe22a445c60> (entity: Event; id: 0xd000000000200000 <x-coredata://BC13045B-499A-4DE6-828F-3BACCFF56CF0/Event/p8> ; data: {
desc = "abcdefg\n\nhhhh\n\nxx\nsd\nd\nfsad\nf";
localTime = "2015-01-21 08:56:31 +0000";
localUTC = "-2";
title = "New event111";
}) // log 3: NSLog(@"%@", event); --NOTICE, the value of localUTC is "-2", but when display, it becomes -30
2015-01-21 20:13:21.189 EventTest[3695:248822] -30 // log 4: NSLog(@"%d", event.localUTC);
UPDATE UPDATE
the code Event *event = [[utility getAllEvents] lastObject];
was cast from NSManagedObject, is this the problem? 是从NSManagedObject投射的,这是问题吗? [utility getAllEvents] returns NSArray of NSManagedObject (should be). [utility getAllEvents]返回NSManagedObject的NSArray(应该是)。 I'm a noob of Core Data, now sure if this causes the problem. 我是Core Data的菜鸟,现在确定这是否会导致问题。
UPDATE 2 The problem is fixed by using this code: 更新2通过使用以下代码可解决此问题:
[_eventUTC setText:[NSString stringWithFormat:@"%@", [event valueForKey:@"localUTC"]]];
but I don't know why it solves the problem, anyone can point it out? 但我不知道为什么它可以解决问题,有人可以指出吗?
Even though the core data model (your .xcdatamodeld file) has attributes such as Integer 16 / 32 /64 the values are stored in a NSNumber object. 即使核心数据模型(您的.xcdatamodeld文件)具有诸如Integer 16/32/64之类的属性,其值仍存储在NSNumber对象中。 Therefore you need to access your number values via the NSNumber object. 因此,您需要通过NSNumber对象访问数字值。
If you look carefully at the output of NSLog(@"%@", event);
如果您仔细查看NSLog(@"%@", event);
you will see that localUTC
is a NSString
: 您将看到localUTC
是NSString
:
localUTC = "-2"; localUTC =“ -2”;
In your another log statement you use %d
format specifier , which is used for integer types, not strings. 在另一个日志语句中,您使用%d
格式说明符 ,它用于整数类型,而不是字符串。
NSLog(@"%d", event.localUTC);
Change this to 更改为
NSLog(@"%@", event.localUTC);
or, if you are interested in the integer value 或者,如果您对整数值感兴趣
NSLog(@"%d", [event.localUTC integerValue]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.