Strange results when trying to stringWithFormat method

I tried using the NSString method stringWithFormat while working with ASObj-C objects and the results were very strange. I posted things I tried to figure out the problem. The result of each line of code is appended to the line as a comment. My results follow the -->. I suspect I ran into a problem with the tool free bridging but I’m really not sure. I’ve fooled with in for a few days and am no closer to figuring it out.

At first I thought the stringWithFormat would not work with ASObj-C objects but it didn’t work when I gave it AppleScript integers either. Perhaps someone can figure out why I get 3895 when I expected 15,

I ran the script posted in Script Editor and it got the same results. I typed cast the outputs of all the lines in the script to a string to see the output in Script Editor.

The list of codes for formatted strings can be found on Apple’s site at:
Click here to see the list

Anybody have any ideas?

-- I started off with this not working
set TheInteger to NSNumber's alloc()'s initWithInteger:15 --> (NSNumber) 15
NSString's stringWithFormat_("%d", TheInteger) --> (NSString) "3895"

-- The Apple docs say %ld or %lx cast the value to long.
-- I don't know for sure what the "x" in "%lx" means but I'm guessing hexadecimal
-- since "3895" base 10 is equal to "F37" in hex.
NSString's stringWithFormat_("%d", NSNumber's alloc()'s initWithInteger:15) --> (NSString) "3895"
NSString's stringWithFormat_("%ld", NSNumber's alloc()'s initWithInteger:15) --> (NSString) "3895"
NSString's stringWithFormat_("%lx", NSNumber's alloc()'s initWithInteger:15) --> (NSString) "f37"

-- I checked the class of the returned values & found I got different results depending on how I checked it
-- The AppleScript code returned (Class) __NSCFNumber which is the usual for the toll free numbers.
-- The ASObj-C code returned (NSNumber) 15.
(NSNumber's alloc()'s initWithInteger:15)'s className() --> (NSNumber) 15
class of (NSNumber's alloc()'s initWithInteger:15) --> (Class) __NSCFNumber

-- I tried a different number type to see if it changed anything
set LongNumber to NSNumber's alloc()'s initWithLong:15 --> (NSNumber) 15
LongNumber's className() --> (NSString) "__NSCFNumber"
class of LongNumber --> (Class) __NSCFNumber
NSString's stringWithFormat_("%d", LongNumber) --> (NSString) "3895"
NSString's stringWithFormat_("%ld", LongNumber) --> (NSString) "3895"

-- The number 3895 changed to 3879 when I type cast the number to integer
-- I tried playing with the format characters but got more strange result and different number as well.
set TheVar to 15
NSString's stringWithFormat_("%d", TheVar as integer) --> (NSString) "3879"
NSString's stringWithFormat_("%f", TheVar as integer) --> (NSString) "0.000000"
NSString's stringWithFormat_("%@", TheVar as integer) --> (NSString) "15" so it's seeing the right value.
set TheVar to 15.0
NSString's stringWithFormat_("%f", TheVar as integer) --> (NSString) "0.000000"
NSString's stringWithFormat_("%f", TheVar as integer) --> (NSString) "0.000000"


ASObjC can only make very limited use of stringWithFormat:. The problem is that the scripting bridge converts any AppleScript values you pass into Cocoa objects, so integers and reals all end up NSNumbers. And once they are NSNumbers, you can’t use formats like %d – they’re objects, so you can only use %@.

As you’ve seen, if you try using anything other than %@, you’re likely to get nonsense – and in some cases you may even get a crash.

The same thing holds for NSLog() – anything you pass should be treated as an object, so you can only use %@.

Thanks Shane. I thought that might be the case, but I was hoping for some kind of way around it. But still some level of formatting control is better than none.

Bill Kopp

I think the Cocoa way would be to use NSNumberFormatter objects to format your numbers.

Yep – although that doesn’t cover things like %x and %o, sadly.