I\'ve been trying to make a dynamic string format so that through user options precision or padding could be somewhat user defined.
An example includes, the padding of
There's a better way to do this.
... stringWithFormat:@"Hour: %0*i", length, hour]; // note the asterisk
where length
is the number of digits you want. Use 1
to get no leading zeros, use 2
to get a length of 2 with leading zeros as needed.
FYI - to solve what you originally tried you need to do it in two steps:
NSString *dynFmt = [NSString stringWithFormat:@"Hour: %%%@i", padding];
NSString *res = [NSString stringWithFormat:dynFmt, hour];