PDA

View Full Version : Objective-C: Convert NSString Object to Long Primitive




Kelmon
Aug 3, 2006, 12:17 PM
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?



Mesa Dynamics
Aug 3, 2006, 12:54 PM
You need to use an NSScanner:

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

}

kainjow
Aug 6, 2006, 08:31 AM
Couldn't you also use sscanf? NSScanner always seems like too much bloat for me. Maybe it's just all in my head :p

kpua
Aug 6, 2006, 12:18 PM
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...

Kelmon
Aug 11, 2006, 06:41 AM
You need to use an NSScanner:

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

}

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.

Kelmon
Aug 11, 2006, 08:01 AM
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.

Kelmon
Aug 11, 2006, 11:31 AM
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?

kainjow
Aug 11, 2006, 11:51 AM
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?
To create a string from an int or long, you can use int value = 10;
NSString *str = [NSString stringWithFormat:@"value is %d", value];
Also look into the stringByAppendingString: method.

Kelmon
Aug 11, 2006, 12:01 PM
That looks like it will do very nicely. Thanks.

kainjow
Aug 11, 2006, 12:03 PM
Also check out this page for formatting other datatypes:

http://developer.apple.com/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW1

kpua
Aug 11, 2006, 12:21 PM
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.

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.