Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Kelmon

macrumors 6502a
Original poster
Mar 28, 2005
733
0
United Kingdom
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

macrumors newbie
May 19, 2005
19
0
You need to use an NSScanner:

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

}
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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

macrumors 6502
Jul 25, 2006
294
0
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

macrumors 6502a
Original poster
Mar 28, 2005
733
0
United Kingdom
Mesa Dynamics said:
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

macrumors 6502a
Original poster
Mar 28, 2005
733
0
United Kingdom
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

macrumors 6502a
Original poster
Mar 28, 2005
733
0
United Kingdom
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

Moderator emeritus
Jun 15, 2000
7,958
7
Kelmon said:
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
Code:
int value = 10;
NSString *str = [NSString stringWithFormat:@"value is %d", value];
Also look into the stringByAppendingString: method.
 

kpua

macrumors 6502
Jul 25, 2006
294
0
Kelmon said:
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.