﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-gembin-随笔分类-Objective-C</title><link>http://www.blogjava.net/gembin/category/50998.html</link><description>&lt;font color="red"&gt;OSGi, Eclipse Equinox, ECF, Virgo, Gemini,  Apache Felix, Karaf, Aires, Camel, Eclipse RCP&lt;/font&gt;&lt;br/&gt;&lt;br/&gt;
&lt;font color="green"&gt;HBase, Hadoop, ZooKeeper, Cassandra&lt;/font&gt;&lt;br/&gt;&lt;br/&gt;

&lt;font color="blue"&gt;Flex4, AS3, Swiz framework, GraniteDS, BlazeDS etc.&lt;/font&gt;&lt;br/&gt;&lt;br/&gt;
&lt;font color="black"&gt;
There is nothing that software can't fix. Unfortunately, there is also nothing that software can't completely fuck up. That gap is called talent.&lt;/font&gt;
&lt;br/&gt;&lt;br/&gt;

&lt;a href="http://about.me/gembin"&gt;About Me&lt;/a&gt;

&lt;script type="text/javascript" src="http://platform.linkedin.com/in.js"&gt;&lt;/script&gt;&lt;script type="in/share" data-counter="right"&gt;&lt;/script&gt;

</description><language>zh-cn</language><lastBuildDate>Sat, 03 Mar 2012 16:34:06 GMT</lastBuildDate><pubDate>Sat, 03 Mar 2012 16:34:06 GMT</pubDate><ttl>60</ttl><item><title>Objective-C NSString formatting </title><link>http://www.blogjava.net/gembin/archive/2012/03/04/371188.html</link><dc:creator>gembin</dc:creator><author>gembin</author><pubDate>Sat, 03 Mar 2012 16:27:00 GMT</pubDate><guid>http://www.blogjava.net/gembin/archive/2012/03/04/371188.html</guid><wfw:comment>http://www.blogjava.net/gembin/comments/371188.html</wfw:comment><comments>http://www.blogjava.net/gembin/archive/2012/03/04/371188.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/gembin/comments/commentRss/371188.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/gembin/services/trackbacks/371188.html</trackback:ping><description><![CDATA[<p style="margin-top: 20px; margin-right: 0px; margin-bottom: 20px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 25px; word-wrap: break-word; font-family: 'Lucida Grande', Geneva, Arial, Verdana, Tahoma, Arial, 微软雅黑, 黑体, 宋体; ">The format specifiers supported by the NSString formatting methods and CFString formatting <br />functions follow the IEEE printf specification; the specifiers are summarized in Table 1.<br /> Note that you can also use the &#8220;n$&#8221; positional specifiers such as %1$@ %2$s. <br />For more details, see the IEEE printf specification. You can also use these format specifiers with the NSLog function.</p><table cellspacing="1" border="0" style="color: #000000; font-family: 'Lucida Grande', Geneva, Arial, Verdana, Tahoma, Arial, 微软雅黑, 黑体, 宋体; font-size: 14px; line-height: 25px; width: 643px; "><caption>Table 1 Format specifiers supported by the NSString formatting methods and CFString formatting functions</caption><tbody style="background-color: #eeeeee; "><tr style="background-color: #cccccc; "><td style="width: 62px; ">定义</td><td style="width: 574px; ">说明</td></tr><tr><td>%@</td><td>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.</td></tr><tr><td>%%</td><td>&#8216;%&#8217; character</td></tr><tr><td>%d, %D, %i</td><td>Signed 32-bit integer (int)</td></tr><tr><td>%u, %U</td><td>Unsigned 32-bit integer (unsigned int)</td></tr><tr><td>%hi</td><td>Signed 16-bit integer (short)</td></tr><tr><td>%hu</td><td>Unsigned 16-bit integer (unsigned short)</td></tr><tr><td>%qi</td><td>Signed 64-bit integer (long long)</td></tr><tr><td>%qu</td><td>Unsigned 64-bit integer (unsigned long long)</td></tr><tr><td>%x</td><td>Unsigned 32-bit integer (unsigned int), printed in hexadecimal using the digits 0&#8211;9 and lowercase a&#8211;f</td></tr><tr><td>%X</td><td>Unsigned 32-bit integer (unsigned int), printed in hexadecimal using the digits 0&#8211;9 and uppercase A&#8211;F</td></tr><tr><td>%qx</td><td>Unsigned 64-bit integer (unsigned long long), printed in hexadecimal using the digits 0&#8211;9 and lowercase a&#8211;f</td></tr><tr><td>%qX</td><td>Unsigned 64-bit integer (unsigned long long), printed in hexadecimal using the digits 0&#8211;9 and uppercase A&#8211;F</td></tr><tr><td>%o, %O</td><td>Unsigned 32-bit integer (unsigned int), printed in octal</td></tr><tr><td>%f</td><td>64-bit floating-point number (double)</td></tr><tr><td>%e</td><td>64-bit floating-point number (double), printed in scientific notation using a lowercase e to introduce the exponent</td></tr><tr><td>%E</td><td>64-bit floating-point number (double), printed in scientific notation using an uppercase E to introduce the exponent</td></tr><tr><td>%g</td><td>64-bit floating-point number (double), printed in the style of %e if the exponent is less than &#8211;4 or greater than or equal to the precision, in the style of %f otherwise</td></tr><tr><td>%G</td><td>64-bit floating-point number (double), printed in the style of %E if the exponent is less than &#8211;4 or greater than or equal to the precision, in the style of %f otherwise</td></tr><tr><td>%c</td><td>8-bit unsigned character (unsigned char), printed by NSLog() as an ASCII character, or, if not an ASCII character, in the octal format \\ddd or the Unicode hexadecimal format \\udddd, where d is a digit</td></tr><tr><td>%C</td><td>16-bit Unicode character (unichar), printed by NSLog() as an ASCII character, or, if not an ASCII character, in the octal format \\ddd or the Unicode hexadecimal format \\udddd, where d is a digit</td></tr><tr><td>%s</td><td>Null-terminated array of 8-bit unsigned characters. %s interprets its input in the system encoding rather than, for example, UTF-8.</td></tr><tr><td>%S</td><td>Null-terminated array of 16-bit Unicode characters</td></tr><tr><td>%p</td><td>Void pointer (void *), printed in hexadecimal with the digits 0&#8211;9 and lowercase a&#8211;f, with a leading 0x</td></tr><tr><td>%L</td><td>Length modifier specifying that a following a, A, e, E, f, F, g, or G conversion specifier applies to a long double argument</td></tr><tr><td>%a</td><td>64-bit floating-point number (double), printed in scientific notation with a leading 0x and one hexadecimal digit before the decimal point using a lowercase p to introduce the exponent</td></tr><tr><td>%A</td><td>64-bit floating-point number (double), printed in scientific notation with a leading 0X and one hexadecimal digit before the decimal point using a uppercase P to introduce the exponent</td></tr><tr><td>%F</td><td>64-bit floating-point number (double), printed in decimal notation</td></tr><tr><td>%z</td><td>Length modifier specifying that a following d, i, o, u, x, or X conversion specifier applies to a size_t or the corresponding signed integer type argument</td></tr><tr><td>%t</td><td>Length modifier specifying that a following d, i, o, u, x, or X conversion specifier applies to a ptrdiff_t or the corresponding unsigned integer type argument</td></tr><tr><td>%j</td><td>Length modifier specifying that a following d, i, o, u, x, or X conversion specifier applies to a intmax_t or uintmax_t argument<br /><br /></td></tr></tbody></table><br /><p style="margin-top: 20px; margin-right: 0px; margin-bottom: 20px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 25px; word-wrap: break-word; font-family: 'Lucida Grande', Geneva, Arial, Verdana, Tahoma, Arial, 微软雅黑, 黑体, 宋体; ">Mac OS X uses several data types&#8212;NSInteger, NSUInteger,CGFloat, and CFIndex&#8212;to provide a <br />consistent means of representing values in 32- and 64-bit environments. In a 32-bit environment, <br />NSInteger and NSUInteger are defined as int and unsigned int, respectively. In 64-bit environments, <br />NSInteger and NSUInteger are defined as long and unsigned long, respectively. To avoid the need to <br />use different printf-style type specifiers depending on the platform, you can use the specifiers shown<br /> in Table 2. Note that in some cases you may have to cast the value.</p><table cellspacing="1" border="0" style="color: #000000; font-family: 'Lucida Grande', Geneva, Arial, Verdana, Tahoma, Arial, 微软雅黑, 黑体, 宋体; font-size: 14px; line-height: 25px; width: 643px; "><caption>Table 2 Format specifiers for data types</caption><tbody style="background-color: #eeeeee; "><tr style="background-color: #cccccc; "><td style="width: 76px; ">类型</td><td style="width: 60px; ">定义</td><td style="width: 497px; ">建议</td></tr><tr><td>NSInteger</td><td>%ld or %lx</td><td>Cast the value to long</td></tr><tr><td>NSUInteger</td><td>%lu or %lx</td><td>Cast the value to unsigned long</td></tr><tr><td>CGFloat</td><td>%f or %g</td><td>%f works for floats and doubles when formatting; but see below warning when scanning</td></tr><tr><td>CFIndex</td><td>%ld or %lx</td><td>The same as NSInteger</td></tr><tr><td>pointer</td><td>%p</td><td>%p adds 0x to the beginning of the output. If you don&#8217;t want that, use %lx and cast to long.</td></tr><tr><td>long long</td><td>%lld or %llx</td><td>long long is 64-bit on both 32- and 64-bit platforms</td></tr><tr><td>unsigned long long</td><td>%llu or %llx</td><td>unsigned long long is 64-bit on both 32- and 64-bit platforms</td></tr></tbody></table><p style="margin-top: 20px; margin-right: 0px; margin-bottom: 20px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 25px; word-wrap: break-word; font-family: 'Lucida Grande', Geneva, Arial, Verdana, Tahoma, Arial, 微软雅黑, 黑体, 宋体; ">The following example illustrates the use of %ld to format an NSInteger and the use of a cast.</p><div objc=""  default"="" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, Tahoma, Arial, 微软雅黑, 黑体, 宋体; line-height: 25px; overflow-x: auto; overflow-y: auto; white-space: nowrap; width: 580px; "><table cellspacing="0" cellpadding="0"><tbody><tr><td><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">1<br />2</div></td><td><div codecolorer"="" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: nowrap; ">NSInteger i&nbsp;=&nbsp;42;<br />printf("%ld\n",&nbsp;(long)i);</div></td></tr></tbody></table></div><p style="margin-top: 20px; margin-right: 0px; margin-bottom: 20px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 25px; word-wrap: break-word; font-family: 'Lucida Grande', Geneva, Arial, Verdana, Tahoma, Arial, 微软雅黑, 黑体, 宋体; ">In addition to the considerations mentioned in Table 2, there is one extra case with scanning: <br />you must distinguish the types for float and double. You should use %f for float, %lf for double.<br /> If you need to use scanf (or a variant thereof) with CGFloat, switch to double instead, and copy the double to CGFloat.</p><div objc=""  default"="" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, Tahoma, Arial, 微软雅黑, 黑体, 宋体; line-height: 25px; overflow-x: auto; overflow-y: auto; white-space: nowrap; width: 580px; "><table cellspacing="0" cellpadding="0"><tbody><tr><td><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">1<br />2<br />3<br />4</div></td><td><div codecolorer"="" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; white-space: nowrap; ">CGFloat imageWidth;<br />double&nbsp;tmp;<br />sscanf&nbsp;(str,&nbsp;"%lf",&nbsp;&amp;amp;tmp);<br />imageWidth&nbsp;=&nbsp;tmp;</div></td></tr></tbody></table></div><p style="margin-top: 20px; margin-right: 0px; margin-bottom: 20px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 25px; word-wrap: break-word; font-family: 'Lucida Grande', Geneva, Arial, Verdana, Tahoma, Arial, 微软雅黑, 黑体, 宋体; ">It is important to remember that %lf does not represent CGFloat correctly on either 32- or 64-bit platforms. <br />This is unlike %ld, which works for long in all cases.</p><br /><br /><br /><br /><img src ="http://www.blogjava.net/gembin/aggbug/371188.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/gembin/" target="_blank">gembin</a> 2012-03-04 00:27 <a href="http://www.blogjava.net/gembin/archive/2012/03/04/371188.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>