Objective-C: Convert NSString Object to Long Primitive

Discussion in 'Mac Programming' started by Kelmon, Aug 3, 2006.

  1. macrumors 6502a

    Kelmon

    Joined:
    Mar 28, 2005
    Location:
    United Kingdom
    #1
    This is no doubt a very simple question, but how can you convert a long integer represented as an instance of NSString to a long primitive? I see that NSString has methods to return integer, float and double primitives but long seems to be curiously absent. Any ideas?
     
  2. macrumors newbie

    Joined:
    May 19, 2005
    #2
    You need to use an NSScanner:

    NSScanner* scanner = [NSScanner scannerWithString:myString];
    long long valueToGet;
    if([scanner scanLongLong:&valueToGet] == YES) {

    }
     
  3. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #3
    Couldn't you also use sscanf? NSScanner always seems like too much bloat for me. Maybe it's just all in my head :p
     
  4. macrumors 6502

    Joined:
    Jul 25, 2006
    #4
    Using scanf() would require switching to a C string from the NSString, which Apple kind of discourages. NSScanner abstracts all the complications that arise when you start dealing with Unicode strings and so forth as well. I'd say stick with the NSScanner solution. I don't think it needs to be a long long though... The originator never said anything about dealing with 64-bit quantities...
     
  5. thread starter macrumors 6502a

    Kelmon

    Joined:
    Mar 28, 2005
    Location:
    United Kingdom
    #5
    Sorry it's taken me so long to respond, but what's a "long long" value? That's not something that I have encountered before.
     
  6. thread starter macrumors 6502a

    Kelmon

    Joined:
    Mar 28, 2005
    Location:
    United Kingdom
    #6
    Ignore my last request on the question of what is a "long long" value. It's a 64-bit version of a long as, apparently, a long value is normally only guaranteed to be 32-bits wide.
     
  7. thread starter macrumors 6502a

    Kelmon

    Joined:
    Mar 28, 2005
    Location:
    United Kingdom
    #7
    Actually, I do have a question still remaining on this one: is there an easy way to put primitive numbers into instances of NSString? At present I am using the primitives with instances of NSNumber and then using the stringValue: method of NSNumber. Is it possible to go direct to string?
     
  8. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #8
    To create a string from an int or long, you can use
    Code:
    int value = 10;
    NSString *str = [NSString stringWithFormat:@"value is %d", value];
    Also look into the stringByAppendingString: method.
     
  9. thread starter macrumors 6502a

    Kelmon

    Joined:
    Mar 28, 2005
    Location:
    United Kingdom
    #9
    That looks like it will do very nicely. Thanks.
     
  10. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #10
  11. macrumors 6502

    Joined:
    Jul 25, 2006
    #11
    To be more specific, long long is 64-bit in both 32-bit and 64-bit environments. On 32-bit machines, long is 32-bits. In an LP 64-bit environment (which is what Apple and most *nixes use), long 64-bit.
     

Share This Page