Search code examples
iosobjective-cmemory-managementnsstringnsinteger

Efficiency NSString vs NSInteger/int - only for textual representation


I'd like to know if it would make any sense to cast/convert a number, parsed from a csv file, e.g. customer id, to a NSString? Or maybe better a simple int? As I'm quite new to obj-c, I'm not really sure, wether to consistently use the NSxyz types, or use what I'm used to, coming from Java/C/C++.

Actually the value only is stored in a variable, and then loaded into some textfields (which again would imply a conversion back to NSString I guess?).

Would there be any benefit in less memory being used? Let's assume the ids had 6 digits, parsing roughly 10'000-100'000 customers. Same would apply to smaller numbers, e.g. the addresses street number.


Solution

  • In a string, 1 letter == 1 byte, so if you have 6 digits, you are occupying 6 bytes.

    An int instead takes generally 2 (short), 3 or 4 (long) bytes. It can arrive also to 8 bytes with an int_64. But, you are limited because for example in the 2 byte case (16 bit) you can consider 2^16 numbers.

    In your case you could use an int, but i would use an NSString, also because you need it in your textfield.

    An NSInteger is an int. An NSUInteger is an unsigned int.

    An NSNumber is an Object (so no primitive) which can store an int, a float, a double or a boolean. So you can store many type of primitive in this type of variable and then use the appropriate:

    [number floatValue];
    [number boolValue];
    ...