0

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.

  • 2
    The ids are not numbers. They happen to be numeric but they are strings. Keep them as strings. – rmaddy May 24 '14 at 00:05

1 Answers1

-1

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];
...
Matteo Gobbi
  • 17,697
  • 3
  • 27
  • 41
  • 2
    "1 letter = 1 byte" is only true for some letters in some encodings. – Chuck May 24 '14 at 00:14
  • sure. But we are in super-base modality. Don't you see? :) And also it is better say: is not true for some letters in some encodings. – Matteo Gobbi May 24 '14 at 00:15
  • OK, will stick to NSStrings then. And thanks for the "super-base modality", made me smile haha :) – little_endian May 24 '14 at 00:30
  • 1 letter does not necessarily take up 1 byte. It depends on encoding, composition and the represented character. – uchuugaka Jul 04 '14 at 03:34
  • One letter in an NSString is often 1, sometimes 2 bytes. However, an NSString object on its own uses a considerable amount of memory. The NSString* alone uses as much memory as an NSInteger. – gnasher729 Oct 29 '14 at 16:27
  • There are so many things wrong about this. For example, an NSInteger is either an int or a long, with 32 or 64 bits. The big advantage of using NSInteger instead of NSString to store an integer is that you _know_ the value is an integer and not for example the string "bla bla bla". – gnasher729 Oct 29 '14 at 16:29