ray
ray

Reputation: 193

Should I just use string as attribute type instead of integer in core-data?

I have a text filed which allows user to input numbers. This is what I did:

[_textfield setText:[NSString stringWithFormat:@"%d", [_textfield.text intValue]]];

Basically, I convert text in text filed to integer, then convert back to string. This will ensure the text is numbers only.

Now I need to store the text in _textfield into core data. I was wondering wether should use string as the attribute type or integer.

I know integer is a more sensible option. But to this case, every time the view is loaded, I need fetch this data and set to the _textfield. If I use integer as the attribute type, I have to convert to string each time. I know what I need to do is simply:

_textfield.text = [numberFromCoreData stringValue];

I don't need to compare, sort or do any arithmetic computation with that number, so should I just use string as the attribute type?

Upvotes: 1

Views: 840

Answers (4)

Tom Harrington
Tom Harrington

Reputation: 70946

It depends how you need to use that field. In almost every case, integer data should be stored as an integer type, but not always. You definitely want an integer type if you'll ever be using that field in a case where its numeric value counts in some way. That includes sorting (because it's a hell of a lot faster with numeric fields), comparing numeric values, or any kind of mathematical operation.

But there's are exceptions. For example, in some cases fields which initially seem to be inherently numeric turn out not to be so. Like a "size" field which is normally an integer. But on closer inspection it turns out that some sizes are specified as "8 - 10", "12 - 14", etc. This happened in one app I worked on a couple of years ago. In that case I ended up using two fields for the data-- a numeric "sortSize" that could be used for sorting, and a string "displaySize" that included the full string.

Upvotes: 1

Marcus S. Zarra
Marcus S. Zarra

Reputation: 46718

Integer searching is significantly faster than string searching. That is the single most compelling reason to use numbers in your persistence layer. Numbers also can sort differently than strings.

For performance reasons I would never use a string when I know the value is always going to be an integer. Control the input, force it to only accept numbers and keep your data integrity.

Upvotes: 5

Jeef
Jeef

Reputation: 27275

Honestly I can't think of a compelling reason. Strings in general take up more storage space than Integers but in the modern world of computing this isn't much of an issue. If you aren't really pushing you processor too hard I'd go with what is convenient.

From the most basic way of thinking about it an integer is a number but for a string the computer needs to know when the string ends, starts, and what is in it so its a little bigger.

Upvotes: 0

Vincent
Vincent

Reputation: 188

It's probably not what you want but why don't you use a keyboard type "number Pad" for your textfield? With that, you would be sure that you have only numbers into your textfield.

Upvotes: 0

Related Questions