Reputation: 145
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.
However, the localUTC
, an int type value, for example, I insert -2 into core data, but after fetching, it becomes -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
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
the code Event *event = [[utility getAllEvents] lastObject];
was cast from NSManagedObject, is this the problem? [utility getAllEvents] returns NSArray of NSManagedObject (should be). I'm a noob of Core Data, now sure if this causes the problem.
UPDATE 2 The problem is fixed by using this code:
[_eventUTC setText:[NSString stringWithFormat:@"%@", [event valueForKey:@"localUTC"]]];
but I don't know why it solves the problem, anyone can point it out?
Upvotes: 0
Views: 212
Reputation: 3109
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. Therefore you need to access your number values via the NSNumber object.
Upvotes: 1
Reputation: 7944
If you look carefully at the output of NSLog(@"%@", event);
you will see that localUTC
is a NSString
:
localUTC = "-2";
In your another log statement you use %d
format specifier, which is used for integer types, not strings.
NSLog(@"%d", event.localUTC);
Change this to
NSLog(@"%@", event.localUTC);
or, if you are interested in the integer value
NSLog(@"%d", [event.localUTC integerValue]);
Upvotes: 0