String Formatting Tricks/Docs

前端 未结 1 996
时光取名叫无心
时光取名叫无心 2020-12-22 04:38

Was reading the response by Shaggy Frog to this post and was intrigued by the following line of code:

NSLog(@\"%@\", [NSString stringWithFormat:@\"%@:%*s%5.         


        
1条回答
  •  有刺的猬
    2020-12-22 05:19

    The documentation of -stringWithFormat leads you to String Format Specifier which in turn sends you to the IEEE printf specification. That's about as much information as you'll ever want.

    The only notable exception:

    %@

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

    • nil gets converted to (null), that's the reason why NSLog(@"%@", someObject) is safer than NSLog("someObject). The later might crash when someObject is nil:

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

    0 讨论(0)
提交回复
热议问题