zztop
zztop

Reputation: 791

IOS/JSON: Store float value in object variable

An API I am using returns some JSON that includes many decimal values between 0 and 1--basically probabilities--such as .422323.

I need to store this value in Core Data. My understanding is that the correct variable type for that type of number is a float value (or possibly double.)

However, in order to store values in core data, I am having to wrap them in NSNumber wrappers using something like:

[newPoi setValue:[NSNumber numberWithFloat:someFloat] forKey:@"yCoordinate"];

Given this overhead which will be considerable as I have many operations that use these numbers, is there a better variable type for storing values such as .422323?

Upvotes: 0

Views: 202

Answers (1)

ystack
ystack

Reputation: 1805

Float attribute type should serve you well enough.
Yes, by default Core Data wraps all primitive types to provide an object oriented interface (which is a desired behaviour commonly).
More than an overhead, for syntactical sugaring you can choose to use Scalar Type attributes.
You can toggle to Use Scalar Type in the Data Model Inspector panel.

Use Scalar Type

Now, on creating the ManagedObject Subclass from Editor menu, you'll get:
@property (nonatomic) float yCoordinate;

Upvotes: 1

Related Questions