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
Use two steps:
NSString* hourString=nil;
if(...){
hourString=[NSString stringWithFormat:@"%i", hour];
}else{
hourString=[NSString stringWithFormat:@"%02i", hour];
}
NSString* result=[NSString stringWithFormat:@"Hour: %@", hourString];
The following is considered a bad practice:
NSString* formatString=nil;
if(...){
formatString=@"Hour: %i";
}else{
formatString=@"Hour: %02i";
}
NSString* result=[NSString stringWithFormat:formatString, hour];
because it's very dangerous to have a variable formatString, which can be used in many types of cracker attacks.