Problem parsing integers / NSNumbers

2 views
Skip to first unread message

Alastair

unread,
Feb 16, 2009, 5:11:23 PM2/16/09
to ObjectiveResource
When reading values from the rails application through JSON the
correct value of account_id is passed through in the JSON but when
parsing the value it ends up being a ridiculously high number. The id
being passed is 1 and the value after parsing is 18224352. As first I
thought this might be a memory address but it doesn't match. Any
Ideas?

Josh Vickery

unread,
Feb 16, 2009, 6:05:57 PM2/16/09
to objectiv...@googlegroups.com


Can you include a snippet of the JSON, and the Objective-C that you
are using to print out/display the value?

The number you list sounds like it's a float being displayed/printed as an int.

I wrote some code to test this out with the sample application and
uploaded it as a branch on github:

http://github.com/yfactorial/objectiveresource/commit/0c91c163d563a75d1ec0fadc63258aa1b4bc4424

Both decimals and integers seem to display correctly when using XML or
JSON. You do have to know the type
though, since NSNumber can hold both.

This section of the commit illustrates printing NSNumber as a float or
an int, based on the type coming from Rails:

http://github.com/yfactorial/objectiveresource/commit/0c91c163d563a75d1ec0fadc63258aa1b4bc4424#L2R66

I hope this helps. I'll file a ticket on Lighthouse to do a better job
of updating the sample app to illustrate handling different data
types.

Josh

Alastair

unread,
Feb 16, 2009, 6:33:24 PM2/16/09
to ObjectiveResource
Here's a sample of the JSON object being parsed

{"goal": {"name": "Save the world", "completed_at": "", "updated_at":
"2008/11/18 22:40:21 +0000", "team_member_id": 1, "account_id": 1,
"business_area_id": 1, "id": 13, "blurb": "", "due_on": "",
"created_at": "2008/11/18 22:40:21 +0000"}}

and the Objective-C

NSLog(@"Team Member id for goal is: %f", [[goals objectAtIndex:i]
teamMemberId]); // This prints out 0.000000
NSLog(@"Team Member id for goal is: %d", [[goals objectAtIndex:i]
teamMemberId]); // This gives random values like the earlier
example

where teamMemberId is a NSNumber.

- Alastair


On Feb 17, 12:05 pm, Josh Vickery <vicke...@gmail.com> wrote:
> On Mon, Feb 16, 2009 at 5:11 PM, Alastair <aperso...@gmail.com> wrote:
>
> > When reading values from the rails application through JSON the
> > correct value of account_id is passed through in the JSON but when
> > parsing the value it ends up being a ridiculously high number. The id
> > being passed is 1 and the value after parsing is 18224352. As first I
> > thought this might be a memory address but it doesn't match. Any
> > Ideas?
>
> Can you include a snippet of the JSON, and the Objective-C that you
> are using to print out/display the value?
>
> The number you list sounds like it's a float being displayed/printed as an int.
>
> I wrote some code to test this out with the sample application and
> uploaded it as a branch on github:
>
> http://github.com/yfactorial/objectiveresource/commit/0c91c163d563a75...
>
> Both decimals and integers seem to display correctly when using XML or
> JSON. You do have to know the type
> though, since NSNumber can hold both.
>
> This section of the commit illustrates printing NSNumber as a float or
> an int, based on the type coming from Rails:
>
> http://github.com/yfactorial/objectiveresource/commit/0c91c163d563a75...

Josh Vickery

unread,
Feb 16, 2009, 6:40:58 PM2/16/09
to objectiv...@googlegroups.com
NSNumber is an Object, so you could use the %@ format string to print
the description, but since you know that it's an integer, it would
make sense to print the int value

So, replace:


> NSLog(@"Team Member id for goal is: %d", [[goals objectAtIndex:i]
> teamMemberId]);

with:
> NSLog(@"Team Member id for goal is: %d", [[[goals objectAtIndex:i] teamMemberId] intValue]);

and you should see the value you are looking for.

Alastair

unread,
Feb 17, 2009, 4:45:00 PM2/17/09
to ObjectiveResource
That worked perfectly, Thanks Josh
Reply all
Reply to author
Forward
0 new messages