Formatting Tricks / Documents

I read Shaggy Frog’s answer to this post and was intrigued by the following line of code:

NSLog(@"%@", [NSString stringWithFormat:@"%@:%*s%5.2f", key, padding, " ", [object floatValue]]); 

I know that formatting strings is an old art, but I’m kind of ending up in Cocoa / Obj-C programming and skipped a few classes along the way. Where is a good (best) place to learn all the string formatting tricks allowed in an NSString stringWithFormat ? I am familiar with Apple String Format Specifiers , but from what I can say, it does not shed light on everything that happens with %*s or %5.2f (not to mention 3 obvious placeholders followed by 4 arguments) above? !?

+1
source share
1 answer

The -stringWithFormat documentation brings you to the String Format Specifier , which in turn sends you to the IEEE printf specification . This is about as much information as you want.

The only noticeable exception:

% @

Objective-C object printed as a string returned by descriptionWithLocale: if available, or description otherwise. Also works with CFTypeRef objects, returning the result of the CFCopyDescription function.

  • nil converts to (null), which is the reason NSLog(@"%@", someObject) safer than NSLog("someObject) . Later it may fail when someObject is zero:

You might also be interested in the wikipedia page about formatting strings .

+1
source

Source: https://habr.com/ru/post/896272/


All Articles