1

I'm actually working with Realm Objective-C, here is my problem:

I have a webservice which return this:

{
    "id": 1,
    "name": "Project 1",
    "summary": "",
    "description": "description project 1 ...",
    "amountTargeted": 0,
    "amountReached": 0,
    "beginDate": 1432044917000,
    ...
    ... 
}

When I try to parse the response using this method:

 [Project createOrUpdateInDefaultRealmWithValue:projectDictionary];

I have this error:

'RLMException', reason: 'Invalid value '0' for property 'amountReached''

In my model of project I declared amourReached like that:

@property NSInteger amountReached;

I did some tests, and this problem appears only for value is zero.

I think the problems come from this method who return false when it's zero:

static inline bool nsnumber_is_like_integer(NSNumber *obj)
{
        const char *data_type = [obj objCType];
        // FIXME: Performance optimization - don't use strcmp, use first char in data_type.
        return (strcmp(data_type, @encode(short)) == 0 ||
        strcmp(data_type, @encode(int)) == 0 ||
        strcmp(data_type, @encode(long)) ==  0 ||
        strcmp(data_type, @encode(long long)) == 0 ||
        strcmp(data_type, @encode(unsigned short)) == 0 ||
        strcmp(data_type, @encode(unsigned int)) == 0 ||
        strcmp(data_type, @encode(unsigned long)) == 0 ||
        strcmp(data_type, @encode(unsigned long long)) == 0);
}

Note : I add a breakpoint and try some log:

(lldb) p number
(NSNumber *) $6 = 0x15e76f50 @"0"
(lldb) po number
0
(lldb) p nsnumber_is_like_integer(number)(why ???)
(bool) $3 = false
(lldb) p nsnumber_is_like_integer(@0)(Why different of the first ?)
(bool) $5 = true
(lldb) p nsnumber_is_like_integer(@1)
(bool) $4 = true

Thanks in advance if you have any idea, is it a realm problem or a problem from my code ?

Hikosei
  • 193
  • 1
  • 11
  • is it possible that number is actually a string? can you also `p obj.objCType`? – segiddins May 20 '15 at 14:52
  • I can"t log "p obj.objCType", because it's an id, but my first log show that the object is an NSNumber. ((NSNumber *) $6 = 0x15e76f50 @"0") I tried to log po [obj class] and the result is __NSCFNumber – Hikosei May 20 '15 at 15:05

1 Answers1

0

I found the solution, I changed the type from NSInteger to double, and know every thing is ok.

@property NSInteger amountReached; //Old way
@property double amountReached; //Working way

I'm not sure it's normal, for me it should be ok with NSInteger, if someone as an explanation :).

Hikosei
  • 193
  • 1
  • 11
  • Is there any way you could share some sample code that would allow us to reproduce the issue? `NSInteger` should definitely work in this case and from your comments above, it seems like it's an NSNumber with a value of zero, which should be valid. – jpsim May 25 '15 at 16:45
  • I think the problem is from my json serializer, zero has a class NSDecimalNumber, so it's logical that it can't be a int. Now I have to found why it's an NSDecimalNumber and not a NSCFNumber. – Hikosei May 29 '15 at 15:41