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

smokyonion

macrumors newbie
Original poster
Dec 13, 2009
13
0
Hi, guys.
I am learning Cocoa right now, and
I am working on a small Cocoa project try to learn how to use NSNumberFormatter,

This project is a simple screen capture program and I want it has the ability to auto save the captured image file.
I create three NSTextField for users
one is Filename prefix
another one is digit,
and the last is the length of the digit.

So when users capture the screenshot my program should automatically save file with the naming rule.
For Example: I have a fifth image captured. and the digit length is 4.
The file name will be: "FileNamePrefix-0005.jpg"
And then auto increment one for next image.

How can I make the digit in the fixed length.
I have looked up NSNumberFormatter and try to find something can help me on this, however it seems not for this kind of usage.

Any reply will be highly appreciated, thank you so much.
 
Code:
NSString *myFormat, *myFilename, *prefixString = @"myFilePrefix";
NSInteger numWidth = 7,imageNumber = 5;
myFormat = [NSString stringWithFormat:@"%%s%%0%lldlld.jpg",(long)numWidth];
myFilename = [NSString stringWithFormat:myFormat,prefixString,(long)imageNumber];

Something like this should do it. I'm on my phone, so this hasn't been compiled or tested.

-Lee
 
Dear GorillaPaws and lee1210:

Thank you so much for your precious help.

lee1210:

your example code works great for me.
There is only one character has to be changed.

Change from %%s to %%@,

From

myFormat = [NSString stringWithFormat:mad:"%%s%%0%lldlld.jpg",(long)numWidth];

to

myFormat = [NSString stringWithFormat:mad:"%%@%%0%lldlld.jpg",(long)numWidth];

I am curious about the format you given.
%%@%%0%lldlld.jpg,
I don't know I can use stringFormat like this format before.
(After I NSLog myFormat, I got it. )
Thank you all. You rock~
 
Ah, oops. In my sleepiness I forgot NSString needs %@, I was thinking of char * c-style strings, even though I used NSString in the code.

If you log what myFormat is, you should see a pretty normal looking format string. The reason for all of the %s when building it is that we need the % to show up in our string, but % is special. It is used for format specifiers, so you have to double it to get a single % in your resulting string.

-Lee

edit: again, due to sleepiness I missed that you had looked and figured out what was going on.
 
Hi, guys.
I am learning Cocoa right now, and
I am working on a small Cocoa project try to learn how to use NSNumberFormatter,

This project is a simple screen capture program and I want it has the ability to auto save the captured image file.
I create three NSTextField for users
one is Filename prefix
another one is digit,
and the last is the length of the digit.

So when users capture the screenshot my program should automatically save file with the naming rule.
For Example: I have a fifth image captured. and the digit length is 4.
The file name will be: "FileNamePrefix-0005.jpg"
And then auto increment one for next image.

How can I make the digit in the fixed length.
I have looked up NSNumberFormatter and try to find something can help me on this, however it seems not for this kind of usage.

Any reply will be highly appreciated, thank you so much.

Get an old C book out, look at printf and sprintf and formatting codes and figure out what format turns the number 5 into the string 0005. That's easy, it is all well documented.

Then find an NSString method that does basically the same as sprintf does.
 
Thank you, gnasher729.

I have looked up the C book.

That really helps for me to understand what the format is describing and how to use it.

Thank you all~
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.