﻿<?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>C++博客-佳为好友-随笔分类-非UI</title><link>http://www.cppblog.com/walkklookk/category/17725.html</link><description /><language>zh-cn</language><lastBuildDate>Thu, 24 Jan 2013 20:08:34 GMT</lastBuildDate><pubDate>Thu, 24 Jan 2013 20:08:34 GMT</pubDate><ttl>60</ttl><item><title>转:判断是在模拟器中还是在真实的iphone上 -2</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/30/196840.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sun, 30 Dec 2012 03:39:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/30/196840.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196840.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/30/196840.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196840.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196840.html</trackback:ping><description><![CDATA[<span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">转:</span><a href="http://www.bit-101.com/blog/?p=1926" target="_blank" style="color: #1155cc; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">http://www.bit-101.com/blog/<wbr>?p=1926</a><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">转：<a href="http://stackoverflow.com/questions/146986/what-defines-are-setup-by-xcode-when-compiling-for-iphone" target="_blank" style="color: #1155cc;">http://stackoverflow.com/<wbr>questions/146986/what-defines-<wbr>are-setup-by-xcode-when-<wbr>compiling-for-iphone</a><br /><h1><a href="http://www.bit-101.com/blog/?p=1926" rel="bookmark" title="XCode Conditional Compilation" target="_blank" style="color: #1155cc;">XCode Conditional Compilation</a></h1><br /><br />#if (TARGET_IPHONE_SIMULATOR)&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 在模拟器的情况下<br />#else<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 在真机情况下<br />#endif<br /><div><br />或者反之，<br /><br />#if !(TARGET_IPHONE_SIMULATOR)&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 在真机的情况下<br />#else<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 在模拟器情况下<br />#endif<br /><br />它们使用的是<br /><pre style="white-space: pre-wrap;"><code>#include "TargetConditionals.h"<br />文件里面的东西，但是，这个文件是自动包含到工程中的。<br /><br />另外，有另外一个宏TARGET_OS_IPHONE，<wbr>这个宏表面上看和</code>TARGET_IPHONE_<wbr>SIMULATOR是<br /> 相反的，是代表真机的。但是，实际上不是，无论在模拟器和真机，<wbr>它的值始终是1.因此，我们<br />只能使用TARGET_IPHONE_<wbr>SIMULATOR来判断，而不能使用<code>TARGET_OS_<wbr>IPHONE</code><br />来判断<br /></pre><br /></div>+++++</div><img src ="http://www.cppblog.com/walkklookk/aggbug/196840.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-30 11:39 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/30/196840.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>原：关于自动缓存池的一些原理</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/30/196836.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sun, 30 Dec 2012 03:23:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/30/196836.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196836.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/30/196836.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196836.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196836.html</trackback:ping><description><![CDATA[<span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">创建：2011.05.25</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">#&nbsp; mian函数里面的NSAutoreleasePool，</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">当每次事件循环一次后，都会释放一次缓存池的内容，因此，</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">不是我原来想想的那样&#8220;只在程序退出时才完全释放&#8221;，</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">看来苹果做的还是不错的。</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"># 如果要自定义NSAutoreleasePool的话，</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">那么注意：</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">## 要使用drain，而不要调用release。</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">这个是SDK文档中写的。主要的原因是为了兼容&#8220;</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">有垃圾回收器时&#8221;的情况。在有gc的时候，</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">任何对象的release都不会被调用，因此缓存池不会被释放。</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">而且，对于非gc的情况，</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">drain会执行和release一样的操作，也会释放缓存池。</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">因此，只要调用drain就足够了。</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">## 千万要注意：如果调用了drain，</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">就一定不要在调用release了，</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">这样不但在gdb调试的时候有警告，而且，</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">会导致一些对象释放的很不正常！！！！我为了这个问题，</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">调试了一天！！</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">+++++</span><img src ="http://www.cppblog.com/walkklookk/aggbug/196836.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-30 11:23 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/30/196836.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>原：注销Logout逻辑分析</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/30/196835.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sun, 30 Dec 2012 03:23:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/30/196835.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196835.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/30/196835.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196835.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196835.html</trackback:ping><description><![CDATA[<span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">创建：2011.05.24</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">日期：2011.05</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><div style="color: #500050;"><br />当程序注销之后，程序需要释放所有资源，并还原设置，<wbr>以等待新的用户登录。<br />但是，这个过程并不简单。<br />它应该包括3个方面的内容：<br /># UI的还原<br /># 在后台执行的线程的关闭，即NSOperation的处理<br /># Session的操作<br /><br />==对于UI还原的处理==</div>对于UI的还原，比较好处理。除了Window外，<wbr>每次都重新加载全部的UI。<br />在Login的VC中进行加载。<wbr>或者在自动登录处理结束之后在加载。<br /># 注册一个注销消息的Notification（如：<wbr>SessionLogoutNotification）。<br /><br />- (void)<wbr>handleAppLogoutNotification:(<wbr>NSNotification *)notification {<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; [self AD_showLoadingView];<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; [self AD_showLoginViewController];<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; // 淡出Loading动画。<br />&nbsp;&nbsp;&nbsp; [self AD_hiddenLoadingView];<div><div style="margin: 5px 0px;"><div id="q_1302a5473d1c7135_3"  h4"="" style="cursor: pointer; color: #500050; font-size: 11px; background-color: #f1f1f1; clear: both; line-height: 6px; outline: none; position: relative; width: 20px;"><div style="background-image: url(https://ssl.gstatic.com/ui/v1/icons/mail/ellipsis.png); height: 8px; opacity: 0.3; width: 20px; background-position: initial initial; background-repeat: no-repeat no-repeat;"></div></div></div></div># 取消一切后台运行设置的控制字段。<br /># 调用Session中的NSOperationQueue的ca<wbr>ncelAllOperations。<br /># 检查queue的op数目是否都为0。<br />##如果是，则删除Session。<wbr>将AppDelegate的currentSession设置为<wbr>nil。返回。<br />## 如果是否，则：<br />### 将当前Session放到&#8220;已注销Session数组&#8221;里面。<br />### 将AppDelegate的currentSession设置为<wbr>nil。<br />### 设定一个定时器，并不断的检查&#8220;已注销Session数组&#8221;<wbr>里面的每个session的queue的op是否都为0，<wbr>如果有，则从&#8220;已注销Session数组&#8221;<wbr>里面中删除该session。<br /># 发送注销消息的Notification，让UI进行还原。<br /><br /><br />例如：<br />// LogoutAction<br />- (BOOL)run {<br />&nbsp;&nbsp;&nbsp; NSDictionary *jsonValue = xxxxxx;&nbsp;&nbsp;&nbsp; // 网络请求<br />&nbsp;&nbsp;&nbsp; BOOL success = xxxxxx; // 判断是否注销成功了。<br />&nbsp;&nbsp;&nbsp; if (success) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [gGlobal() globalLogout];&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; return success;<br />}<br /><br />// YDTSession<br />- (void)sessionLogout {<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; mIsLogin = NO;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; [self saveInformation];<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; // 取消上传。<br />&nbsp;&nbsp;&nbsp; gSession().mMediaFileList.<wbr>isNeedStopAndRelease = YES;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; [mQueueManager cancelAllQueueOperations];<br />}<br /><br />// YDTGlobal<br />- (void)globalLogout {<br />&nbsp;&nbsp;&nbsp; // 用于自动登录。<br />&nbsp;&nbsp;&nbsp; gGlobal().mPrevPhone = nil;<br />&nbsp;&nbsp;&nbsp; gGlobal().mPrevPassword = nil;<br />&nbsp;&nbsp;&nbsp; [gGlobal() saveInformation];<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; [gSession() sessionLogout];<br />&nbsp;&nbsp;&nbsp; if ([mYDTSession isSessionCanRelease]) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.mYDTSession = nil;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return;<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; // 如果当前仍有Op要处理，则使用Timer不停检测。<br />&nbsp;&nbsp;&nbsp; [self G_AddLogoutSession:self.<wbr>mYDTSession];<br />&nbsp;&nbsp;&nbsp; self.mYDTSession = nil;<br />&nbsp;&nbsp;&nbsp; [mLogoutTimer performSelectorOnMainThread:@<wbr>selector(fire) withObject:nil waitUntilDone:NO];<br />}<br /><br /><br />+++++</div><img src ="http://www.cppblog.com/walkklookk/aggbug/196835.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-30 11:23 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/30/196835.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>原：ios发送短信 sms</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/30/196834.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sun, 30 Dec 2012 03:22:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/30/196834.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196834.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/30/196834.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196834.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196834.html</trackback:ping><description><![CDATA[<span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">创建：2011.05.26</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">ios4以上的版本发送短信比较简单，</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">可以使用MFMessageComposeViewContro</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">ller。</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">ios4以下的版本发送短信只能使用sms协议。例如</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">[[UIApplication sharedApplication] openURL: @"sms:12345678"];</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">不过，这有几个主要限制：（虽然没有在官方找到依据，</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">但是查阅了大量大量大量的资源，得出的这个结论，</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">应该是有说服力的。可以参照下面的一些连接）</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">1，必须退出当前程序，进入发送短信的页面。</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">2，不能输入短信内容。通常的方法是，</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">把短信内容放到剪切版里面，然后提示用户使用剪切版来黏贴。</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">3，不能群发。</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">很多软件的做法是，使用一个单独SMS Server，叫做SMS Gateway。</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">然后把要发的短信发送到这个server上，</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">由server发给其它手机用户。</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">默默的在后台发送短信肯定是行不通的。否则，</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">你可以猛猛的让用户发短信赚钱了。</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">即使使用这种方法，虽然技术上可行，但是也不能匿名的发送短信，</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">否则会遭到AppStore的拒绝。根据协议：</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><p>As mentioned in you other post, Apple is really clear about that. They will not approve an application which supports anonymous SMS messaging:</p><p>Guidelines:&nbsp;<a href="https://developer.apple.com/appstore/resources/approval/guidelines.html" rel="nofollow" target="_blank" style="color: #1155cc;">https://developer.apple.com/<wbr>appstore/resources/approval/<wbr>guidelines.html</a></p><blockquote><p>22.6 Apps that enable anonymous or prank phone calls or SMS/MMS messaging will be rejected</p></blockquote></div><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">以下为参考资料：</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><a href="http://developer.apple.com/library/ios/#featuredarticles/iPhoneURLScheme_Reference/Articles/PhoneLinks.html%23//apple_ref/doc/uid/TP40007893-SW1" target="_blank" style="color: #1155cc; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">http://developer.apple.com/<wbr>library/ios/#featuredarticles/<wbr>iPhoneURLScheme_Reference/<wbr>Articles/PhoneLinks.html#//<wbr>apple_ref/doc/uid/TP40007893-<wbr>SW1</a><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">官方文档中，关于发送sms协议的介绍。</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><a href="http://blog.mugunthkumar.com/coding/iphone-tutorial-how-to-send-in-app-sms/" target="_blank" style="color: #1155cc; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">http://blog.mugunthkumar.com/<wbr>coding/iphone-tutorial-how-to-<wbr>send-in-app-sms/</a><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">介绍了在各个版本ios中发送短信的方法</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><a href="http://stackoverflow.com/questions/5462672/will-apple-reject-my-ios-sms-client" target="_blank" style="color: #1155cc; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">http://stackoverflow.com/<wbr>questions/5462672/will-apple-<wbr>reject-my-ios-sms-client</a><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">这里介绍了WhatsApp发送短信的原理。提到了SMS Gateway的方式。</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">以及提到了不允许发送匿名短信的协议。</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><a href="http://stackoverflow.com/questions/10848/how-to-programmatically-send-sms-on-the-iphone" target="_blank" style="color: #1155cc; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">http://stackoverflow.com/<wbr>questions/10848/how-to-<wbr>programmatically-send-sms-on-<wbr>the-iphone</a><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">介绍了发送短信的一些背景。也提到了SMS Gateway的方式。</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">另有其它网页也提到了以上的知识，但是，感觉都很短，</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">因此没有记录。</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">+++++</span><img src ="http://www.cppblog.com/walkklookk/aggbug/196834.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-30 11:22 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/30/196834.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>转：NULL NSNull nil Nil</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/30/196833.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sun, 30 Dec 2012 03:22:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/30/196833.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196833.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/30/196833.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196833.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196833.html</trackback:ping><description><![CDATA[<span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">日期：2011.05.27</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">转：</span><a href="http://blog.sinzy.net/jinjian/entry/22022" target="_blank" style="color: #1155cc; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">http://blog.sinzy.net/<wbr>jinjian/entry/22022</a><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">nil: A null pointer to an Objective-C object.</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">( #define nil ((id)0)&nbsp; )</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">Nil: A null pointer to an Objective-C class.</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">NULL: A null pointer to anything else.&nbsp;</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">( #define NULL ((void *)0)&nbsp; )</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">NSNull: A class defines a singleton object used to represent null values in collection objects (which don't allow nil values).</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">[NSNull null]: The singleton instance of NSNull.</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">Technically they're all the same,,, but in practice they give someone reading your code some hints about what's going on; just like naming classes with a capital letter and instances with lowercase is recommended, but not required.</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">If someone sees you passing NULL, they know the receiver expects a C pointer. If they see nil, they know the receiver is expecting an object. If they see Nil, they know the receiver is expecting a class. Readability.</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">example 1:</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><code>[NSApp beginSheet:sheet<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modalForWindow:mainWindow&#8232;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; modalDelegate:nil //pointing to an object&#8232;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; didEndSelector:NULL //pointing to a non object/class&#8232;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contextInfo:NULL]; //pointing to a non object/class</code></div><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">example 2: The values1 will only have obj1,</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">NSObject *obj1 = [NSObject new];</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">NSObject *obj2 = nil;</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">NSObject *obj3 = [NSObject new];</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">NSArray *values1 = [NSArray arrayWithObjects:obj1,obj2,</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">obj3,nil];</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">example 3: The values2 have 3 objects and the second object is instance of NUSNull</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">NSObject *obj1 = [NSObject new];</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">NSObject *obj2 = [NSNull null];</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">NSObject *obj3 = [NSObject new];</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">NSArray *values1 = [NSArray arrayWithObjects:obj1,obj2,</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">obj3,nil];</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">about testing:</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">if obj is nil , [obj message] will return NO, without NSException</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">if obj is NSNull , [obj message will throw a NSException</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">testing NSNull: (NSNull *)obj == [NSNull null]</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">＋＋＋＋＋</span><img src ="http://www.cppblog.com/walkklookk/aggbug/196833.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-30 11:22 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/30/196833.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>原：&lt;Objective-C程序设计&gt;笔记</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/30/196831.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sun, 30 Dec 2012 03:20:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/30/196831.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196831.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/30/196831.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196831.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196831.html</trackback:ping><description><![CDATA[<span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">创建：2011.06.03</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&lt;Objective-C程序设计&gt;笔记</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">＊变量用volatile修饰之后，它明确的告诉编译器，</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">该变量值会发生改变，它用来</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">修饰被不同线程访问和修改的变量。例如：</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">*char1 = 'a';</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">*char1 = 'b';</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">如果没有使用volatile，那么，</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">当编译器遇到这两行代码时，因为对一个地址连续</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">两次赋值，所以编译器就将第一个语句从程序中删除掉。</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">*dynamic标记的属性，</span><wbr style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">系统会推迟到运行时才动态生成相应的方法。</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">*定义新协议，可以扩展原有协议。</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">@protocol NewPro&lt;OldPro&gt;</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">＊不允许函数重载。</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">＊ObjC没有抽象类的概念。</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">＊宏定义中的＃，会产生c语言风格的字符串</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">#define str(x) #x</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">str(test);</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">得到 "test".</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">*#ifdef, #endif,#else, #ifndef</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">*#if, #elif, #endif</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">*#undef</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">+++++</span><img src ="http://www.cppblog.com/walkklookk/aggbug/196831.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-30 11:20 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/30/196831.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>转：MethodSwizzling</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/30/196830.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sun, 30 Dec 2012 02:53:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/30/196830.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196830.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/30/196830.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196830.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196830.html</trackback:ping><description><![CDATA[<span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">创建：2011.06.07</span><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" />［自：这个版本介绍了原理，但是，真实使用的时候，<wbr>我们要使用文中提到的JRSwizzle库。因 为，它的兼容性和通用性更好。］<br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">转：</span><a href="http://www.cocoadev.com/index.pl?MethodSwizzling" target="_blank" style="color: #1155cc; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">http://www.cocoadev.com/<wbr>index.pl?MethodSwizzling</a><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><a href="http://www.cocoadev.com/index.pl?ExtendingClasses" target="_blank" style="color: #1155cc;">ExtendingClasses</a>&nbsp;- Lightweight modification of a single method at runtime</span><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">The Objective-C runtime lets you modify the mappings from a selector (method name) to an implementation (the method code itself). This allows you to "patch" methods in code you don't have the source to (<a href="http://www.cocoadev.com/index.pl?AppKit" target="_blank" style="color: #1155cc;">AppKit</a>,&nbsp;<a href="http://www.cocoadev.com/index.pl?FoundationKit" target="_blank" style="color: #1155cc;">FoundationKit</a>, etc). Unlike creating a category method with the same name as the original method (effectively replacing the original method),&nbsp;<a href="http://www.cocoadev.com/index.pl?MethodSwizzling" target="_blank" style="color: #1155cc;">MethodSwizzling</a>&nbsp;lets your replacement method make use of the original method, almost like subclassing.</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">This is best used in cases where a single method needs substitution or extension; If you need to modify many behaviors of a class, you may be better off using<a href="http://www.cocoadev.com/index.pl?ClassPosing" target="_blank" style="color: #1155cc;">ClassPosing</a>.</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">December 29, 2007: [<a href="http://mjtsai.com/blog/2007/12/29/jrswizzle/" target="_blank" style="color: #1155cc;">http://mjtsai.com/blog/2007/<wbr>12/29/jrswizzle/</a>] Jonathan Rentzsch has started a project, JRSwizzle<a href="http://www.cocoadev.com/index.pl?edit=JRSwizzle" target="_blank" style="color: #1155cc;">?</a>&nbsp;[<a href="http://github.com/rentzsch/jrswizzle" target="_blank" style="color: #1155cc;">http://github.com/rentzsch/<wbr>jrswizzle</a>], to implement method swizzling correctly with different versions of the Objective-C runtime:</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">There&#8217;s at least four swizzling implementations floating around. Here&#8217;s a comparison chart to help you make sense of how they relate to each other and why JRSwizzle<a href="http://www.cocoadev.com/index.pl?edit=JRSwizzle" target="_blank" style="color: #1155cc;">?</a>exists.</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></p><hr size="1" style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><strong style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">An illustration</strong><hr size="1" style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">For instance, let's pretend there's a class called Foo that implements the following method:</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></p><blockquote style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><code></code><pre style="white-space: pre-wrap;">// returns the login name of the current user - (<a href="http://www.cocoadev.com/index.pl?NSString" target="_blank" style="color: #1155cc;">NSString</a> *)fooBar;</pre></blockquote><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">Now we've got applications that call [[Foo sharedFoo] fooBar] all over the place; We'd like to modify the functionality of the fooBar method to append something silly to the result of the original fooBar return value.</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">So, we'll implement a category method that does the work:</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></p><blockquote style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><code></code><pre style="white-space: pre-wrap;">@implementation Foo(specialfooBar) // returns the login name of the current user plus some other junk - (<a href="http://www.cocoadev.com/index.pl?NSString" target="_blank" style="color: #1155cc;">NSString</a> *)myfooBar {     return [[self myfooBar] stringByAppendingString:@", bigtime luser"]; } @end</pre></blockquote><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">"But wait", I hear you saying, "the myfooBar method is calling itself, generating infinite recursion!" Well, that would be the case except for the fact that we're going to do some&nbsp;<a href="http://www.cocoadev.com/index.pl?MethodSwizzling" target="_blank" style="color: #1155cc;">MethodSwizzling</a>&nbsp;and swap the implementations of the two methods! Here's the deal: Right now, any code that calls -[Foo fooBar] is going to use the original implementation (let's call that "A"), and any code that calls -[Foo myfooBar] is going to use our new implementation (let's call that "B"). To summarize:</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></p><blockquote style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><code></code><pre style="white-space: pre-wrap;">selector "fooBar"    -&gt;  implementation "A" selector "myfooBar"  -&gt;  implementation "B"  - implementation "A" returns @"username" - implementation "B" calls "myfooBar",   appends some silly text, and returns that</pre></blockquote><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">So right now, calling "fooBar" invokes implementation "A" like it always has; Calling "myfooBar", however, invokes implementation "B", which in turn calls "myfooBar", which invokes implementation "B", etc, leading to the unwanted infinite recursion.</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></p><blockquote style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><code></code><pre style="white-space: pre-wrap;">// Do the swizzling (see below for the function code) <a href="http://www.cocoadev.com/index.pl?MethodSwizzle" target="_blank" style="color: #1155cc;">MethodSwizzle</a>([Foo class],               @selector(fooBar)               @selector(myfooBar));</pre></blockquote><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">After swizzling the two methods, we have the following situation:</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></p><blockquote style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><code></code><pre style="white-space: pre-wrap;">selector "fooBar"    -&gt;  implementation "B" selector "myfooBar"  -&gt;  implementation "A"  - implementation "A" returns @"username" - implementation "B" calls "myfooBar",   appends some silly text, and returns that</pre></blockquote><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">Now, after the swizzling, anybody who calls "fooBar" will invoke implementation "B", which calls "myfooBar" (invoking implementation "A" and appending some silly text). In short, calling "fooBar" will now return @"username, bigtime luser"!</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">Hopefully this makes sense to people!</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">--<a href="http://www.cocoadev.com/index.pl?JackNutting" target="_blank" style="color: #1155cc;">JackNutting</a></p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></p><hr size="1" style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><strong style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">The&nbsp;<a href="http://www.cocoadev.com/index.pl?MethodSwizzle" target="_blank" style="color: #1155cc;">MethodSwizzle</a>&nbsp;function</strong><hr size="1" style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">Here's a C-function that does the trick:</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></p><blockquote style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><code></code><pre style="white-space: pre-wrap;">#import &lt;/usr/include/objc/objc-class.<wbr>h&gt; void <a href="http://www.cocoadev.com/index.pl?MethodSwizzle" target="_blank" style="color: #1155cc;">MethodSwizzle</a>(Class aClass, SEL orig_sel, SEL alt_sel) {     Method orig_method = nil, alt_method = nil;      // First, look for the methods     orig_method = class_getInstanceMethod(<wbr>aClass, orig_sel);     alt_method = class_getInstanceMethod(<wbr>aClass, alt_sel);      // If both are found, swizzle them     if ((orig_method != nil) &amp;&amp; (alt_method != nil))         {         char *temp1;         IMP temp2;          temp1 = orig_method-&gt;method_types;         orig_method-&gt;method_types = alt_method-&gt;method_types;         alt_method-&gt;method_types = temp1;          temp2 = orig_method-&gt;method_imp;         orig_method-&gt;method_imp = alt_method-&gt;method_imp;         alt_method-&gt;method_imp = temp2;         } }</pre></blockquote><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></p><hr size="1" style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">Here's code that uses it:</p><blockquote style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><code></code><pre style="white-space: pre-wrap;">@interface T : <a href="http://www.cocoadev.com/index.pl?NSObject" target="_blank" style="color: #1155cc;">NSObject</a> { } - (void)swizzleMethod; @end @interface T (AltMethod) - (void)altMethod; @end  int main (int argc, const char * argv[]) {     T *t;     t = [[T alloc] init];     [t swizzleMethod];      <a href="http://www.cocoadev.com/index.pl?NSLog" target="_blank" style="color: #1155cc;">NSLog</a>(@"Methods swizzled");     <a href="http://www.cocoadev.com/index.pl?MethodSwizzle" target="_blank" style="color: #1155cc;">MethodSwizzle</a>([T class],                   @selector(swizzleMethod),                   @selector(altMethod));          [t swizzleMethod];     [t release];          return 0; }  @implementation T - (void)swizzleMethod {     <a href="http://www.cocoadev.com/index.pl?NSLog" target="_blank" style="color: #1155cc;">NSLog</a>(@"Original method called"); } @end  @implementation T (AltMethod) - (void)altMethod {     <a href="http://www.cocoadev.com/index.pl?NSLog" target="_blank" style="color: #1155cc;">NSLog</a>(@"Alternative method called");     [self altMethod]; } @end</pre></blockquote><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">Note the use of a category, just to check everything works correctly.</span><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">Here's the output as logged:</p><blockquote style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><code></code><pre style="white-space: pre-wrap;">2002-04-09 17:23:58.144 Swizzling[2285] Original method called 2002-04-09 17:23:58.148 Swizzling[2285] Methods swizzled 2002-04-09 17:23:58.150 Swizzling[2285] Alternative method called 2002-04-09 17:23:58.152 Swizzling[2285] Original method called</pre></blockquote><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></p><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">＋＋＋＋＋</span><img src ="http://www.cppblog.com/walkklookk/aggbug/196830.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-30 10:53 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/30/196830.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>原：ObjC的编解码文件解析 （序列化文件）</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/30/196829.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sun, 30 Dec 2012 02:27:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/30/196829.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196829.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/30/196829.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196829.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196829.html</trackback:ping><description><![CDATA[<span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">创建：2011.06.14</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">ObjC编码有一种需求，即同一个对象只能编码一次，</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">无论它被引用了多少次。因此，编码文件的结构，</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">和代码中类的结构并不相同，也不够直观。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">为了达到上述目的，直观的做法是：</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">＃将所有的对象全部列出到一个数组中。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">＃每个对象的成员变量写出成员变量名，但是，</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">它的值要指向上述数组中的索引。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">＃如果多个对象引用了一个对象，那么该对象值在数组中生成一次，</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">而其它则都是指向它的索引。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">举个实例说明一下：</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">＃定义一个类A。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">@interface A : NSObject {</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; NSString *mStr;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; NSString *m2Str;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; int mInt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">}</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">@end</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">＃将生成的plist文件是UTF8编码，但是没有BOM信息，</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">可以使用TextWrangler来加入一个BOM。</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">这样FireFox就能 识别了。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">＃将文件改名为xml。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">＃拖拽到Firefox中打开。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">＃内容如下：（我加了注释）</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&lt;plist version="1.0"&gt;&nbsp;&nbsp;&nbsp;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&lt;dict&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; &lt;key&gt;$archiver&lt;/key&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 1.略</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; &lt;string&gt;NSKeyedArchiver&lt;/</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">string&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; &lt;key&gt;$objects&lt;/key&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 2.很重要，下面详细叙述。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; &lt;array&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;string&gt;$null&lt;/string&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 2.0 对象0：string对象，值为null。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;dict&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 2.1 a对象。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;key&gt;$class&lt;/key&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 该对象的类（即A）的对象的索引。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;dict&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;key&gt;CF$UID&lt;/key&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;integer&gt;4&lt;/integer&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 指明A的信息在&#8220;4&#8221;中。即2.4中。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/dict&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;key&gt;m2Str&lt;/key&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // m2Str的信息。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;dict&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;key&gt;CF$UID&lt;/key&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;integer&gt;3&lt;/integer&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 指明A的信息在&#8220;3&#8221;中。即2.3中。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/dict&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;key&gt;mInt&lt;/key&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // mInt的信息。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;integer&gt;123456&lt;/integer&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;key&gt;mStr&lt;/key&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // mStr的信息。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;dict&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;key&gt;CF$UID&lt;/key&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;integer&gt;2&lt;/integer&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 指明A的信息在&#8220;3&#8221;中。即2.2中。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/dict&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/dict&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;string&gt;String111&lt;/string&gt;&nbsp;&nbsp;&nbsp;&nbsp;</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 2.2</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;string&gt;String222&lt;/string&gt;&nbsp;&nbsp;&nbsp;&nbsp;</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 2.3</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;dict&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 2.4 列出A对象的类的信息。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;key&gt;$classes&lt;/key&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 2.4.0 A对象的父类。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;array&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;string&gt;A&lt;/string&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;string&gt;NSObject&lt;/string&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/array&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;key&gt;$classname&lt;/key&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 2.4.1 A对象的类名。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;string&gt;A&lt;/string&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/dict&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; &lt;/array&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; &lt;key&gt;$top&lt;/key&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 3.略</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; &lt;dict&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;key&gt;root&lt;/key&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;dict&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;key&gt;CF$UID&lt;/key&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;integer&gt;1&lt;/integer&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;/dict&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; &lt;/dict&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; &lt;key&gt;$version&lt;/key&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // 4.略</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;&nbsp;&nbsp; &lt;integer&gt;100000&lt;/integer&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&lt;/dict&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&lt;/plist&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">＃</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">array编码了所有的对象。类型也编码为一种对象。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">array的顶层子节点列出所有的NSObject的派生类对象</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">第一个都是&lt;string&gt;$null&lt;/string&gt;，</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">不考虑它。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">第二个是Root对象。即[NSKeyedArchiver archivedDataWithRootObject:a];</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">中的a。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">array的其他的顶层子节点都是Root对象中直接或简介关联</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">到的对象，即要编码的全部对象。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">如果是内建的NSObject的类型，例如NSString，</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">则直接列出其值， 如&lt;string&gt;String222&lt;/string&gt;</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">如果不是内建的NSObject的派生类的类型，则又是一个&lt;</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">dict&gt;。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">每个对象都首先列出该对象类型在array的索引，</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">然后依次是各个成员变量。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">成员变量列出名字。如果值是非NSObject型，</span><wbr style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">则直接列出值。否则指向array的索引。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">＃依次类推。</span><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: STKaiti; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">+++++</span><img src ="http://www.cppblog.com/walkklookk/aggbug/196829.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-30 10:27 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/30/196829.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>转：Working with Streams －官方</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/30/196826.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sun, 30 Dec 2012 02:21:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/30/196826.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196826.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/30/196826.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196826.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196826.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 创建：2011.06.29转：http://developer.apple.com/library/mac&nbsp;/#documentation/Networking/Conceptual/CFNetwork/CFStreamTasks/CFStreamTasks.html&nbsp;&nbsp;NextPreviousWorking with Streams&nbsp;This chapte...&nbsp;&nbsp;<a href='http://www.cppblog.com/walkklookk/archive/2012/12/30/196826.html'>阅读全文</a><img src ="http://www.cppblog.com/walkklookk/aggbug/196826.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-30 10:21 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/30/196826.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>转：KVO-官方</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/30/196825.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sun, 30 Dec 2012 02:20:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/30/196825.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196825.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/30/196825.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196825.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196825.html</trackback:ping><description><![CDATA[<p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">日期：2011.07.11&nbsp;<br />&lt;&lt;1.Note-Registering for Key-Value Observing.doc&gt;&gt;&nbsp;&lt;&lt;2.Note-KVO Compliance.doc&gt;&gt;</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><a href="http://developer.apple.com/library/ios/documentation/cocoa/conceptual/KeyValueObserving/Articles/KVOCompliance.html" target="_blank" style="color: #1155cc;"><u>Next</u></a><a href="http://developer.apple.com/library/ios/documentation/cocoa/conceptual/KeyValueObserving/KeyValueObserving.html" target="_blank" style="color: #1155cc;"><u>Previous</u></a>&nbsp;<br /><a href="http://developer.apple.com/library/ios/documentation/cocoa/conceptual/KeyValueObserving/index.html" target="_blank" style="color: #1155cc;"><u>http://developer.apple.com/<wbr>library/ios/documentation/<wbr>cocoa/conceptual/<wbr>KeyValueObserving/index.html</u></a>&nbsp;<br /><strong>Registering for Key-Value Observing</strong>&nbsp;<br />In order to receive key-value observing notifications for a property, three things are required:</p><ul style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><ul><li style="margin-left: 15px;">The observed class must be key-value observing compliant for the property that you wish to observe.</li><li style="margin-left: 15px;">You must register the observing object with the observed object, using the method&nbsp;addObserver:forKeyPath:<wbr>options:context:.</li><li style="margin-left: 15px;">The observing class must implement&nbsp;observeValueForKeyPath:<wbr>ofObject:change:context:.</li></ul></ul><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><strong>Important:</strong>&nbsp;Not all classes are KVO-compliant for all properties. You can ensure your own classes are KVO-compliant by following the steps described in&nbsp;<a href="http://developer.apple.com/library/ios/documentation/cocoa/conceptual/KeyValueObserving/Articles/KVOCompliance.html" target="_blank" style="color: #1155cc;"><u>&#8220;KVO Compliance.&#8221;</u></a>&nbsp;Typically properties in Apple-supplied frameworks are only KVO-compliant if they are documented as such.</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><strong>Registering as an Observer</strong>&nbsp;<br />In order to be notified of changes to a property, an observing object must first register with the object to be observed by sending it an&nbsp;addObserver:forKeyPath:<wbr>options:context:&nbsp;message, passing the observer object and the key path of the property to be observed. The options parameter specifies the information that is provided to the observer when a change notification is sent. Using the option<a href="http://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html" target="_blank" style="color: #1155cc;"><u>NSKeyValueObservingOptionOld</u></a>&nbsp;specifies that the original object value is provided to the observer as an entry in the change dictionary. Specifying the<a href="http://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html" target="_blank" style="color: #1155cc;"><u>NSKeyValueObservingOptionNew</u></a>&nbsp;option provides the new value as an entry in the change dictionary. To receive both values, you would bitwise&nbsp;OR&nbsp;the option constants.</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">The example in Listing 1 demonstrates registering an inspector object for the propertyopeningBalance.&nbsp;<br /><strong>Listing 1</strong>&nbsp;&nbsp;Registering the inspector as an observer of the openingBalance property&nbsp;<br />- (void)registerAsObserver&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; /*&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; Register 'inspector' to receive change notifications for the "openingBalance" property of&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; the 'account' object and specify that both the old and new values of "openingBalance"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; should be provided in the observe&#8230; method.<br />&nbsp;&nbsp;&nbsp;&nbsp; */<br />&nbsp;&nbsp;&nbsp; [account addObserver:inspector&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; forKeyPath:@"openingBalance"&nbsp;&nbsp;<wbr>&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; options:(<wbr>NSKeyValueObservingOptionNew |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NSKeyValueObservingOptionOld)&nbsp;<wbr>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; context:NULL];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />When you register an object as an observer, you can also provide a context pointer. The context pointer is provided to the observer when&nbsp;observeValueForKeyPath:<wbr>ofObject:change:context:&nbsp;is invoked. The context pointer can be a C pointer or an object reference. The context pointer can be used as a unique identifier to determine the change that is being observed, or to provide some other data to the observer. The context pointer is not retained, and it is the responsibility of the application to ensure that it is not released before the observing object is removed as an observer.</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><strong>Note:</strong>&nbsp;The key-value observing&nbsp;addObserver:forKeyPath:<wbr>options:context:&nbsp;method does not retain the observing object or the observed objects. You need to review your application&#8217;s requirements and manage retain and release for the observing, and observed, objects.</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><strong>Receiving Notification of a Change</strong>&nbsp;<br />When the value of an observed property of an object changes, the observer receives an<a href="http://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html" target="_blank" style="color: #1155cc;"><u>observeValueForKeyPath:<wbr>ofObject:change:context:</u></a>&nbsp;message. All observers must implement this method.</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">The observer is provided the object and key path that triggered the observer notification, a dictionary containing details about the change, and the context pointer that was provided when the observer was registered.</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">The change dictionary entry&nbsp;<a href="http://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html" target="_blank" style="color: #1155cc;"><u>NSKeyValueChangeKindKey</u></a>&nbsp;provides information about the type of change that occurred. If the value of the observed object has changed, the<a href="http://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html" target="_blank" style="color: #1155cc;"><u>NSKeyValueChangeKindKey</u></a>&nbsp;entry returns&nbsp;<a href="http://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html" target="_blank" style="color: #1155cc;"><u>NSKeyValueChangeSetting</u></a>. Depending on the options specified when the observer was registered, the&nbsp;<a href="http://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html" target="_blank" style="color: #1155cc;"><u>NSKeyValueChangeOldKey</u></a>&nbsp;and<a href="http://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html" target="_blank" style="color: #1155cc;"><u>NSKeyValueChangeNewKey</u></a>&nbsp;entries in the change dictionary contain the values of the property before, and after, the change. If the property is an object, the value is provided directly. If the property is a scalar or a C structure, the value is wrapped in an&nbsp;<a href="http://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Classes/NSValue_Class/Reference/Reference.html" target="_blank" style="color: #1155cc;"><u>NSValue</u></a>object (as with key-value coding).</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">If the observed property is a to-many relationship, the&nbsp;<a href="http://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html" target="_blank" style="color: #1155cc;"><u>NSKeyValueChangeKindKey</u></a>&nbsp;entry also indicates whether objects in the relationship were inserted, removed, or replaced by returning&nbsp;<a href="http://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html" target="_blank" style="color: #1155cc;"><u>NSKeyValueChangeInsertion</u></a>,&nbsp;<a href="http://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html" target="_blank" style="color: #1155cc;"><u>NSKeyValueChangeRemoval</u></a>, or<a href="http://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html" target="_blank" style="color: #1155cc;"><u>NSKeyValueChangeReplacement</u></a>, respectively.</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">The change dictionary entry for&nbsp;<a href="http://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html" target="_blank" style="color: #1155cc;"><u>NSKeyValueChangeIndexesKey</u></a>&nbsp;is an&nbsp;NSIndexSetobject specifying the indexes in the relationship that changed. If<a href="http://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html" target="_blank" style="color: #1155cc;"><u>NSKeyValueObservingOptionNew</u></a>&nbsp;or&nbsp;<a href="http://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html" target="_blank" style="color: #1155cc;"><u>NSKeyValueObservingOptionOld</u></a>&nbsp;are specified as options when the observer is registered, the&nbsp;<a href="http://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html" target="_blank" style="color: #1155cc;"><u>NSKeyValueChangeOldKey</u></a>&nbsp;and<a href="http://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html" target="_blank" style="color: #1155cc;"><u>NSKeyValueChangeNewKey</u></a>&nbsp;entries in the change dictionary are arrays containing the values of the related objects before, and after, the change.</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">The example in Listing 2 shows the&nbsp;observeValueForKeyPath:<wbr>ofObject:change:context:&nbsp;implementation for an inspector that reflects the old and new values of the property&nbsp;openingBalance, as registered in&nbsp;<a href="http://developer.apple.com/library/ios/documentation/cocoa/conceptual/KeyValueObserving/Articles/KVOBasics.html" target="_blank" style="color: #1155cc;"><u>Listing 1</u></a>.</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><strong>Listing 2</strong>&nbsp;&nbsp;Implementation of observeValueForKeyPath:<wbr>ofObject:change:context:&nbsp;<br />- (void)observeValueForKeyPath:(<wbr>NSString *)keyPath&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ofObject:(id)object&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; change:(NSDictionary *)change&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; context:(void *)context&nbsp;<br />{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; if ([keyPath isEqual:@"openingBalance"]) {&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [openingBalanceInspectorField setObjectValue:&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [change objectForKey:<wbr>NSKeyValueChangeNewKey]];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; /*&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; Be sure to call the superclass's implementation *if it implements it*.&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; NSObject does not implement the method.&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; */<br />&nbsp;&nbsp;&nbsp; [super observeValueForKeyPath:<wbr>keyPath&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ofObject:object&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; change:change&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; context:context];&nbsp;&nbsp;&nbsp;<br />}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><strong></strong><strong></strong><strong>Removing an Object as an Observer</strong>&nbsp;<br />You remove a key-value observer by sending the observed object aremoveObserver:forKeyPath:&nbsp;message, specifying the observing object and the key path. The example in Listing 3 removes the inspector as an observer of&nbsp;openingBalance.</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><strong>Listing 3</strong>&nbsp;&nbsp;Removing the inspector as an observer of openingBalance&nbsp;<br />- (void)<wbr>unregisterForChangeNotificatio<wbr>n<br />{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; [observedObject removeObserver:inspector&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; forKeyPath:@"openingBalance"];<wbr>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />If the context specified when the observer was registered is an object, it can be safely released only after removing the observer. After receiving a&nbsp;removeObserver:forKeyPath:message, the observing object will no longer receive any&nbsp;observeValueForKeyPath:<wbr>ofObject:change:context:&nbsp;messages for the specified key path and object.</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><a href="http://developer.apple.com/library/ios/documentation/cocoa/conceptual/KeyValueObserving/Articles/KVOCompliance.html" target="_blank" style="color: #1155cc;"><u>Next</u></a><a href="http://developer.apple.com/library/ios/documentation/cocoa/conceptual/KeyValueObserving/KeyValueObserving.html" target="_blank" style="color: #1155cc;"><u>Previous</u></a></p><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><img src="https://mail.google.com/mail/u/0/?ui=2&amp;ik=f710b2434e&amp;view=att&amp;th=13116ca1d11eaaff&amp;attid=0.0.1&amp;disp=emb&amp;zw&amp;atsh=1" alt="Picture (Metafile)" /></p><p align="CENTER" style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&#169;&nbsp;2003, 2011 Apple Inc. All Rights Reserved. (Last updated: 2011-03-08)</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><strong>Did this document help you?&nbsp;</strong><a href="http://developer.apple.com/feedback/?v=1" target="_blank" style="color: #1155cc;"><strong></strong><strong><u>Yes</u></strong><strong></strong></a><strong>&nbsp;</strong><a href="http://developer.apple.com/feedback/?v=2" target="_blank" style="color: #1155cc;"><strong></strong><strong><u>It's good, but...</u></strong><strong></strong></a><strong>&nbsp;</strong><a href="http://developer.apple.com/feedback/?v=3" target="_blank" style="color: #1155cc;"><strong></strong><strong><u>Not helpful...</u></strong><strong></strong></a><strong>&nbsp;</strong><br />Shop the&nbsp;<a href="http://www.apple.com/store/" target="_blank" style="color: #1155cc;"><u>Apple Online Store</u></a>&nbsp;(1-800-MY-APPLE), visit an&nbsp;<a href="http://www.apple.com/retail/" target="_blank" style="color: #1155cc;"><u>Apple Retail Store</u></a>, or find a<a href="http://www.apple.com/buy/locator/" target="_blank" style="color: #1155cc;"><u>reseller</u></a>.</p><ul style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><ul><li style="margin-left: 15px;"><a href="http://lists.apple.com/" target="_blank" style="color: #1155cc;"><u>Mailing Lists</u></a></li><li style="margin-left: 15px;"><a href="http://developer.apple.com/rss/" target="_blank" style="color: #1155cc;"><u>RSS Feeds</u></a></li></ul></ul><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">Copyright &#169; 2010 Apple Inc. All rights reserved.</p><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><a href="http://developer.apple.com/library/ios/documentation/cocoa/conceptual/KeyValueObserving/Articles/KVODependentKeys.html" target="_blank" style="color: #1155cc;"><u>Next</u></a><a href="http://developer.apple.com/library/ios/documentation/cocoa/conceptual/KeyValueObserving/Articles/KVOBasics.html" target="_blank" style="color: #1155cc;"><u>Previous</u></a>&nbsp;<br /><a href="http://developer.apple.com/library/ios/documentation/cocoa/conceptual/KeyValueObserving/index.html" target="_blank" style="color: #1155cc;"><u>http://developer.apple.com/<wbr>library/ios/documentation/<wbr>cocoa/conceptual/<wbr>KeyValueObserving/index.html</u></a>&nbsp;<br /><strong>KVO Compliance</strong>&nbsp;<br />In order to be considered KVO-compliant for a specific property, a class must ensure the following:</p><ul style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><ul><li style="margin-left: 15px;">The class must be key-value coding compliant for the property, as specified in<a href="http://developer.apple.com/library/ios/documentation/cocoa/conceptual/KeyValueCoding/Articles/Compliant.html" target="_blank" style="color: #1155cc;"><u>&#8220;Ensuring KVC Compliance&#8221;</u></a>.</li><p>KVO supports the same data types as KVC.</p><li style="margin-left: 15px;">The class emits KVO change notifications for the property.</li><p>&nbsp;</p></ul></ul><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">There are two techniques for ensuring the change notifications are emitted. Automatic support is provided by&nbsp;NSObject&nbsp;and is by default available for all properties of a class that are key-value coding compliant. Typically, if you follow standard Cocoa coding and naming conventions, you can use automatic change notifications&#8212;you don&#8217;t have to write any additional code.</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">Manual change notification provides additional control over when notifications are emitted, and requires additional coding. You can control automatic notifications for properties of your subclass by implementing the class method&nbsp;<a href="http://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html" target="_blank" style="color: #1155cc;"><u>automaticallyNotifiesObservers<wbr>ForKey:</u></a>.</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">[自：2种技术：自动提交和手动提交。]&nbsp;<br /><strong>Automatic Change Notification</strong>&nbsp;<br />[自：这种方法好像是默认的，<wbr>我没有发现特殊的函数调用来触发自动Notification机<wbr>制。]&nbsp;<br />NSObject&nbsp;provides a basic implementation of automatic key-value change notification. Automatic key-value change notification informs observers of changes made using key-value compliant accessors, as well as the key-value coding methods. Automatic notification is also supported by the collection proxy objects returned by, for example,mutableArrayValueForKey:.</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">The examples shown in Listing 1 result in any observers of the property&nbsp;name&nbsp;to be notified of the change.&nbsp;<br /><strong>Listing 1</strong>&nbsp;&nbsp;Examples of method calls that cause KVO change notifications to be emitted&nbsp;<br />// Call the accessor method.&nbsp;&nbsp;&nbsp;<br />[account setName:@"Savings"];&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />// Use setValue:forKey:.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />[account setValue:@"Savings" forKey:@"name"];&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />// Use a key path, where 'account' is a kvc-compliant property of 'document'.&nbsp;&nbsp;<br />[document setValue:@"Savings" forKeyPath:@"<a href="http://account.name/" target="_blank" style="color: #1155cc;">account.name</a>"];&nbsp;&nbsp;<wbr>&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />// Use mutableArrayValueForKey: to retrieve a relationship proxy object.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />Transaction *newTransaction = &lt;#Create a new transaction for the account#&gt;;&nbsp;&nbsp;&nbsp;&nbsp;<br />NSMutableArray *transactions = [account mutableArrayValueForKey:@"<wbr>transactions"];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />[transactions addObject:newTransaction];&nbsp;&nbsp;&nbsp;&nbsp;<wbr>&nbsp;&nbsp;&nbsp;<br /><strong></strong><strong></strong><strong>Manual Change Notification</strong>&nbsp;<br />Manual change notification provides more granular control over how and when notifications are sent to observers. This can be useful to help minimize triggering notifications that are unnecessary, or to group a number of changes into a single notification.</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">A class that implements manual notification must override the&nbsp;NSObject&nbsp;implementation of<a href="http://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html" target="_blank" style="color: #1155cc;"><u>automaticallyNotifiesObservers<wbr>ForKey:</u></a>. It is possible to use both automatic and manual observer notifications in the same class. For properties that perform manual notification, the subclass implementation of&nbsp;automaticallyNotifiesObservers<wbr>ForKey:&nbsp;should return&nbsp;NO. A subclass implementation should invoke&nbsp;super&nbsp;for any unrecognized keys. The example in Listing 2 enables manual notification for theopeningBalance&nbsp;property allowing the superclass to determine the notification for all other keys.</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><strong>Listing 2</strong>&nbsp;&nbsp;Example implementation of automaticallyNotifiesObservers<wbr>ForKey:&nbsp;<br />+ (BOOL)<wbr>automaticallyNotifiesObservers<wbr>ForKey:(NSString *)theKey {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; BOOL automatic = NO;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; if ([theKey isEqualToString:@"<wbr>openingBalance"]) {&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; automatic = NO;<br />&nbsp;&nbsp;&nbsp; } else {&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; automatic=[super automaticallyNotifiesObservers<wbr>ForKey:theKey];&nbsp;<br />&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; return automatic;&nbsp;&nbsp;<br />}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />To implement manual observer notification, you invoke&nbsp;<a href="http://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html" target="_blank" style="color: #1155cc;"><u>willChangeValueForKey:</u></a>&nbsp;before changing the value, and&nbsp;<a href="http://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html" target="_blank" style="color: #1155cc;"><u>didChangeValueForKey:</u></a>&nbsp;after changing the value. The example in Listing 3 implements manual notifications for the&nbsp;openingBalance&nbsp;property.</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><strong>Listing 3</strong>&nbsp;&nbsp;Example accessor method implementing manual notification&nbsp;<br />- (void)setOpeningBalance:(<wbr>double)theBalance {&nbsp;<br />&nbsp;&nbsp;&nbsp; [self willChangeValueForKey:@"<wbr>openingBalance"];&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; openingBalance=theBalance;&nbsp;<br />&nbsp;&nbsp;&nbsp; [self didChangeValueForKey:@"<wbr>openingBalance"];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />You can minimize sending unnecessary notifications by first checking if the value has changed. The example in Listing 4 tests the value of&nbsp;openingBalance&nbsp;and only provides the notification if it has changed.</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><strong>Listing 4</strong>&nbsp;&nbsp;Testing the value for change before providing notification&nbsp;<br />- (void)setOpeningBalance:(<wbr>double)theBalance {&nbsp;<br />&nbsp;&nbsp;&nbsp; if (theBalance != openingBalance) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [self willChangeValueForKey:@"<wbr>openingBalance"];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; openingBalance=theBalance;&nbsp;&nbsp;&nbsp;&nbsp;<wbr>&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [self didChangeValueForKey:@"<wbr>openingBalance"];&nbsp;<br />&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;<br />}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />If a single operation causes multiple keys to change you must nest the change notifications as shown in Listing 5.[自：注意：如果是2个以上的变化，<wbr>则需要使用例子中的方法，而非单独Notification。]</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><strong>Listing 5</strong>&nbsp;&nbsp;Nesting change notifications for multiple keys&nbsp;<br />- (void)setOpeningBalance:(<wbr>double)theBalance {&nbsp;<br />&nbsp;&nbsp;&nbsp; [self willChangeValueForKey:@"<wbr>openingBalance"];&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; [self willChangeValueForKey:@"<wbr>itemChanged"];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; openingBalance=theBalance;&nbsp;<br />&nbsp;&nbsp;&nbsp; itemChanged=itemChanged+1;&nbsp;<br />&nbsp;&nbsp;&nbsp; [self didChangeValueForKey:@"<wbr>itemChanged"];<br />&nbsp;&nbsp;&nbsp; [self didChangeValueForKey:@"<wbr>openingBalance"];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />In the case of an ordered to-many relationship, you must specify not only the key that changed, but also the type of change and the indexes of the objects involved. The type of change is an&nbsp;<a href="http://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html" target="_blank" style="color: #1155cc;"><u>NSKeyValueChange</u></a>&nbsp;that specifies&nbsp;<a href="http://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html" target="_blank" style="color: #1155cc;"><u>NSKeyValueChangeInsertion</u></a>,<a href="http://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html" target="_blank" style="color: #1155cc;"><u>NSKeyValueChangeRemoval</u></a>, or&nbsp;<a href="http://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Protocols/NSKeyValueObserving_Protocol/Reference/Reference.html" target="_blank" style="color: #1155cc;"><u>NSKeyValueChangeReplacement</u></a>. The indexes of the affected objects are passed as an&nbsp;<a href="http://developer.apple.com/library/ios/documentation/cocoa/Reference/Foundation/Classes/NSIndexSet_Class/Reference/Reference.html" target="_blank" style="color: #1155cc;"><u>NSIndexSet</u></a>&nbsp;object.</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">The code fragment in Listing 6 demonstrates how to wrap a deletion of objects in the to-many relationship&nbsp;transactions.&nbsp;<br /><strong>Listing 6</strong>&nbsp;&nbsp;Implementation of manual observer notification in a to-many relationship&nbsp;<br />- (void)<wbr>removeTransactionsAtIndexes:(<wbr>NSIndexSet *)indexes {&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; [self willChange:<wbr>NSKeyValueChangeRemoval&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp; valuesAtIndexes:indexes forKey:@"transactions"];&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; // remove the transaction objects at the specified indexes here&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; [self didChange:<wbr>NSKeyValueChangeRemoval&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp; valuesAtIndexes:indexes forKey:@"transactions"];&nbsp;&nbsp;&nbsp;<br />}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /><strong></strong><strong></strong><strong>Note:</strong>&nbsp;In a reference counted environment, ou must make sure that you do not release the values that will change before sending a&nbsp;willChange&nbsp;message.</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><a href="http://developer.apple.com/library/ios/documentation/cocoa/conceptual/KeyValueObserving/Articles/KVODependentKeys.html" target="_blank" style="color: #1155cc;"><u>Next</u></a><a href="http://developer.apple.com/library/ios/documentation/cocoa/conceptual/KeyValueObserving/Articles/KVOBasics.html" target="_blank" style="color: #1155cc;"><u>Previous</u></a></p><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><img src="https://mail.google.com/mail/u/0/?ui=2&amp;ik=f710b2434e&amp;view=att&amp;th=13116ca1d11eaaff&amp;attid=0.0.2&amp;disp=emb&amp;zw&amp;atsh=1" alt="Picture (Metafile)" /></p><p align="CENTER" style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&#169;&nbsp;2003, 2011 Apple Inc. All Rights Reserved. (Last updated: 2011-03-08)</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><strong>Did this document help you?&nbsp;</strong><a href="http://developer.apple.com/feedback/?v=1" target="_blank" style="color: #1155cc;"><strong></strong><strong><u>Yes</u></strong><strong></strong></a><strong>&nbsp;</strong><a href="http://developer.apple.com/feedback/?v=2" target="_blank" style="color: #1155cc;"><strong></strong><strong><u>It's good, but...</u></strong><strong></strong></a><strong>&nbsp;</strong><a href="http://developer.apple.com/feedback/?v=3" target="_blank" style="color: #1155cc;"><strong></strong><strong><u>Not helpful...</u></strong><strong></strong></a><strong>&nbsp;</strong><br />Shop the&nbsp;<a href="http://www.apple.com/store/" target="_blank" style="color: #1155cc;"><u>Apple Online Store</u></a>&nbsp;(1-800-MY-APPLE), visit an&nbsp;<a href="http://www.apple.com/retail/" target="_blank" style="color: #1155cc;"><u>Apple Retail Store</u></a>, or find a<a href="http://www.apple.com/buy/locator/" target="_blank" style="color: #1155cc;"><u>reseller</u></a>.</p><ul style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><ul><li style="margin-left: 15px;"><a href="http://lists.apple.com/" target="_blank" style="color: #1155cc;"><u>Mailing Lists</u></a></li><li style="margin-left: 15px;"><a href="http://developer.apple.com/rss/" target="_blank" style="color: #1155cc;"><u>RSS Feeds</u></a></li></ul></ul><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">Copyright &#169; 2010 Apple Inc. All rights reserved.</p><ul style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><ul><li style="margin-left: 15px;"><a href="http://www.apple.com/legal/terms/site.html" target="_blank" style="color: #1155cc;"><u>Terms of Use</u></a></li><li style="margin-left: 15px;"><a href="http://www.apple.com/legal/privacy/" target="_blank" style="color: #1155cc;"><u>Privacy Policy</u></a></li></ul></ul><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><img src="https://mail.google.com/mail/u/0/?ui=2&amp;ik=f710b2434e&amp;view=att&amp;th=13116ca1d11eaaff&amp;attid=0.0.3&amp;disp=emb&amp;zw&amp;atsh=1" alt="Picture (Metafile)" />&nbsp;<img src="https://mail.google.com/mail/u/0/?ui=2&amp;ik=f710b2434e&amp;view=att&amp;th=13116ca1d11eaaff&amp;attid=0.0.4&amp;disp=emb&amp;zw&amp;atsh=1" alt="Picture (Metafile)" />&nbsp;<img src="https://mail.google.com/mail/u/0/?ui=2&amp;ik=f710b2434e&amp;view=att&amp;th=13116ca1d11eaaff&amp;attid=0.0.5&amp;disp=emb&amp;zw&amp;atsh=1" alt="Picture (Metafile)" />&nbsp;<img src="https://mail.google.com/mail/u/0/?ui=2&amp;ik=f710b2434e&amp;view=att&amp;th=13116ca1d11eaaff&amp;attid=0.0.6&amp;disp=emb&amp;zw&amp;atsh=1" alt="Picture (Metafile)" />&nbsp;<img src="https://mail.google.com/mail/u/0/?ui=2&amp;ik=f710b2434e&amp;view=att&amp;th=13116ca1d11eaaff&amp;attid=0.0.7&amp;disp=emb&amp;zw&amp;atsh=1" alt="Picture (Metafile)" />&nbsp;<img src="https://mail.google.com/mail/u/0/?ui=2&amp;ik=f710b2434e&amp;view=att&amp;th=13116ca1d11eaaff&amp;attid=0.0.8&amp;disp=emb&amp;zw&amp;atsh=1" alt="Picture (Metafile)" />&nbsp;<img src="https://mail.google.com/mail/u/0/?ui=2&amp;ik=f710b2434e&amp;view=att&amp;th=13116ca1d11eaaff&amp;attid=0.0.9&amp;disp=emb&amp;zw&amp;atsh=1" alt="Picture (Metafile)" />&nbsp;<img src="https://mail.google.com/mail/u/0/?ui=2&amp;ik=f710b2434e&amp;view=att&amp;th=13116ca1d11eaaff&amp;attid=0.0.10&amp;disp=emb&amp;zw&amp;atsh=1" alt="Picture (Metafile)" />&nbsp;<img src="https://mail.google.com/mail/u/0/?ui=2&amp;ik=f710b2434e&amp;view=att&amp;th=13116ca1d11eaaff&amp;attid=0.0.11&amp;disp=emb&amp;zw&amp;atsh=1" alt="Picture (Metafile)" />&nbsp;<img src="https://mail.google.com/mail/u/0/?ui=2&amp;ik=f710b2434e&amp;view=att&amp;th=13116ca1d11eaaff&amp;attid=0.0.12&amp;disp=emb&amp;zw&amp;atsh=1" alt="Picture (Metafile)" /></p><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><ul style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><ul><li style="margin-left: 15px;"><a href="http://www.apple.com/legal/terms/site.html" target="_blank" style="color: #1155cc;"><u>Terms of Use</u></a></li><li style="margin-left: 15px;"><a href="http://www.apple.com/legal/privacy/" target="_blank" style="color: #1155cc;"><u>Privacy Policy</u></a></li></ul></ul><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><img src="https://mail.google.com/mail/u/0/?ui=2&amp;ik=f710b2434e&amp;view=att&amp;th=13116ca1d11eaaff&amp;attid=0.0.13&amp;disp=emb&amp;zw&amp;atsh=1" alt="Picture (Metafile)" />&nbsp;<img src="https://mail.google.com/mail/u/0/?ui=2&amp;ik=f710b2434e&amp;view=att&amp;th=13116ca1d11eaaff&amp;attid=0.0.14&amp;disp=emb&amp;zw&amp;atsh=1" alt="Picture (Metafile)" />&nbsp;<img src="https://mail.google.com/mail/u/0/?ui=2&amp;ik=f710b2434e&amp;view=att&amp;th=13116ca1d11eaaff&amp;attid=0.0.15&amp;disp=emb&amp;zw&amp;atsh=1" alt="Picture (Metafile)" />&nbsp;<img src="https://mail.google.com/mail/u/0/?ui=2&amp;ik=f710b2434e&amp;view=att&amp;th=13116ca1d11eaaff&amp;attid=0.0.16&amp;disp=emb&amp;zw&amp;atsh=1" alt="Picture (Metafile)" />&nbsp;<img src="https://mail.google.com/mail/u/0/?ui=2&amp;ik=f710b2434e&amp;view=att&amp;th=13116ca1d11eaaff&amp;attid=0.0.17&amp;disp=emb&amp;zw&amp;atsh=1" alt="Picture (Metafile)" />&nbsp;<img src="https://mail.google.com/mail/u/0/?ui=2&amp;ik=f710b2434e&amp;view=att&amp;th=13116ca1d11eaaff&amp;attid=0.0.18&amp;disp=emb&amp;zw&amp;atsh=1" alt="Picture (Metafile)" />&nbsp;<img src="https://mail.google.com/mail/u/0/?ui=2&amp;ik=f710b2434e&amp;view=att&amp;th=13116ca1d11eaaff&amp;attid=0.0.19&amp;disp=emb&amp;zw&amp;atsh=1" alt="Picture (Metafile)" />&nbsp;<img src="https://mail.google.com/mail/u/0/?ui=2&amp;ik=f710b2434e&amp;view=att&amp;th=13116ca1d11eaaff&amp;attid=0.0.20&amp;disp=emb&amp;zw&amp;atsh=1" alt="Picture (Metafile)" />&nbsp;<img src="https://mail.google.com/mail/u/0/?ui=2&amp;ik=f710b2434e&amp;view=att&amp;th=13116ca1d11eaaff&amp;attid=0.0.21&amp;disp=emb&amp;zw&amp;atsh=1" alt="Picture (Metafile)" />&nbsp;<img src="https://mail.google.com/mail/u/0/?ui=2&amp;ik=f710b2434e&amp;view=att&amp;th=13116ca1d11eaaff&amp;attid=0.0.22&amp;disp=emb&amp;zw&amp;atsh=1" alt="Picture (Metafile)" /></p><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">+++++</p><img src ="http://www.cppblog.com/walkklookk/aggbug/196825.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-30 10:20 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/30/196825.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>转：User interface strings in Cocoa -国际化，本地化</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/30/196824.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sun, 30 Dec 2012 02:10:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/30/196824.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196824.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/30/196824.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196824.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196824.html</trackback:ping><description><![CDATA[<ul style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><p>日期：2011.08.01</p><br /><p><strong></strong><a href="http://cocoawithlove.com/2011/04/user-interface-strings-in-cocoa.html" target="_blank" style="color: #1155cc;"><strong><u>User interface strings in Cocoa</u></strong><strong></strong></a><strong></strong><strong></strong><strong></strong><strong></strong></p><p>In this post, I'll look at best practice for using and managing text strings in your user interface. This is a fairly simple topic but Cocoa has established "best practices" for handling user interface strings that new Cocoa developers should be aware of. Since it is inevitably related, I'll also look at the steps involved in localizing the strings in your applications but remember: you should follow good practice for string handling, even if you have no intention of ever translating your application.</p><br /><p><strong>Introduction (the wrong way)</strong></p><br /><p>Putting a text string in your user interface is not a difficult thing to do on a technical level. In code, filling in text can be as simple as setting the&nbsp;textproperty of a&nbsp;UILabel&nbsp;to a literal string:</p><p>someUserInterfaceLabel.text = @"Text to display";</p><p>(This code is for an iOS&nbsp;UILabel. On Mac OS X, you would set thestringValue&nbsp;property of an&nbsp;NSTextField&nbsp;but otherwise the step is the same.)</p><p>While this will work, you should never set a user interface string this way.</p><br /><p><strong>Setting labels with literal strings (the right way)</strong></p><br /><p>The most thorough way to put a literal string into your Cocoa application's user interface is:</p><p>someUserInterfaceLabel.text =<br />&nbsp;&nbsp;&nbsp; NSLocalizedStringFromTable(<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @"Text to display",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // the native language string<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @"SomePageLabels",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // the category<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @"Label display string"); // a comment describing context</p><p>This is pretty verbose though. It is often okay to just use:</p><p>someUserInterfaceLabel.text = NSLocalizedString(@"Text to display", nil);</p><p>If you take no other steps, this will produce exact the same output as the "wrong way" example.</p><p>You should<em>&nbsp;always</em>&nbsp;use the&nbsp;NSLocalizedString[...] macros for<em>&nbsp;every</em>&nbsp;user interface string in your code.</p><p>But wait... this&nbsp;NSLocalizedString[...] stuff requires more typing and unless you take yet more additional steps, it won't have any functional difference? If I'm not planning to translate my program right now, aren't they a complete waste of time?</p><br /><p><strong>Why NSLocalizedString is important, even if you don't intend to translate</strong></p><br /><p>Obviously, the&nbsp;NSLocalizedString[...] functions (and the less commonCFCopyLocalizedString[...] variants) are functions that exist to enable localization (i.e. letting you translate your application into different languages).</p><p>Technically, they're not even functions &#8212; they're just macros that invoke the&nbsp;-[NSBundle localizedStringForKey:value:<wbr>table:]&nbsp;method &#8212; but you should always use the macro and not the underlying method for reasons I'll discuss in the "Mechanics of Translation" section below.</p><p>However, even if you're not intending to ever localize your application, you should<em>&nbsp;always</em>&nbsp;use&nbsp;NSLocalizedString.</p><p>There are a few reasons for this:</p><ol type="1"><li style="margin-left: 15px;"><strong>Futureproofing</strong>: The future is hard to predict: you never know if you'll want to translate in the future. Needing to go through your code and find rogue literal strings is time consuming and prone to mistakes. Instead, everything should always have&nbsp;NSLocalizedString&nbsp;from the beginning.</li><li style="margin-left: 15px;"><strong>MVC practices</strong>: It keeps the exact details of your model/presentation layer at least one level of indirection removed from your controller code. In some cases, you can simply change the .strings files for your program to update the user interface and not need to change code due to this separation.</li><li style="margin-left: 15px;"><strong>Separation of concerns</strong>: It clearly identifies text strings intended for user presentation as opposed to text strings used as keys for programming use only.</li><li style="margin-left: 15px;"><strong>Discourages other bad practices</strong>: with your user interface strings detached from your controller, you'll be less likely to try to read static strings back from the user interface (a very bad idea) or place programmer-targetted strings in the user-interface.</li><br /></ol><p>Get into the habit of using&nbsp;NSLocalizedString. It's really simple to do &#8212; even when you're hacking code together quickly, you should be able to use it.</p><p>The first two points in the previous list are self-explanatory but the second two merit further explanation.</p><br /><p><strong>Separation of concerns</strong></p><br /><p>It is always helpful in programming to be able to glance at code and understand the intent. Consider the following piece of code in isolation:</p><p>[someDictionary setObject:@"value" forKey:SomeKeyNameString];<br />[someDictionary setObject:NSLocalizedString(@"<wbr>value", nil) forKey:SomeOtherKeyNameString]<wbr>;</p><p>Without knowing what&nbsp;someDictionary&nbsp;is for or what the purpose of theSomeKeyNameString&nbsp;and&nbsp;SomeOtherKeyNameString&nbsp;values are, we know that the second string is intended for display in the user interface at some point whereas the first string is purely for tracking a value internally.</p><p>This clear labelling of intent is helpful as strings for user display have a very different role in a program compared to strings for internal use.</p><br /><p><strong>Discourages other bad practices</strong></p><br /><p>If you treat&nbsp;NSLocalizedString&nbsp;in your mind as though its output is a black box, this can help you avoid poor controller design when managing user interface elements. It can act as a conceptual tool to encourage you to design things the right way, instead of a lazy way.</p><p>Your controller code should treat user interface strings as something that can be written but not read. Reading static strings back from the user interface is always bad (it ends up being a form of "<a href="http://en.wikipedia.org/wiki/Coupling_%28computer_programming%29" target="_blank" style="color: #1155cc;"><u>common or data coupling</u></a>" &#8212; a bad design practice).</p><p>In the "Separation of concerns" example above, you might consider that since the keys&nbsp;SomeKeyNameString&nbsp;and&nbsp;SomeOtherKeyNameString&nbsp;are defined in global variables in this example, that perhaps you'd want to define your localized strings in global variables. In most cases a global variable for a user string is actually a bad idea.</p><p>We define dictionary keys in global variables because more than one location in the program may need to use exact the same value or the exchange of information between the two points will fail. But with user interface strings, you should never have a second piece of code that requires the exact same value: you should never read back from the user interface or require user interface collusion. Generally, the only situation where the same string should appear multiple times is if the same user interface code is displaying it (i.e. you're drawing the same object) but in this case, the code is common and the string should only need to appear once in the code.</p><p>If you need to uniquely identify a label or the state of a text displaying item, testing the text it contains is the wrong way to do that. A far better way is to use the&nbsp;tag&nbsp;value of any&nbsp;UIView/NSActionCell&nbsp;and then map the&nbsp;tag&nbsp;value onto the object's role or function (tag&nbsp;is a pointer sized value so you can store a non-retained object reference here if needed, not just an integer). The&nbsp;tagproperty is not reserved for any other purpose; it is intended for the controller to track user interface items and their state.</p><br /><p><strong>Mechanics of translation (when you're ready)</strong></p><br /><p>Eventually, you may actually have to translate your program. Let's look at the steps involved.</p><br /><p><strong>Create your ".strings" files</strong></p><br /><p>The files you need to translate are the ".strings" files in your application. By default though, your project probably won't have any ".strings" files (except possibly an InfoPlist.strings file which is for translating your Info.plist file's strings).</p><p>The first step is to make sure you have a localized directory somewhere (probably in the Resources subdirectory of your project's folder). The localized directory should be named "en.lproj" if you're starting with English strings, otherwise you'll want to replace "en" with the appropriate ISO 639-1 or ISO 639-2 designators. If needed you can use the script and region identifiers too as described in&nbsp;<a href="http://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPInternational/Articles/LanguageDesignations.html" target="_blank" style="color: #1155cc;"><u>Apple's Language and Locale Designations</u></a>.</p><p>A note on folder names: it is common to see "English.lproj" used as the name for English localization instead of "en.lproj" &#8212; in fact, Xcode 3 still generates folders with this name if you Get Info on a file and select "Make file localizable". Apple have stated that these old, "full" names are deprecated from Mac OS X 10.4 onwards in favor of ISO 639-1 or ISO 639-2 designators. Don't use the old "English.proj" style names anymore and replace with "en.lproj" if it is autocreated (yes, you might need to update your Xcode paths if you change the folder name).</p><p>Now we can create ".strings" files automatically from all theNSLocalizedString&nbsp;references in your program. To do this, open a Terminal in your Project's root directory and run the following command:</p><p>find -E . -iregex '.*\.(m|h|mm)$' -print0 | xargs -0 genstrings -a -o Resources/en.lproj</p><p>This will process all .m, .h and .mm files in your directory hierarchy and create ".strings" files for them in the en.lproj directory (note, the en.lproj directory must already exist). This assumes that the localized resources directory you created is located at "Resources/en.proj", relative to your Project's root directory; obviously, you'll need to change this if your localized resources are elsewhere.</p><p>The ".strings" file will be filled with entries that look like this:</p><p>/* This comment provided comes from the last parameter of the NSLocalizedString macro. */<br />"Some UI string %@ to translate %@" = "Some UI string %1$@ to translate %2$@";</p><p>Your translator just needs to translate the right-hand side of the equality statement. Notice that placeholders in your strings are given ordinal positions (1 and 2 in this case) so that the translation can change the order of placeholders if necessary (obviously, if you use placeholders, you should include a comment that explains what they're going to be).</p><ul><p><strong>Localization versus Internationalization:</strong>&nbsp;generally, the whole process of creating new language variants is referred to as localization. In reality though, it comprises two steps:<br /></p><ol type="1"><li style="margin-left: 15px;"><strong>Internationalization</strong>: where you decouple the program from the original locale</li><li style="margin-left: 15px;"><strong>Localization</strong>: where you add translations and behaviors for each new locale</li><br /></ol></ul><p>By that terminology, the inclusion of&nbsp;NSLocalizedString&nbsp;wrappers and the creation of ".strings" files is the "Internationalizing" phase.</p><br /><p><strong>genstrings will only handle static NSLocalizedString and CFCopyLocalizedString strings</strong></p><br /><p>The only strings that will be automatically extracted are those wrapped inNSLocalizedString[...] and&nbsp;CFCopyLocalizedString[...] macros. Obviously, all your user interface text needs to be wrapped in these but also remember that the underlying&nbsp;-[NSBundle localizedStringForKey:value:<wbr>table:]method will<em>&nbsp;not</em>&nbsp;be automatically extracted.</p><p>Why would you ever use&nbsp;-[NSBundle localizedStringForKey:value:<wbr>table:]&nbsp;directly then? The answer is for dynamically generated strings.</p><p>The genstrings command will raise an error if it detects anything other than a static string in the localization macros. This is appropriate because you don't want your translators translating variable names and function calls (they only need to translate the results of those calls).</p><p>The answer to why you would use&nbsp;-[NSBundle localizedStringForKey:value:<wbr>table:]&nbsp;is then: the actual strings to be translated are located elsewhere in the code (or are in a ".strings" file that was not generated from code) and you are simply looking them up dynamically.</p><br /><p><strong>Encoding problems</strong></p><br /><p>From Mac OS X 10.5 onwards, you can put any UTF-8 characters in yourNSLocalizedString&nbsp;constants. Prior to this, they were required to be pure 7-bit ASCII with all Unicode escaped with&nbsp;<a style="color: #1155cc;"><u>\\Uxxxx</u></a>&nbsp;style escaping or you could use MacRoman with the -macRoman command-line option to use MacRoman high-ASCII characters.</p><p><strong>A quick swipe at almost everybody</strong>: UTF-8 has been around since 1993 and Unicode 2.0 since 1996; if you have created any 8-bit character content since 1996 in anything other than UTF-8, then I hate you.<br /><br />I weep to think of the years of programmer time that are still wasted attempting to support non-Unicode formats without characters getting garbled because people are still creating content using ancient encodings without useful identifiers to indicate what nonsense encoding they're using (or worse, people creating content that explicitly uses the wrong encoding for an encoding-specific text field).<br /><br />MacRoman? Atrocious. Big-5? I hope you want to see garbage output. Windows Latin? You suck. If you're creating new content using anything other than UTF-8, UTF-16 or UTF-32 then you should be forced to serve prison time with whatever idiot monkey decided that UTF-16 should be allowed little-endian and big-endian variants instead of a single authoritative encoding.</p><p>The actual text files generated by genstrings are UTF-16 in whatever byte order your system happens to use. i.e. UTF-16BE on PowerPC and UTF-16LE on Intel Macs.</p><p>Grumble.</p><br /><p><strong>Translating XIB files</strong></p><br /><p>Not all your strings will come from your code. The other common text location is in XIB files. XIB files can be a little bit trickier than strings in code due to two factors:</p><ol type="1"><li style="margin-left: 15px;">While you can extract the strings from a XIB file easily, you also have to merge them back in once the translation is complete &#8212; basically another step that can go wrong</li><li style="margin-left: 15px;">The ".strings" file format extracted from XIB files is uglier and doesn't have easy room for comments to send to the translator</li><br /></ol><p>For these two reasons, I generally avoid putting text in XIB files if reasonably possible &#8212; it is normally easier to have text inserted at NIB load time by the code. Of course, menus, button labels and automator labels can't reasonably be moved into code so you're still likely to need a number of XIB files translated.</p><p>You extract the .strings from XIB files in a similar way to extracting the strings from code. However, first we must make all of our XIB files localizable (if they aren't already).</p><p>To localize your XIB files, select them all in the Xcode project Group Tree, Get Info on them and then from the first tab, select "Make file localizable".</p><p>Then, go to the localized directory where your files all ended up (if there's multiple, you'll need to do this for each one) and run the following in Terminal:</p><p>for file in *.xib; do<br />&nbsp;&nbsp;&nbsp; ibtool --export-strings-file "$file".strings "$file"<br />done</p><p>This will generate all the ".strings" files for your XIB files.</p><p>Once the ".strings" files are localized, create a new ".lproj" directory with the appropriate language name for the new translations and put all the ".strings" files in it. Then open a Terminal in this new folder and run:</p><p>for file in *.xib.strings; do<br />&nbsp;&nbsp;&nbsp; basename=`basename "$file" .strings`<br />&nbsp;&nbsp;&nbsp; ibtool --strings-file "$file" --write "$basename" "../en.lproj/$basename"<br />done</p><p>This will merge all the ".xib.strings" files in the current directory with the XIB files from the en.lproj directory, creating the translated XIB files.</p><br /><p><strong>Translating other resources</strong></p><br /><p>The same "Make file localizable" step that we used for the XIB files in the previous section can be applied to any resource file in your Xcode group tree so you can localize other resources in whatever way is apppropriate.</p><p>Here's a tip though: avoid localizing anything other than strings and XIB files by whatever means possible. Having non-strings files for translation will cause you nothing but pain and suffering.</p><p>In particular:<strong>&nbsp;avoid localizing images</strong>. Work as hard as you can to keep all text out of images (except in logos that don't require translation). You can perform quite sophisticated drawing and text handling in Cocoa code if needed and this will almost always be easier than localizing images.</p><p>I haven't really touched on non-string code localization topics in this post. There's date, time, numbers, error descriptions and other stuff &#8212; most of the time, the classes and APIs for these make it clear what you need to do. Just read&nbsp;<a href="http://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPInternational/BPInternational.html%23//apple_ref/doc/uid/10000171-SW1" target="_blank" style="color: #1155cc;"><u>Apple's Internationalization documentation</u></a>.</p><br /><p><strong>Conclusion</strong></p><br /><p>Most programmers should already know the information in this post. Numerous other Mac programming blogs have discussed the topic:</p><ul><li style="margin-left: 15px;"><a href="http://wilshipley.com/blog/2009/10/pimp-my-code-part-17-lost-in.html" target="_blank" style="color: #1155cc;"><u>Call Me Fishmeal: Pimp My Code, Part 17: Lost in Translations.</u></a></li><li style="margin-left: 15px;"><a href="http://www.stone.com/The_Cocoa_Files/Internationalize_Your_App.html" target="_blank" style="color: #1155cc;"><u>OS X &amp; Cocoa Writings: Internationalizing Cocoa Applications, by Andrew C Stone</u></a></li><li style="margin-left: 15px;"><a href="http://homepage.mac.com/mmalc/Stepwise/Internationalization/" target="_blank" style="color: #1155cc;"><u>Internationalizing Cocoa applications, mmalcolm Crawford</u></a></li><br /></ul><p>See how anciently old those second two links are? I'm not telling you new information. The advice remains the same: always,<em>&nbsp;ALWAYS</em>&nbsp;useNSLocalizedString&nbsp;for your user interface strings.</p><p>Actual translation can happen later or not at all &#8212; my point is that your need for translation (or lack of need) should not determine whether you useNSLocalizedString&nbsp;as it is best practice in any case. Of course you can rest assured that translation will all work out if your code is alreadyNSLocalizedString-compliant.</p><br /><br /><p>+++++</p></ul><img src ="http://www.cppblog.com/walkklookk/aggbug/196824.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-30 10:10 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/30/196824.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>转：修改XCode文件的公司名 --2</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/29/196814.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sat, 29 Dec 2012 11:53:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/29/196814.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196814.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/29/196814.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196814.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196814.html</trackback:ping><description><![CDATA[<div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">下面的方法只对XCode 3.0有效，对于XCode 4要在系统的Accounts里面修改登陆的名字，<wbr>并在Address Book里面修改公司信息，这样才能修改。</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">&nbsp;</div><div dir="ltr" align="left" style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">转:</span><a href="http://iphonedevelopertips.com/xcode/change-company-name-from-within-xcode-on-a-per-project-basis.html" target="_blank" style="color: #1155cc; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">http://iphonedevelopertips.<wbr>com/xcode/change-company-name-<wbr>from-within-xcode-on-a-per-<wbr>project-basis.html</a><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><h1><a title="Permanent Link: Change Company Name from Within  Xcode on a Per Project Basis" href="http://iphonedevelopertips.com/xcode/change-company-name-from-within-xcode-on-a-per-project-basis.html" rel="bookmark" target="_blank" style="color: #1155cc;">Change Company Name from Within Xcode on a Per Project Basis</a></h1><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">Posted on March 25, 2010 by&nbsp;<a title="Posts by John  Muchow" href="http://iphonedevelopertips.com/author/admin/" target="_blank" style="color: #1155cc;">John Muchow</a>&nbsp;in&nbsp;<a title="View all  posts in Xcode" href="http://iphonedevelopertips.com/category/xcode" rel="category tag" target="_blank" style="color: #1155cc;">Xcode</a></div><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff; float: right;"></div><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">A common question for most anyone new to Xcode is how to change the Company Name that is added to each new source file. The default information looks similar to the following:</p><blockquote style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><p>Copyright (c) 2010 MyCompanyName. All rights reserved.</p></blockquote><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">Up until Xcode 3.2 to change this value you had to resort to running a command from a terminal, you can see how this was previously done in this tip:&nbsp;<a href="http://iphonedevelopertips.com/xcode/change-company-name-in-xcode.html" target="_blank" style="color: #1155cc;">Change Company Name in Xcode</a>.</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">A welcome change with 3.2+ is that this value can now be managed from within Xcode. Even better, the name can be configured on a project by project basis, which is really nice if you need to maintain multiple copyright statements across projects.</p><h5>Change Company Name within Xcode</h5><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">In the Groups and Files window, right click on the project name and select&nbsp;<strong>Get Info</strong>:</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><img  alt="" /></p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">In the&nbsp;<strong>General Settings</strong>&nbsp;tab, near the bottom you&#8217;ll see a field for the Organization Name:</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><img  alt="" /></p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">Update this field and the new value will be reflected in each source code file you create in the active project.</p><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">+++++</span><img src ="http://www.cppblog.com/walkklookk/aggbug/196814.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-29 19:53 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/29/196814.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>原：iPhone编程小经验 -7</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/29/196813.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sat, 29 Dec 2012 11:52:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/29/196813.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196813.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/29/196813.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196813.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196813.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 日期：2011.04.29日期：2011.05.26日期：2011.06.09日期：2011.06.16日期：2011.06.27日期：2011.06.29日期：2011.08.26*自定义的类型，进行传递的时候，尽量使用retain的方式。否则，每次使用copy的方式，每次添加新的变量，都要更新copy协议的内容， 很麻烦。*成员变量的访问函数，最好不要处理的很复杂，尽量使用系统的就可以了。如果...&nbsp;&nbsp;<a href='http://www.cppblog.com/walkklookk/archive/2012/12/29/196813.html'>阅读全文</a><img src ="http://www.cppblog.com/walkklookk/aggbug/196813.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-29 19:52 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/29/196813.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>原:CorePlot经验</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/29/196812.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sat, 29 Dec 2012 11:52:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/29/196812.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196812.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/29/196812.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196812.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196812.html</trackback:ping><description><![CDATA[<p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">日期：2011.08.26</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">＊所有元素都负责显示自身。因此，有时会__<wbr>weak所在layer的指针。&nbsp;<br />@property (nonatomic, readwrite, assign) __weak CPTAnnotationHostLayer *annotationHostLayer;&nbsp;<br />＊即使自己重写函数，也适用了@synthesize关键字。&nbsp;<br />＊好像：<wbr>setNeedsLayout不但让该layer重新layou<wbr>t，而且让其各个父layer也重新layout。</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">* globalYRange会影响到YRange的改变。因此，<wbr>如果需要改变YRange，<wbr>则要先将globalYRange设置为nil。&nbsp;<br />pricePlotSpace.globalYRange = nil;&nbsp;<br />[pricePlotSpace scaleToFitPlots:graph.<wbr>allPlots];&nbsp;<br />pricePlotSpace.globalYRange = pricePlotSpace.yRange;</p><p style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">+++++</p><img src ="http://www.cppblog.com/walkklookk/aggbug/196812.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-29 19:52 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/29/196812.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>原：使用强指针，弱指针还是标记符</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/29/196811.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sat, 29 Dec 2012 11:51:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/29/196811.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196811.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/29/196811.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196811.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196811.html</trackback:ping><description><![CDATA[<span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">创建：Wednesday, November 30, 2011</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">两个强指针相互引用，会导致内存泄露。</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">一强一弱，如果强的先释放了，会导致弱指针成为悬空指针（<wbr>无效指针）。</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">通过记录标志符，<wbr>并且等需要的时候在向一个全局对象中动态查询获得。<wbr>这种方法虽然没有上述两个弊端，但是却相对比较耗时，<wbr>因为要涉及一个查询的过程。在某些需要大量使用的地方，<wbr>会带来不小的运行开销。</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">比较好的方法是使用智能指针，类似与C＋＋中的shared_<wbr>ptr weak_ptr，但是OC不支持，暂时没有找到替代类。</span><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;"><br /></span></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">+++++</span></div><img src ="http://www.cppblog.com/walkklookk/aggbug/196811.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-29 19:51 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/29/196811.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>转：让代码自动发送内存不足的消息</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/29/196810.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sat, 29 Dec 2012 11:51:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/29/196810.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196810.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/29/196810.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196810.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196810.html</trackback:ping><description><![CDATA[<span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">转：</span><a href="http://idevrecipes.com/2011/05/04/debugging-magic-auto-simulate-memory-warnings/" target="_blank" style="color: #1155cc; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">http://idevrecipes.com/2011/<wbr>05/04/debugging-magic-auto-<wbr>simulate-memory-warnings/</a><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><br /><div></div><div>日期：120103</div><div></div><div>［自：亲自测试：该消息只能在ios4上使用，<wbr>在ios5上则不会发送内存不足的消息。］</div><div>［自：这种方法能够完全测试内存不足情况下的问题，避免遗忘，<wbr>推荐。］</div><div></div><div><p style="margin: 0px 0px 1.5em; padding: 0px; border-width: 0px; outline: 0px; font-size: 1.167em; vertical-align: baseline; background-color: #494949; line-height: 1.5em; color: #ffffff; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Full Source code:&nbsp;<a href="https://gist.github.com/956403" target="_blank" style="color: #afd7f5; margin: 0px; padding: 0px; border-width: 0px; outline: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; text-decoration: initial;">https://gist.github.com/<wbr>956403</a></p><h2>Problem:</h2><p style="margin: 0px 0px 1.5em; padding: 0px; border-width: 0px; outline: 0px; font-size: 1.167em; vertical-align: baseline; background-color: #494949; line-height: 1.5em; color: #ffffff; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">You spend a lot of time and effort building your app, writing countless view controllers. You think it&#8217;s near perfect.</p><p style="margin: 0px 0px 1.5em; padding: 0px; border-width: 0px; outline: 0px; font-size: 1.167em; vertical-align: baseline; background-color: #494949; line-height: 1.5em; color: #ffffff; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Then one of your beta testers (or customers, or app reviewers) finds a problem. You look into it and realize that it only happens after a low memory pressure warning.</p><p style="margin: 0px 0px 1.5em; padding: 0px; border-width: 0px; outline: 0px; font-size: 1.167em; vertical-align: baseline; background-color: #494949; line-height: 1.5em; color: #ffffff; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">You should have written your viewDidLoad to handle getting called after a low memory pressure warning, but you didn&#8217;t.</p><p style="margin: 0px 0px 1.5em; padding: 0px; border-width: 0px; outline: 0px; font-size: 1.167em; vertical-align: baseline; background-color: #494949; line-height: 1.5em; color: #ffffff; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Wouldn&#8217;t it be great if you were forced to write your viewDidLoad implementation with low memory situations in mind? After all, out in the wild on real world devices, it&#8217;s very likely that sooner or later every single one of your view controllers will have to handle this.</p><h2>Solution:</h2><p style="margin: 0px 0px 1.5em; padding: 0px; border-width: 0px; outline: 0px; font-size: 1.167em; vertical-align: baseline; background-color: #494949; line-height: 1.5em; color: #ffffff; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Add&nbsp;<a href="https://gist.github.com/956403" target="_blank" style="color: #afd7f5; margin: 0px; padding: 0px; border-width: 0px; outline: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; text-decoration: initial;">this code</a>&nbsp;to your project and have your view controller inherit from BaseViewController instead of UIViewController. Every time viewWillAppear is called, the simulator will force a low memory warning:</p><p style="margin: 0px 0px 1.5em; padding: 0px; border-width: 0px; outline: 0px; font-size: 1.167em; vertical-align: baseline; background-color: #494949; line-height: 1.5em; color: #ffffff; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"></p><div style="background-color: #494949; width: 455px; color: #ffffff; margin: 1em 0px !important; border: 1px solid #e0e0e0 !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; direction: ltr !important;"><div style="margin: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><div style="margin: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; background-color: #ffffff !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><table style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; border-collapse: collapse !important; text-align: left !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><tbody style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><tr style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><td style="font-family: arial, sans-serif; margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: top !important; background-image: none !important; float: none !important; width: 3em !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important; color: #afafaf !important;"><code style="margin: 0px !important; padding: 0px 0.3em 0px 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; text-align: right !important; float: none !important; width: 2.7em !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; direction: ltr !important; display: block !important;">1</code></td><td style="font-family: arial, sans-serif; margin: 0px !important; padding: 0px 0px 0px 0.5em !important; border-width: 0px 0px 0px 3px !important; outline: 0px !important; vertical-align: top !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><code style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; direction: ltr !important; display: inline !important;">- (</code><code style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; direction: ltr !important; display: inline !important;">void</code><code style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; direction: ltr !important; display: inline !important;">)simulateMemoryWarning</code></td></tr></tbody></table></div><div style="margin: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; background-color: #f8f8f8 !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><table style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; border-collapse: collapse !important; text-align: left !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><tbody style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><tr style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><td style="font-family: arial, sans-serif; margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: top !important; background-image: none !important; float: none !important; width: 3em !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important; color: #afafaf !important;"><code style="margin: 0px !important; padding: 0px 0.3em 0px 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; text-align: right !important; float: none !important; width: 2.7em !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; direction: ltr !important; display: block !important;">2</code></td><td style="font-family: arial, sans-serif; margin: 0px !important; padding: 0px 0px 0px 0.5em !important; border-width: 0px 0px 0px 3px !important; outline: 0px !important; vertical-align: top !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><code style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; direction: ltr !important; display: inline !important;">{</code></td></tr></tbody></table></div><div style="margin: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; background-color: #ffffff !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><table style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; border-collapse: collapse !important; text-align: left !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><tbody style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><tr style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><td style="font-family: arial, sans-serif; margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: top !important; background-image: none !important; float: none !important; width: 3em !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important; color: #afafaf !important;"><code style="margin: 0px !important; padding: 0px 0.3em 0px 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; text-align: right !important; float: none !important; width: 2.7em !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; direction: ltr !important; display: block !important;">3</code></td><td style="font-family: arial, sans-serif; margin: 0px !important; padding: 0px 0px 0px 0.5em !important; border-width: 0px 0px 0px 3px !important; outline: 0px !important; vertical-align: top !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><code style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; direction: ltr !important; display: inline !important; color: gray !important;">#if TARGET_IPHONE_SIMULATOR</code></td></tr></tbody></table></div><div style="margin: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; background-color: #f8f8f8 !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><table style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; border-collapse: collapse !important; text-align: left !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><tbody style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><tr style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><td style="font-family: arial, sans-serif; margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: top !important; background-image: none !important; float: none !important; width: 3em !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important; color: #afafaf !important;"><code style="margin: 0px !important; padding: 0px 0.3em 0px 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; text-align: right !important; float: none !important; width: 2.7em !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; direction: ltr !important; display: block !important;">4</code></td><td style="font-family: arial, sans-serif; margin: 0px !important; padding: 0px 0px 0px 0.5em !important; border-width: 0px 0px 0px 3px !important; outline: 0px !important; vertical-align: top !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><code style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; direction: ltr !important; display: inline !important;">&nbsp;&nbsp;</code><code style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; direction: ltr !important; display: inline !important; color: gray !important;">#ifdef DEBUG</code></td></tr></tbody></table></div><div style="margin: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; background-color: #ffffff !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><table style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; border-collapse: collapse !important; text-align: left !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><tbody style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><tr style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><td style="font-family: arial, sans-serif; margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: top !important; background-image: none !important; float: none !important; width: 3em !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important; color: #afafaf !important;"><code style="margin: 0px !important; padding: 0px 0.3em 0px 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; text-align: right !important; float: none !important; width: 2.7em !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; direction: ltr !important; display: block !important;">5</code></td><td style="font-family: arial, sans-serif; margin: 0px !important; padding: 0px 0px 0px 0.5em !important; border-width: 0px 0px 0px 3px !important; outline: 0px !important; vertical-align: top !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><code style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; direction: ltr !important; display: inline !important;">&nbsp;&nbsp;&nbsp;&nbsp;</code><code style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; direction: ltr !important; display: inline !important;">CFNotificationCenterPostNo<wbr>tification(<wbr>CFNotificationCenterGetDarwinN<wbr>otifyCenter(), (CFStringRef)</code><code style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; direction: ltr !important; display: inline !important; color: blue !important;">@"<wbr>UISimulatedMemoryWarningNotifi<wbr>cation"</code><code style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; direction: ltr !important; display: inline !important;">,&nbsp;</code><code style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; font-weight: bold !important; min-height: inherit !important; direction: ltr !important; display: inline !important; color: #006699 !important;">NULL</code><code style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; direction: ltr !important; display: inline !important;">,&nbsp;</code><code style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; font-weight: bold !important; min-height: inherit !important; direction: ltr !important; display: inline !important; color: #006699 !important;">NULL</code><code style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; direction: ltr !important; display: inline !important;">,&nbsp;</code><code style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; font-weight: bold !important; min-height: inherit !important; direction: ltr !important; display: inline !important; color: #006699 !important;">true</code><code style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; direction: ltr !important; display: inline !important;">);</code></td></tr></tbody></table></div><div style="margin: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; background-color: #f8f8f8 !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><table style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; border-collapse: collapse !important; text-align: left !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><tbody style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><tr style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><td style="font-family: arial, sans-serif; margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: top !important; background-image: none !important; float: none !important; width: 3em !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important; color: #afafaf !important;"><code style="margin: 0px !important; padding: 0px 0.3em 0px 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; text-align: right !important; float: none !important; width: 2.7em !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; direction: ltr !important; display: block !important;">6</code></td><td style="font-family: arial, sans-serif; margin: 0px !important; padding: 0px 0px 0px 0.5em !important; border-width: 0px 0px 0px 3px !important; outline: 0px !important; vertical-align: top !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><code style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; direction: ltr !important; display: inline !important;">&nbsp;&nbsp;</code><code style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; direction: ltr !important; display: inline !important; color: gray !important;">#endif</code></td></tr></tbody></table></div><div style="margin: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; background-color: #ffffff !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><table style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; border-collapse: collapse !important; text-align: left !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><tbody style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><tr style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><td style="font-family: arial, sans-serif; margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: top !important; background-image: none !important; float: none !important; width: 3em !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important; color: #afafaf !important;"><code style="margin: 0px !important; padding: 0px 0.3em 0px 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; text-align: right !important; float: none !important; width: 2.7em !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; direction: ltr !important; display: block !important;">7</code></td><td style="font-family: arial, sans-serif; margin: 0px !important; padding: 0px 0px 0px 0.5em !important; border-width: 0px 0px 0px 3px !important; outline: 0px !important; vertical-align: top !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><code style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; direction: ltr !important; display: inline !important; color: gray !important;">#endif</code></td></tr></tbody></table></div><div style="margin: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; background-color: #f8f8f8 !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><table style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; border-collapse: collapse !important; text-align: left !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><tbody style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><tr style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><td style="font-family: arial, sans-serif; margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; vertical-align: top !important; background-image: none !important; float: none !important; width: 3em !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important; color: #afafaf !important;"><code style="margin: 0px !important; padding: 0px 0.3em 0px 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; text-align: right !important; float: none !important; width: 2.7em !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; direction: ltr !important; display: block !important;">8</code></td><td style="font-family: arial, sans-serif; margin: 0px !important; padding: 0px 0px 0px 0.5em !important; border-width: 0px 0px 0px 3px !important; outline: 0px !important; vertical-align: top !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; min-height: inherit !important; direction: ltr !important;"><code style="margin: 0px !important; padding: 0px !important; border-width: 0px !important; outline: 0px !important; font-size: 11px !important; vertical-align: baseline !important; background-image: none !important; float: none !important; width: auto !important; line-height: 1.3em !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; min-height: inherit !important; direction: ltr !important; display: inline !important;">}</code></td></tr></tbody></table></div></div></div><p style="margin: 0px 0px 1.5em; padding: 0px; border-width: 0px; outline: 0px; font-size: 1.167em; vertical-align: baseline; background-color: #494949; line-height: 1.5em; color: #ffffff; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;"></p><h3>Finding the Solution</h3><p style="margin: 0px 0px 1.5em; padding: 0px; border-width: 0px; outline: 0px; font-size: 1.167em; vertical-align: baseline; background-color: #494949; line-height: 1.5em; color: #ffffff; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">I found myself in this pattern one too many times. I finally decided to find a solution.</p><p style="margin: 0px 0px 1.5em; padding: 0px; border-width: 0px; outline: 0px; font-size: 1.167em; vertical-align: baseline; background-color: #494949; line-height: 1.5em; color: #ffffff; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">The iOS Simulator has the menu Hardware -&gt; Simulate Memory Warning</p><p style="margin: 0px 0px 1.5em; padding: 0px; border-width: 0px; outline: 0px; font-size: 1.167em; vertical-align: baseline; background-color: #494949; line-height: 1.5em; color: #ffffff; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">But I wasn&#8217;t going to select that menu item every time you test to see if something works.</p><p style="margin: 0px 0px 1.5em; padding: 0px; border-width: 0px; outline: 0px; font-size: 1.167em; vertical-align: baseline; background-color: #494949; line-height: 1.5em; color: #ffffff; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">I embarked on figuring out how this menu item works.</p><p style="margin: 0px 0px 1.5em; padding: 0px; border-width: 0px; outline: 0px; font-size: 1.167em; vertical-align: baseline; background-color: #494949; line-height: 1.5em; color: #ffffff; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">I looked through the UIKit framework binary (/Developer/Platforms/<wbr>iPhoneSimulator.platform/<wbr>Developer/SDKs/<wbr>iPhoneSimulator4.3.sdk/System/<wbr>Library/Frameworks/UIKit.<wbr>framework/UIKit) searching for words like memory and warning.</p><p style="margin: 0px 0px 1.5em; padding: 0px; border-width: 0px; outline: 0px; font-size: 1.167em; vertical-align: baseline; background-color: #494949; line-height: 1.5em; color: #ffffff; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">I put breakpoints in gdb trying to find what message is sent. I read the NSNotification docs. After many dead ends I discovered the name of the notification: UISimulatedMemoryWarningNotifi<wbr>cation and that I needed to use CFNotificationCenter to send the message.</p><p style="margin: 0px 0px 1.5em; padding: 0px; border-width: 0px; outline: 0px; font-size: 1.167em; vertical-align: baseline; background-color: #494949; line-height: 1.5em; color: #ffffff; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">I&#8217;ve tried this out on a couple of projects and it has forced me to think about low memory situations from day one.</p><p style="margin: 0px 0px 1.5em; padding: 0px; border-width: 0px; outline: 0px; font-size: 1.167em; vertical-align: baseline; background-color: #494949; line-height: 1.5em; color: #ffffff; font-family: 'Helvetica Neue', Arial, Helvetica, sans-serif;">Full Source code:&nbsp;<a href="https://gist.github.com/956403" target="_blank" style="color: #afd7f5; margin: 0px; padding: 0px; border-width: 0px; outline: 0px; font-size: 14px; vertical-align: baseline; background-color: transparent; text-decoration: initial;">https://gist.github.com/<wbr>956403</a></p></div><div></div><div>＋＋＋＋＋</div></div><img src ="http://www.cppblog.com/walkklookk/aggbug/196810.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-29 19:51 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/29/196810.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>转：关于ARC的一些规则</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/29/196794.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sat, 29 Dec 2012 03:12:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/29/196794.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196794.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/29/196794.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196794.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196794.html</trackback:ping><description><![CDATA[<span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">创建：2012.02.27</span><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><br /><div>主要内容来自：</div><div><a href="http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1" target="_blank" style="color: #1155cc;">http://www.raywenderlich.com/<wbr>5677/beginning-arc-in-ios-5-<wbr>part-1</a></div><div>还有部分内容来自Cookbook，<wbr>但是cookbook介绍的太过简略很深奥，不易掌握，<wbr>因此没有全部摘录。以后还要在细看看。</div><div></div><div></div><div><div></div><div>Automatic Reference Counting, or ARC for short</div><div></div><div>＋＋＋</div><div>ARC is a feature of the new LLVM 3.0 compiler. Your existing projects most likely use the older GCC 4.2 or LLVM-GCC compilers</div><div></div><div>＋＋＋</div><div>It is important to realize that ARC is a feature of the Objective-C compiler and therefore all the ARC stuff happens when you build your app. ARC is not a runtime feature (except for one small part, the weak pointer system), nor is it *garbage collection* that you may know from other languages.&nbsp;</div><div></div><div>＋＋＋</div><div>All that ARC does is insert retains and releases into your code when it compiles it, exactly where you would have put them yourself. That makes ARC just as fast as manually managed code, and sometimes even a bit faster because it can perform certain optimizations under the hood.</div><div></div><div>When the pointer gets a new value or ceases to exist, the associated object is released.</div><div></div><div>no longer call retain, release and autorelease and retainCount.</div><div></div><div>＋＋＋</div><div>__strong，__weak，__unsafe_<wbr>unretained，__autoreleasing</div><div></div><div>默认情况下，所有的instance variables and local variables都是strong指针。</div><div></div><div>__autoreleasing&#8212;These variables are used with object pointer arguments (id *), which are autoreleased on return.The NSError class offers the most common use case for these variables&#8212;for example,int result = doSomething(arg, arg, arg, &amp;error);. Convenience methods also return autoreleased objects.</div><div></div><div>＋＋＋</div><div>对象被释放后，其上的weak指针将自动变为nil。</div><div></div><div>＋＋＋</div><div>下面的代码是无效的：</div><div>__weak NSString *str = [[NSString alloc] initWithFormat:...];</div><div>NSLog(@"%@", str); &nbsp;// will output "(null)"</div><div>字符串对象没有拥有者（因为str是weak</div><div>指针），并且这个字符串对象将在被创建之后立刻被释放掉了。<wbr>XCode将给你一个警告（&#8220;Warning: assigning retained object to weak variable; object will be released after assignment&#8221;）</div><div></div><div>＋＋＋</div><div>Weak is the recommended relationship for all *outlet* properties.&nbsp;</div><div></div><div>如果将IBOutlet的属性设置为weak，<wbr>那么在进入到viewDidUnload之前，它们就会被释放，<wbr>并设置为nil</div><div>。<wbr>所以在viewDidUnload将不能使用这些IBOutle<wbr>t了。</div><div></div><div>＋＋＋</div><div>With ARC, all strong, weak, and autoreleasing stack variables are automatically initialized with nil.</div><div></div><div>＋＋＋</div><div>NSError __autoreleasing *error;</div><div>// Retrieve the icon's file attributes&nbsp;</div><div>NSString *iconLocation = [[NSBundle mainBundle] pathForResource:@"icon" ofType:@"png"];</div><div>NSDictionary *dictionary = [[NSFileManager defaultManager] attributesOfItemAtPath:<wbr>iconLocation error:&amp;error];</div><div>if (!dictionary)&nbsp;</div><div><span style="white-space: pre-wrap;">	</span>NSLog(@"Could not get attribute information: %@", error);</div><div></div><div>＋＋＋</div><div>ARC continues to use assign qualifiers for non-object values such as int and CGRect.</div><div></div><div>＋＋＋</div><div>These property declarations change from this,</div><div>@property (retain, nonatomic)</div><div>to this:</div><div>@property (strong, nonatomic)</div><div></div><div>Under ARC you are not allowed to call release, nor [super dealloc].</div><div></div><div>+++</div><div>By the way, in your dealloc method you can still use your instance variables because they haven&#8217;t been released yet at that point. That doesn&#8217;t happen until after dealloc returns.</div><div></div><div>+++</div><div>As a best practice, if you define something as a property, then you should always use it as a property. The only places where you should access the property&#8217;s backing instance variable directly are in init and when you provide a custom getter and setter.</div><div></div><div></div><div>+++</div><div>@property (nonatomic, readonly) NSString *result;</div><div>You must explicitly state whether you want this property to be strong, weak or unsafe_unretained. Most of the time, strong is the proper answer:</div><div></div><div>@property (nonatomic, strong, readonly) NSString *result;</div><div></div><div>＋＋＋</div><div>Autorelease didn&#8217;t go away with ARC, even though you never directly call the [autorelease] method on objects anymore. Any time you return an object from a method whose name doesn&#8217;t start with alloc, init, copy, mutableCopy or new, the ARC compiler will autorelease it for you. These objects still end up in an autorelease pool.&nbsp;</div><div>Control ARC&#8217;s return behavior by declaring NS_RETURNS_RETAINED and NS_RETURNS_NOT_RETAINED. For example,</div><div>- (NSString *) copyName NS_RETURNS_NOT_RETAINED;</div><div>returns an autoreleased object to non-ARC code, even though the name starts with the phrase &#8220;copy.&#8221;</div><div></div><div></div><div>The big difference with before is that the NSAutoreleasePool has been retired in favor of a new language construct, @autoreleasepool.</div><div>The conversion tool turned our main() function from this,</div><div></div><div>NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];</div><div>int retVal = UIApplicationMain(argc, argv, nil,&nbsp;</div><div>&nbsp; NSStringFromClass([AppDelegate class]));</div><div>[pool release];</div><div>return retVal;</div><div></div><div>into this:</div><div></div><div>@autoreleasepool {</div><div>&nbsp; int retVal = UIApplicationMain(argc, argv, nil,&nbsp;</div><div>&nbsp; &nbsp; NSStringFromClass([AppDelegate class]));</div><div>&nbsp; &nbsp; return retVal;</div><div>}</div><div></div><div>＋＋＋</div><div>You may have some code that creates its own autorelease pool:</div><div></div><div>NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];</div><div>&nbsp;</div><div>// . . . do calculations . . .</div><div>&nbsp;</div><div>NSArray* sortedResults =&nbsp;</div><div>&nbsp; [[filteredResults sortedArrayUsingSelector:@<wbr>selector(compare:)]&nbsp;</div><div>&nbsp; &nbsp; retain];</div><div>&nbsp;</div><div>[pool release];</div><div>return [sortedResults autorelease];</div><div></div><div>The conversion tool needs to turn that into something like this:</div><div></div><div>NSArray* sortedResults = nil;</div><div>@autoreleasepool</div><div>{</div><div>&nbsp; // . . . do calculations . . . &nbsp; &nbsp; &nbsp;&nbsp;</div><div>&nbsp; sortedResults = [filteredResults sortedArrayUsingSelector:@</div><div>&nbsp; &nbsp; selector(compare:)];<span style="white-space: pre-wrap;">	</span></div><div>}</div><div>return sortedResults;</div><div></div><div>＋＋＋</div><div>&#8220;Switch case is in protected scope&#8221;</div><div>You get this error when your code does the following:</div><div>switch (X)</div><div>{<span style="white-space: pre-wrap;">	</span></div><div>&nbsp; &nbsp; case Y:</div><div>&nbsp; &nbsp; &nbsp; &nbsp; NSString *s = ...;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; break;</div><div>}</div><div>This is no longer allowed. If you declare new pointer variables inside a case statement you must enclose the whole thing in curlies:</div><div>switch (X)</div><div>{<span style="white-space: pre-wrap;">	</span></div><div>&nbsp; &nbsp; case Y:</div><div>&nbsp; &nbsp; {</div><div>&nbsp; &nbsp; &nbsp; &nbsp; NSString *s = ...;</div><div>&nbsp; &nbsp; &nbsp; &nbsp; break;</div><div>&nbsp; &nbsp; }</div><div>}</div><div>Now it is clear what the scope of the variable is, which ARC needs to know in order to release the object at the right moment.</div><div></div><div>＋＋＋</div><div>One of the restrictions of ARC is that you can no longer put Objective-C objects inside C structs. The following code is no longer valid:</div><div></div><div>typedef struct</div><div>{</div><div>&nbsp; &nbsp; UIImage *selectedImage;</div><div>&nbsp; &nbsp; UIImage *disabledImage;</div><div>}</div><div>ButtonImages;&nbsp;</div><div></div><div>＋＋＋</div><div>Automatic Reference Counting also has a few limitations. For starters, ARC only works on Objective-C objects. If your app uses Core Foundation or malloc() and free(), then you&#8217;re still responsible for doing the memory management there.</div><div></div><div>＋＋＋</div><div></div><div>&nbsp; &nbsp; * __bridge_transfer: Give ARC ownership</div><div>&nbsp; &nbsp; * __bridge_retained: Relieve ARC of its ownership</div><div>&nbsp; &nbsp; * __bridge：<wbr>ARC对于此对象的内存的态度保持原来的状态。<wbr>即如果ARC原来是持有该对象的，那么仍然持有。<wbr>如果ARC原来是不持有该对象的，那么现在仍然不持有。</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; 也可以使用CFBridgingRelease()来替代__<wbr>bridge_transfer，它更清晰一些。</div><div>&nbsp; &nbsp;&nbsp;</div><div>&nbsp; &nbsp; CFBridgingRetain来替代__bridge_<wbr>retained。即：</div><div>NSString *s1 = [[NSString alloc] initWithFormat:@"Hello, %@!", name];</div><div>CFStringRef s2 = (__bridge_retained CFStringRef)s1;</div><div>// do something with s2</div><div>// . . .</div><div>CFRelease(s2);</div><div><span style="white-space: pre-wrap;">	</span>或者：</div><div>CFStringRef s2 = CFBridgingRetain(s1);</div><div>// . . .</div><div>CFRelease(s2);</div><div></div><div>＋＋＋</div><div>Note: Not all Objective-C and Core Foundation objects that sound alike are toll-free bridged. For example, CGImage and UIImage cannot be cast to each another, and neither can CGColor and UIColor. This page lists the types that can be used interchangeably.</div><div></div><div>＋＋＋</div><div>To summarize:</div><div></div><div>&nbsp; &nbsp; * When changing ownership from Core Foundation to Objective-C you use CFBridgingRelease().</div><div>&nbsp; &nbsp; * When changing ownership from Objective-C to Core Foundation you use CFBridgingRetain().</div><div>&nbsp; &nbsp; * When you want to use one type temporarily as if it were another without ownership change, you use __bridge.</div><div>&nbsp; &nbsp;&nbsp;</div><div>＋＋＋</div><div>ARC even combines well with C++. With a few restrictions you can also use ARC on iOS 4, which should only help to speed up the adoption. &nbsp;&nbsp;</div><div></div><div>If you must deploy to iOS 4.x, use __unsafe_unretained qualifiers instead of weak.This is functionally equivalent to old- style assign properties and will work on old-style OS runtimes</div><div></div><div></div><div>+++</div><div>Never use NSAllocateObject or NSDeallocateObject.</div><div></div><div>+++</div><div>You must assign the result of [super init], typically as self = [super init]. You probably already do this, as well as checking to see whether self is nil. Just keep doing that.</div><div></div><div>＋＋＋</div><div>常见的内存错误：</div><div>id obj = [array objectAtIndex:0];</div><div>[array removeObjectAtIndex:0];</div><div>NSLog(@"%@", obj);</div><div></div><div></div></div><div>＋＋＋＋＋</div></div><img src ="http://www.cppblog.com/walkklookk/aggbug/196794.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-29 11:12 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/29/196794.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>原：对象在释放的时候，一定要将引用自身为的delegate对象设置成nil</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/29/196791.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sat, 29 Dec 2012 03:10:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/29/196791.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196791.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/29/196791.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196791.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196791.html</trackback:ping><description><![CDATA[<span style="color: #222222; background-color: #ffffff;">创建：2011.11.27</span><br style="color: #222222; background-color: #ffffff;" /><br style="color: #222222; background-color: #ffffff;" /><span style="color: #222222; background-color: #ffffff;">例如，如果A是B的delegate。<wbr>那么在A的dealloc里面一定要设置 b.delegate = nil;然后在释放b。即</span><br style="color: #222222; background-color: #ffffff;" /><span style="color: #222222; background-color: #ffffff;">- (void)dealloc {</span><br style="color: #222222; background-color: #ffffff;" /><span style="color: #222222; background-color: #ffffff;">&nbsp; &nbsp;b.delegate = nil;</span><br style="color: #222222; background-color: #ffffff;" /><span style="color: #222222; background-color: #ffffff;">&nbsp; &nbsp;[b release];</span><br style="color: #222222; background-color: #ffffff;" /><span style="color: #222222; background-color: #ffffff;">&nbsp; &nbsp;......</span><br style="color: #222222; background-color: #ffffff;" /><span style="color: #222222; background-color: #ffffff;">}</span><br style="color: #222222; background-color: #ffffff;" /><br style="color: #222222; background-color: #ffffff;" /><span style="color: #222222; background-color: #ffffff;">虽然在大多数情况下，[b release]都会导致b直接被释放了，<wbr>因此是否将b的delegate设置为nil都不影响结果。</span><br style="color: #222222; background-color: #ffffff;" /><span style="color: #222222; background-color: #ffffff;">但是，随着程序的变化，难保某一天A被释放之后，B没有被释放，<wbr>那么当b在调用delegate的函数的时候，<wbr>可能就出现crash了。</span><br style="color: #222222; background-color: #ffffff;" /><br style="color: #222222; background-color: #ffffff;" /><span style="color: #222222; background-color: #ffffff;">今天我就遇到了这样的情况，<wbr>因为我执行了一个NSNotification，<wbr>其中b作为参数传入了。此时b就被另外保存了，<wbr>进而引起了bug。</span><br style="color: #222222; background-color: #ffffff;" /><br style="color: #222222; background-color: #ffffff;" /><span style="color: #222222; background-color: #ffffff;">＋＋＋＋＋</span><img src ="http://www.cppblog.com/walkklookk/aggbug/196791.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-29 11:10 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/29/196791.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>原：设备24小时制，NSDateFormatter欲12小时制</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/29/196790.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sat, 29 Dec 2012 03:09:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/29/196790.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196790.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/29/196790.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196790.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196790.html</trackback:ping><description><![CDATA[<span style="color: #222222; background-color: #ffffff;">日期：2011.10.30</span><br style="color: #222222; background-color: #ffffff;" /><br style="color: #222222; background-color: #ffffff;" /><span style="color: #222222; background-color: #ffffff;">当使用NSDateFormatter进行12小时制格式化时，<wbr>如果设备被调整为24小时制，</span><span style="color: #222222; line-height: normal; background-color: #ffffff; font-family: 'Lucida Grande', Geneva, Helvetica, Arial, sans-serif;">(via Settings &gt; General &gt; Date &amp; Time &gt; 24-Hour Time)，此时格式化将失败，无法出现PM，AM字样，<wbr>并且时间也是24小时的，此时可以设定</span><span style="color: #222222; background-color: #ffffff;">Locale为en_<wbr>US_POSIX。例如：</span><br style="color: #222222; background-color: #ffffff;" /><br style="color: #222222; background-color: #ffffff;" /><div style="color: #222222; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; width: 1560px; word-break: break-all;">-&nbsp;(NSDate&nbsp;*)geDateFromString:(<wbr>NSString&nbsp;*)s&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;NSDateFormatter&nbsp;*f&nbsp;=&nbsp;[[[<wbr>NSDateFormatter&nbsp;alloc]&nbsp;init]&nbsp;<wbr>autorelease];<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;[f&nbsp;setLocale:[[[NSLocale&nbsp;<wbr>alloc]&nbsp;<wbr>initWithLocaleIdentifier:@"en_<wbr>US_POSIX"]&nbsp;autorelease]];&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;[f&nbsp;setTimeZone:[<wbr>NSTimeZone&nbsp;timeZoneWithName:@"<wbr>America/New_York"]];<br />&nbsp;&nbsp;&nbsp;&nbsp;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;[f&nbsp;setDateFormat:@"MM/dd/<wbr>yyyy&nbsp;hh:mm:ssa"];<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span>&nbsp;[f&nbsp;dateFromString:<wbr>s];<br />}</div><br style="color: #222222; background-color: #ffffff;" /><br style="color: #222222; background-color: #ffffff;" /><br style="color: #222222; background-color: #ffffff;" /><span style="color: #222222; background-color: #ffffff;">＋＋＋＋＋</span><img src ="http://www.cppblog.com/walkklookk/aggbug/196790.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-29 11:09 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/29/196790.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>转：查看iphone，ipad固件版本</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/29/196789.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sat, 29 Dec 2012 03:09:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/29/196789.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196789.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/29/196789.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196789.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196789.html</trackback:ping><description><![CDATA[<span style="color: #222222; background-color: #ffffff;">日期：2011.10.30</span><br style="color: #222222; background-color: #ffffff;" /><span style="color: #222222; background-color: #ffffff;">转：<a href="http://stackoverflow.com/questions/3177634/iphone-how-do-i-detect-the-iphone-version" target="_blank" style="color: #1155cc;">http://stackoverflow.com/<wbr>questions/3177634/iphone-how-<wbr>do-i-detect-the-iphone-version</a></span><br style="color: #222222; background-color: #ffffff;" /><span style="color: #222222; background-color: #ffffff;">转：<a href="http://stackoverflow.com/questions/448162/determine-device-iphone-ipod-touch-with-iphone-sdk" target="_blank" style="color: #1155cc;">http://stackoverflow.com/<wbr>questions/448162/determine-<wbr>device-iphone-ipod-touch-with-<wbr>iphone-sdk</a></span><br style="color: #222222; background-color: #ffffff;" /><br style="color: #222222; background-color: #ffffff;" /><br style="color: #222222; background-color: #ffffff;" /><br style="color: #222222; background-color: #ffffff;" /><div style="color: #222222; background-color: #ffffff;"><div>feel free to use this class - I found it here</div><div></div><div>Usage</div><div></div><div><div style="padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; width: 1560px; word-break: break-all;">UIDeviceHardware&nbsp;*h=[[<wbr>UIDeviceHardware&nbsp;alloc]&nbsp;init];<br />[self&nbsp;setDeviceModel:[h&nbsp;<wbr>platformString]];&nbsp;&nbsp;&nbsp;<br />[h&nbsp;release];</div></div><div></div><div>UIDeviceHardware.h</div><div></div><div><br /><div style="padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; width: 1560px; word-break: break-all;"><span style="color: #008000;">//</span><span style="color: #008000;"><br /></span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;&nbsp;UIDeviceHardware.h<br /></span><span style="color: #008000;">//</span><span style="color: #008000;"><br /></span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;&nbsp;Used&nbsp;to&nbsp;determine&nbsp;EXACT&nbsp;<wbr>version&nbsp;of&nbsp;device&nbsp;software&nbsp;is&nbsp;<wbr>running&nbsp;on.</span><span style="color: #008000;"><br /></span><br />#import&nbsp;&lt;Foundation/<wbr>Foundation.h&gt;<br /><br />@interface&nbsp;UIDeviceHardware&nbsp;:&nbsp;<wbr>NSObject&nbsp;<br /><br />-&nbsp;(NSString&nbsp;*)&nbsp;platform;<br />-&nbsp;(NSString&nbsp;*)&nbsp;platformString;<br /><br />@end</div></div><div>UIDeviceHardware.m</div><div></div><div><div style="padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; border: 1px solid #cccccc; width: 1560px; word-break: break-all;"><span style="color: #008000;">//</span><span style="color: #008000;"><br /></span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;&nbsp;UIDeviceHardware.m<br /></span><span style="color: #008000;">//</span><span style="color: #008000;"><br /></span><span style="color: #008000;">//</span><span style="color: #008000;">&nbsp;&nbsp;Used&nbsp;to&nbsp;determine&nbsp;EXACT&nbsp;<wbr>version&nbsp;of&nbsp;device&nbsp;software&nbsp;is&nbsp;<wbr>running&nbsp;on.</span><span style="color: #008000;"><br /></span><br />#import&nbsp;"UIDeviceHardware.h"<br />#include&nbsp;&lt;sys/types.h&gt;<br />#include&nbsp;&lt;sys/sysctl.h&gt;<br /><br />@implementation&nbsp;<wbr>UIDeviceHardware<br /><br />-&nbsp;(NSString&nbsp;*)&nbsp;platform{<br />&nbsp;&nbsp;&nbsp;&nbsp;size_t&nbsp;size;<br />&nbsp;&nbsp;&nbsp;&nbsp;sysctlbyname("hw.machine",<wbr>&nbsp;NULL,&nbsp;&amp;size,&nbsp;NULL,&nbsp;0);<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">char</span>&nbsp;*machine&nbsp;=&nbsp;malloc(<wbr>size);<br />&nbsp;&nbsp;&nbsp;&nbsp;sysctlbyname("hw.machine",<wbr>&nbsp;machine,&nbsp;&amp;size,&nbsp;NULL,&nbsp;0);<br />&nbsp;&nbsp;&nbsp;&nbsp;NSString&nbsp;*platform&nbsp;=&nbsp;[<wbr>NSString&nbsp;stringWithCString:<wbr>machine];<br />&nbsp;&nbsp;&nbsp;&nbsp;free(machine);<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span>&nbsp;platform;<br />}<br /><br />-&nbsp;(NSString&nbsp;*)&nbsp;platformString{<br />&nbsp;&nbsp;&nbsp;&nbsp;NSString&nbsp;*platform&nbsp;=&nbsp;[<wbr>self&nbsp;platform];<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span>&nbsp;([platform&nbsp;<wbr>isEqualToString:@"iPhone1,1"])<wbr>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span>&nbsp;@"iPhone&nbsp;1G";<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span>&nbsp;([platform&nbsp;<wbr>isEqualToString:@"iPhone1,2"])<wbr>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span>&nbsp;@"iPhone&nbsp;3G";<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span>&nbsp;([platform&nbsp;<wbr>isEqualToString:@"iPhone2,1"])<wbr>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span>&nbsp;@"iPhone&nbsp;3GS";<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span>&nbsp;([platform&nbsp;<wbr>isEqualToString:@"iPhone3,1"])<wbr>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span>&nbsp;@"iPhone&nbsp;4";<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span>&nbsp;([platform&nbsp;<wbr>isEqualToString:@"iPod1,1"])&nbsp;&nbsp;<wbr>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span>&nbsp;@"iPod&nbsp;Touch&nbsp;1G";<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span>&nbsp;([platform&nbsp;<wbr>isEqualToString:@"iPod2,1"])&nbsp;&nbsp;<wbr>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span>&nbsp;@"iPod&nbsp;Touch&nbsp;2G";<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span>&nbsp;([platform&nbsp;<wbr>isEqualToString:@"iPod3,1"])&nbsp;&nbsp;<wbr>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span>&nbsp;@"iPod&nbsp;Touch&nbsp;3G";<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span>&nbsp;([platform&nbsp;<wbr>isEqualToString:@"iPod4,1"])&nbsp;&nbsp;<wbr>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span>&nbsp;@"iPod&nbsp;Touch&nbsp;4G";<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span>&nbsp;([platform&nbsp;<wbr>isEqualToString:@"iPad1,1"])&nbsp;&nbsp;<wbr>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span>&nbsp;@"iPad";<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span>&nbsp;([platform&nbsp;<wbr>isEqualToString:@"iPad2,1"])&nbsp;&nbsp;<wbr>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span>&nbsp;@"iPad&nbsp;2&nbsp;(WiFi)";<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span>&nbsp;([platform&nbsp;<wbr>isEqualToString:@"iPad2,2"])&nbsp;&nbsp;<wbr>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span>&nbsp;@"iPad&nbsp;2&nbsp;(GSM)";<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span>&nbsp;([platform&nbsp;<wbr>isEqualToString:@"iPad2,3"])&nbsp;&nbsp;<wbr>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span>&nbsp;@"iPad&nbsp;2&nbsp;(CDMA)";<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">if</span>&nbsp;([platform&nbsp;<wbr>isEqualToString:@"i386"])&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<wbr>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span>&nbsp;@"Simulator";<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000ff;">return</span>&nbsp;platform;<br />}<br /><br />@end</div></div><div><br /><br /><br />＋＋＋＋＋</div></div><img src ="http://www.cppblog.com/walkklookk/aggbug/196789.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-29 11:09 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/29/196789.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>转：格式化字符串说明，以printf为例，同样适用于scanf类组</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/29/196787.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sat, 29 Dec 2012 03:08:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/29/196787.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196787.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/29/196787.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196787.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196787.html</trackback:ping><description><![CDATA[<div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">创建：2011.10.24</div><br style="color: #222222; background-color: #ffffff;" /><span style="color: #222222; background-color: #ffffff;">转：<a href="http://blog.chinaunix.net/space.php?uid=25272299&amp;do=blog&amp;id=2954172" target="_blank" style="color: #1155cc;">http://blog.chinaunix.net/<wbr>space.php?uid=25272299&amp;do=<wbr>blog&amp;id=2954172</a></span><br style="color: #222222; background-color: #ffffff;" /><br style="color: #222222; background-color: #ffffff;" /><br style="color: #222222; background-color: #ffffff;" /><div style="color: #222222; background-color: #ffffff;"><div>printf()<wbr>会根据参数format字符串来转换并格式化数据，<wbr>然后将结果写出到标准输出设备，直到出现字符串结束('\0')<wbr>为止。参数format字符串可包含下列三种字符类型</div><div>1.一般文本，伴随直接输出。</div><div>2.ASCII控制字符，如\t、\n等。</div><div>3.格式转换字符。</div><div>格式转换为一个百分比符号(％)及其后的格式字符所组成。<wbr>一般而言，每个％符号在其后都必需有一printf()<wbr>的参数与之相呼应（只有当％％转换字符出现时会直接输出％字符）<wbr>，而欲输出的数据类型必须与其相对应的转换字符类型相同。</div><div>Printf()格式转换的一般形式如下</div><div><span style="background-color: #ff99cc;">％(flags)(width)(.prec)type</span></div><div>以中括号括起来的参数为选择性参数，而％与type则是必要的。<wbr>底下先介绍type的几种形式</div><div><ul><li style="margin-left: 15px;">整数</li></ul></div><div>％d 整数的参数会被转成一有符号的十进制数字</div><div>％u 整数的参数会被转成一无符号的十进制数字</div><div>％o 整数的参数会被转成一无符号的八进制数字</div><div>％x 整数的参数会被转成一无符号的十六进制数字，<wbr>并以小写abcdef表示</div><div>％X 整数的参数会被转成一无符号的十六进制数字，<wbr>并以大写ABCDEF表示浮点型数</div><div>％f double 型的参数会被转成十进制数字，并取到小数点以下六位，四舍五入。</div><div>％e double型的参数以指数形式打印，有一个数字会在小数点前，<wbr>六位数字在小数点后，而在指数部分会以小写的e来表示。</div><div>％E 与％e作用相同，唯一区别是指数部分将以大写的E 来表示。</div><div>％g double 型的参数会自动选择以％f 或％e 的格式来打印，<wbr>其标准是根据欲打印的数值及所设置的有效位数来决定。</div><div>％G 与％g 作用相同，唯一区别在以指数形态打印时会选择％E 格式。</div><div><ul><li style="margin-left: 15px;">字符及字符串</li></ul></div><div>％c 整型数的参数会被转成unsigned char型打印出。</div><div>％s 指向字符串的参数会被逐字输出，直到出现NULL字符为止</div><div>％p 如果是参数是&#8220;void *&#8221;型指针则使用十六进制格式显示。</div><div><ul><li style="margin-left: 15px;">prec 有几种情况</li></ul></div><div>1.正整数的最小位数。</div><div>2.在浮点型数中代表小数位数</div><div>3.在％g 格式代表有效位数的最大值。</div><div>4.在％s格式代表字符串的最大长度。</div><div>5.若为&#215;符号则代表下个参数值为最大长度。<br /><br /><div><ul><li style="margin-left: 15px;">width</li></ul></div></div><div>width为参数的最小长度，若此栏并非数值，而是*符号，<wbr>则表示以下一个参数当做参数长度。</div><div><ul><li style="margin-left: 15px;">flags 有下列几种情况</li></ul></div><div>#NAME?</div><div>+ 一般在打印负数时，printf（）会加印一个负号，<wbr>整数则不加任何负号。此旗标会使得在打印正数前多一个正号（+）<wbr>。</div><div># 此旗标会根据其后转换字符的不同而有不同含义。当在类型为o 之前（如％#o），则会在打印八进制数值前多印一个o。</div><div>而在类型为x 之前（％#x）则会在打印十六进制数前多印&#8217;0x&#8217;，<wbr>在型态为e、E、f、g或G 之前则会强迫数值打印小数点。在类型为g 或G之前时则同时保留小数点及小数位数末尾的零。</div><div>0 当有指定参数时，无数字的参数将补上0。默认是关闭此旗标，<wbr>所以一般会打印出空白字符。</div><div>&nbsp;</div><div><ul><li style="margin-left: 15px;">返回值 &nbsp;</li></ul>成功则返回实际输出的字符数，失败则返回-1，<wbr>错误原因存于errno中。</div><div>&nbsp;</div><div><ul><li style="margin-left: 15px;">范例 &nbsp;</li></ul>#include&lt;stdio.h&gt;</div><div>main()</div><div>{</div><div>int i = 150;</div><div>int j = -100;</div><div>double k = 3.14159;</div><div>printf(&#8220;%d %f %x\n&#8221;,j,k,i);</div><div>printf(&#8220;%2d %*d\n&#8221;,i,2,i); /*参数2 会代入格式*中，而与%2d同意义*/</div><div>}</div><div>&nbsp;</div><div>执行 &nbsp;-100 3.14159 96</div><div>150 150</div><div>&nbsp;</div><div>&nbsp;</div><div>1．转换说明符</div><div>&nbsp; &nbsp; &nbsp; %a(%A) &nbsp; &nbsp; 浮点数、十六进制数字和p-(P-)记数法(C99)</div><div>&nbsp; &nbsp; &nbsp; %c &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 字符</div><div>&nbsp; &nbsp; &nbsp; %d &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 有符号十进制整数</div><div>&nbsp; &nbsp; &nbsp; %f &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;浮点数(包括float和doulbe)</div><div>&nbsp; &nbsp; &nbsp; %e(%E) &nbsp; &nbsp; 浮点数指数输出[e-(E-)记数法]</div><div>&nbsp; &nbsp; &nbsp; %g(%G) &nbsp; &nbsp; 浮点数不显无意义的零"0"</div><div>&nbsp; &nbsp; &nbsp; %i &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;有符号十进制整数(与%d相同)</div><div>&nbsp; &nbsp; &nbsp; %u &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 无符号十进制整数</div><div>&nbsp; &nbsp; &nbsp; %o &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 八进制整数 &nbsp; &nbsp;e.g. &nbsp; &nbsp; 0123</div><div>&nbsp; &nbsp; &nbsp; %x(%X) &nbsp; &nbsp; &nbsp;十六进制整数0f(0F) &nbsp; e.g. &nbsp; 0x1234</div><div>&nbsp; &nbsp; &nbsp; %p &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 指针</div><div>&nbsp; &nbsp; &nbsp; %s &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 字符串</div><div>&nbsp; &nbsp; &nbsp; %% &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"%"</div><div>2．标志</div><div>&nbsp; &nbsp; &nbsp; 左对齐："-" &nbsp; e.g. &nbsp; "%-20s"</div><div>&nbsp; &nbsp; &nbsp; 右对齐："+" e.g. &nbsp; "%+20s"</div><div>&nbsp; &nbsp; &nbsp; 空格：若符号为正，则显示空格，负则显示"-" &nbsp; e.g. &nbsp; "% 6.2f" &nbsp; &nbsp; &nbsp;</div><div>&nbsp; &nbsp; &nbsp; #：对c,s,d,u类无影响；对o类，在输出时加前缀o；<wbr>对x类，在输出时加前缀0x；</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;对e,g,f 类当结果有小数时才给出小数点。</div><div>3．格式字符串（格式）</div><div>&nbsp; &nbsp; &nbsp; ［标志］［输出最少宽度］［．精度］［长度］类型&nbsp;</div><div>&nbsp; &nbsp; &nbsp;"％-md" ：左对齐，若m比实际少时，按实际输出。</div><div>&nbsp; &nbsp; &nbsp;"%m.ns"：输出m位，取字符串(左起)n位，左补空格，<wbr>当n&gt;m or m省略时m=n</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.g. &nbsp; &nbsp;"%7.2s" &nbsp; 输入CHINA</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;　 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 输出" &nbsp; &nbsp; CH"</div><div>&nbsp; &nbsp; &nbsp;"%<a href="http://m.nf/" target="_blank" style="color: #1155cc;">m.nf</a>"：输出浮点数，m为宽度，n为小数点右边数位</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.g. &nbsp; &nbsp;"%3.1f" &nbsp; &nbsp;输入3852.99</div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;输出3853.0&nbsp;</div><div>&nbsp; &nbsp; &nbsp; 长度：为ｈ短整形量,ｌ为长整形量</div><div><br />printf的格式控制的完整格式：</div><div>% - 0 m.n l或h 格式字符</div><div>下面对组成格式说明的各项加以说明：</div><div>&#9312;%：表示格式说明的起始符号，不可缺少。</div><div>&#9313;-：有-表示左对齐输出，如省略表示右对齐输出。</div><div>&#9314;0：有0表示指定空位填0,如省略表示指定空位不填。</div><div>&#9315;m.n：m指域宽，即对应的输出项在输出设备上所占的字符数。<wbr>N指精度。用于说明输出的实型数的小数位数。为指定n时，<wbr>隐含的精度为n=6位。</div><div>&#9316;l或h:l对整型指long型，对实型指double型。<wbr>h用于将整型的格式字符修正为short型。</div><div><br />－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－<wbr>－－－－－－－－－</div><div><br />格式字符&nbsp;</div><div>格式字符用以指定输出项的数据类型和输出格式。</div><div>&#9312;d格式：用来输出十进制整数。有以下几种用法：</div><div>%d：按整型数据的实际长度输出。</div><div>%md：m为指定的输出字段的宽度。如果数据的位数小于m，<wbr>则左端补以空格，若大于m，则按实际位数输出。</div><div>%ld：输出长整型数据。</div><div>&#9313;o格式：以无符号八进制形式输出整数。对长整型可以用"%<wbr>lo"格式输出。同样也可以指定字段宽度用&#8220;%mo&#8221;格式输出。</div><div>例：</div><div>&nbsp; &nbsp;main()</div><div>&nbsp; &nbsp;{ int a = -1;</div><div>&nbsp; &nbsp; &nbsp;printf("%d, %o", a, a);</div><div>&nbsp; &nbsp;}</div><div>运行结果：-1,177777</div><div>程序解析：-1在内存单元中（以补码形式存放）为(<wbr>1111111111111111)2，转换为八进制数为(<wbr>177777)8。</div><div>&#9314;x格式：以无符号十六进制形式输出整数。对长整型可以用"%<wbr>lx"格式输出。同样也可以指定字段宽度用"%mx"格式输出。</div><div>&#9315;u格式：以无符号十进制形式输出整数。对长整型可以用"%<wbr>lu"格式输出。同样也可以指定字段宽度用&#8220;%mu&#8221;格式输出。</div><div>&#9316;c格式：输出一个字符。</div><div>&#9317;s格式：用来输出一个串。有几中用法</div><div>%s：例如:printf("%s", "CHINA")输出"CHINA"字符串（不包括双引号）。</div><div>%ms：输出的字符串占m列，如字符串本身长度大于m，<wbr>则突破获m的限制,将字符串全部输出。若串长小于m，<wbr>则左补空格。</div><div>%-ms：如果串长小于m，则在m列范围内，字符串向左靠，<wbr>右补空格。</div><div>%m.ns：输出占m列，但只取字符串中左端n个字符。<wbr>这n个字符输出在m列的右侧，左补空格。</div><div>%-m.ns：其中m、n含义同上，<wbr>n个字符输出在m列范围的左侧，右补空格。如果n&gt;m，<wbr>则自动取n值，即保证n个字符正常输出。</div><div>&#9318;f格式：用来输出实数（包括单、双精度），以小数形式输出。<wbr>有以下几种用法：</div><div>%f：不指定宽度，整数部分全部输出并输出6位小数。</div><div>%<a href="http://m.nf/" target="_blank" style="color: #1155cc;">m.nf</a>：输出共占m列，其中有n位小数，<wbr>如数值宽度小于m左端补空格。&nbsp;</div><div>%-<a href="http://m.nf/" target="_blank" style="color: #1155cc;">m.nf</a>：输出共占n列，其中有n位小数，<wbr>如数值宽度小于m右端补空格。</div><div>&#9319;e格式：以指数形式输出实数。可用以下形式：</div><div>%e：数字部分（又称尾数）输出6位小数，<wbr>指数部分占5位或4位。</div><div>%<a href="http://m.ne/" target="_blank" style="color: #1155cc;">m.ne</a>和%-<a href="http://m.ne/" target="_blank" style="color: #1155cc;">m.ne</a>：m、n和&#8221;-&#8221;字符含义与前相同。<wbr>此处n指数据的数字部分的小数位数，<wbr>m表示整个输出数据所占的宽度。</div><div>&#9320;g格式：自动选f格式或e格式中较短的一种输出，<wbr>且不输出无意义的零。</div><div>－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－<wbr>－－－－－－－－－</div><div>关于printf函数的进一步说明：</div><div>如果想输出字符"%",则应该在&#8220;格式控制&#8221;<wbr>字符串中用连续两个%表示，如:</div><div>printf("%f%%", 1.0/3);</div><div>输出0.333333%。</div><div>－－－－－－－－－－－－－－－－－－－－－－－－－－－－－－<wbr>－－－－－－－－－</div><div>对于单精度数，使用%f格式符输出时，仅前7位是有效数字，<wbr>小数6位．</div><div>对于双精度数，使用%lf格式符输出时，前16位是有效数字，<wbr>小数6位．</div><div>##############################<wbr>########拾遗 ##############################<wbr>##########</div><div>由高手指点</div><div>对于m.n的格式还可以用如下方法表示（例）</div><div>char ch[20];</div><div>printf("%*.*s\n",m,n,ch);</div><div>前边的*定义的是总的宽度，后边的定义的是输出的个数。<wbr>分别对应外面的参数m和n 。我想这种方法的好处是可以在语句之外对参数m和n赋值，<wbr>从而控制输出格式。&nbsp;</div><div>------------------------------<wbr>------------------------------<wbr>--------------------</div><div>今天(06.6.9)又看到一种输出格式 %n 可以将所输出字符串的长度值赋绐一个变量, 见下例:</div><div>int slen;</div><div>printf("hello world%n", &amp;slen);</div><div>执行后变量被赋值为11。</div></div><span style="color: #222222; background-color: #ffffff;">＋＋＋＋＋</span><img src ="http://www.cppblog.com/walkklookk/aggbug/196787.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-29 11:08 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/29/196787.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>原：iPhone内存警告 －3</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/29/196783.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sat, 29 Dec 2012 03:01:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/29/196783.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196783.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/29/196783.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196783.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196783.html</trackback:ping><description><![CDATA[<div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">创建：2011.06.27</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">修改：2012.03.15</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">修改：2012.03.26</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">参见附件。</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">补充：</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">ipad 2，ios4.3 测试：</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><div>&nbsp;*memory warning不会打断一个函数的执行。所以，<wbr>一个函数如果消耗太多内存，则收不到memory warning就直接退出了。</div><div>＊当SpringBoard发出mw之后，<wbr>程序要等待当前正在执行的函数结束之后，才能收到mw。</div><div>＊设定一个Timer执行分配内存大的操作，<wbr>则当间隔小于等于0.0003f秒时，<wbr>则不能在mw1时收到警告，只能在mw2时才能收到警告。因此，<wbr>如果要执行2个分配内存较大的操作时，中间间隔至少要比0.<wbr>0003f秒大。</div><div>*如果在一个大的内存分配的函数之后，<wbr>performSelector另外一个大的操作，<wbr>则间隔时间如果小于等于0.0001f，则无法收到mw。所以，<wbr>要比0.0001f大。</div><div></div><div>补充：</div><div>如果要让代码自动发送内存不足的请求，可以：</div><div></div><div><p>- (void)simulateMemoryWarning</p><p>{</p><p>#if TARGET_IPHONE_SIMULATOR</p><p>&nbsp; &nbsp; CFNotificationCenterPostNotifi<wbr>cation(<wbr>CFNotificationCenterGetDarwinN<wbr>otifyCenter(), (CFStringRef)@"<wbr>UISimulatedMemoryWarningNotifi<wbr>cation",&nbsp;NULL,NULL,&nbsp;true);</p><p>#endif</p><p>}</p><p><br /></p><p>&nbsp;</p><p>- (void)viewDidDisappear:(BOOL)<wbr>animated {</p><p>&nbsp; &nbsp; [super&nbsp;viewDidDisappear:animated];</p><p>&nbsp; &nbsp; [self&nbsp;simulateMemoryWarning];</p><p>}</p><p>注意：只能在模拟器下才可以用。此外，在iOS5下不行，<wbr>在iOS4.3下可以。</p><p>另外：转自：<a href="http://idevrecipes.com/2011/05/04/debugging-magic-auto-simulate-memory-warnings/" target="_blank" style="color: #1155cc;">http://idevrecipes.com/<wbr>2011/05/04/debugging-magic-<wbr>auto-simulate-memory-warnings/</a></p><p>不过，原文中是放在viewWillAppear中，但是，<wbr>这样即无法收到viewDidUnload消息了，因此，<wbr>不过viewDidDisappear却可以。</p><p>&nbsp;</p></div><br />+++++</div><img src ="http://www.cppblog.com/walkklookk/aggbug/196783.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-29 11:01 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/29/196783.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>原：GPS定位的一些经验Tip -2</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/29/196782.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sat, 29 Dec 2012 03:00:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/29/196782.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196782.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/29/196782.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196782.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196782.html</trackback:ping><description><![CDATA[<div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">创建：2011.06.02</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">修改：2012.03.28<br /><br />iOS5：<br />＊系统的定位功能有2个设置。第一，<wbr>设置是否在本机禁用定位功能；第二，<wbr>设置是否允许某程序使用定位功能。<br />＊[CLLocationManager locationServicesEnabled] 用于检测第一种情况。</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">＊[CLLocationManager&nbsp;authorizationStatus] !=&nbsp;kCLAuthorizationStatusDenied用于<wbr>检测第二种情况。<br />＊也可以通过CLLocationManager的回调，<wbr>检测第二种情况。<br /></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">＊当用户首次运行程序的定位功能时，如果用户禁用了第一种情况，<wbr>则会提示用户进入Setting设置，或者Cancel。<wbr>这个提示只提示一次。</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">如果选择Cancel，则定位失败，但是却没有调用回调函数。</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">如果选择Setting，并设置了On，则返回程序时，<wbr>会自动进入下面的检测。</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">如果 选择Setting，但是却没有改变什么，重新返回程序，<wbr>则该提示仍然存在。就好象什么也没发生过。</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">如果选择Setting，先设置成On，然后又设置成Off，<wbr>则当重新返回程序时，什么也不提示，定位失败（但是没有回调）。<wbr>这种行为就相当于选择了Cancel。</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">＊当用户首次运行程序的定位功能时，<wbr>如果用户没有禁用了第二种情况，则系统会自动弹出一个消息框，<wbr>询问是否允许本程序的定位功能。<br />如果是，则正常流程。<br />如果否，则无法定位。当然，<wbr>这是可以通过CLLocationManager的回调检测到，<wbr>而MapKit则无法检测到。<br /></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">＊一个台机器，对于一个程序只提示一次。如果想重新提示，<wbr>则需要如下设定：Settings &gt;&gt; General &gt;&gt; Reset &gt;&gt;Reset Location warnings&nbsp;<br />*该提示消息框，是系统监测到第一个定位消息的时候提示的，<wbr>目前还不能具体定位到是哪个函数引发的。<br /><br />*两种提示框都可以增加额外的内容，通过，设定</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><p>locationManager.purpose&nbsp;=&nbsp;@"test";</p><p>可以在Title的下面，加入一个message，<wbr>它的内容就是上面的@"test"。</p><p><br /></p><p>*有一个判断GPS是否打开的回调：</p><p>- (void)locationManager:(CLLocat<wbr>ionManager&nbsp;*)manager didChangeAuthorizationStatus:(<wbr>CLAuthorizationStatus)status</p><p>但是这个函数不太好用。如果是第一种情况的Alert，<wbr>则status先为kCLAuthorizationStatu<wbr>sDenied，然后才正常流程。</p><p>这导致程序无法判断GPS是不是真的被完全拒绝了。</p><p>还是使用- (void)locationManager:(CLLocat<wbr>ionManager&nbsp;*)manager&nbsp;didFailWithError:(NS<wbr>Error&nbsp;*)error;</p><p>判定好一些。</p><p><br /></p><br />其它网页上的信息：<br />转：<br /><br />I don't know of any specific list, but here are a few of things you should keep in mind:<br /><br />&nbsp;&nbsp;&nbsp; You're not doing GPS lookups. You're doing Core Location lookups. Core Location might or might not be getting its information from GPS. Lots of people still use pre-3g iPhones that don't have GPS, and even on a 3g phone GPS may be unavailable in many cases (if the person is indoors, for example). In those cases the phone will attempt to triangulate based on cell phone towers, and Core Location will return the result. It'll be much less precise than GPS.<br />&nbsp;&nbsp;&nbsp; Core Location will cache data. The first reading it provides to your app is likely to be an old reading, which might or might not be accurate, depending on whether the phone has moved. Make sure to check the timestamp of any location and see if it's from before your app started.<br />&nbsp;&nbsp;&nbsp; I don't know what you mean by "power save" mode, but if you're thinking of when the screen is locked/off, that does not stop Core Location from running if your app is still running. On the contrary it's easy to run down your phone's battery much more quickly than you'd expect if you lock the phone while an app that uses Core Location is running, because the phone will continue to update the app as new location data is available. You could avoid this in your application by listening for UIApplicationWillResignActiveN<wbr>otification to detect the screen locking, and UIApplicationDidBecomeActiveNo<wbr>tification to detect unlock.<br />&nbsp;&nbsp;&nbsp; Higher precision results will take more time to acquire, because the longer you wait, the better the results, up to a limit. If you decide you need to be within N meters, consider how long the user might have to stand there waiting for the phone to home in on its location.<br /><br />Whether 20 meters is accurate enough is something only you can answer, based on how you expect your application to be used. Test the app and see if it works the way you want it to work.<br /><br /><br />Tom had a great answer (I wanted to note especially the usual return of the first value might be an older cached location) but I had a few more things to add:<br /><br />&nbsp;&nbsp;&nbsp; You can tell the location manager to stop and start again if you want to force it to issue you at least one more location update.<br />&nbsp;&nbsp;&nbsp; If you were not aware, there are convenience methods in CLLocation to give you the distance between two locations.<br />&nbsp;&nbsp;&nbsp; As the docs note, you should implement locationManager:<wbr>didFailWithError: even though you do not have to (if only to stop the location manager updates for a time).<br />&nbsp;&nbsp;&nbsp; Test thoroughly scenarios where the user disallows location updates for your application!<br />&nbsp;&nbsp;&nbsp; Very, very infrequently you may get updates out of order (i.e. you get a more accurate location followed by a slightly less accurate one dated earlier)<br />&nbsp;&nbsp;&nbsp; The simulator will give you a location update, but will always report your location as Apple HQ.<br /><br /><br /><br />＋＋＋＋＋</div><img src ="http://www.cppblog.com/walkklookk/aggbug/196782.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-29 11:00 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/29/196782.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>转：stop NSInvocationOperation</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/29/196780.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sat, 29 Dec 2012 02:59:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/29/196780.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196780.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/29/196780.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196780.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196780.html</trackback:ping><description><![CDATA[<span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">创建：2012.03.29</span><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">转：<a href="http://stackoverflow.com/questions/763003/how-to-stop-an-nsinvocationoperation" target="_blank" style="color: #1155cc;">http://stackoverflow.com/<wbr>questions/763003/how-to-stop-<wbr>an-nsinvocationoperation</a>］</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">［自：<wbr>NSInvocationOperation用起来很方便，<wbr>但是却没有办法stop，所以在target退出的时候，<wbr>不能很好的终止op。好在当执行<span style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; line-height: 18px; margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline;">[</span><span style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; line-height: 18px; margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline;">operation initWithTarget</span><span style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; line-height: 18px; margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline;">:</span><span style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; line-height: 18px; margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline;">self selector</span><span style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; line-height: 18px; margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline;">:</span><span style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; line-height: 18px; margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline;">@selector</span><span style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; line-height: 18px; margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline;">(</span><span style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; line-height: 18px; margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline;">myOperation</span><span style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; line-height: 18px; margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline;"><wbr>:)</span><span style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; line-height: 18px; margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline;">&nbsp;object</span><span style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; line-height: 18px; margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline;">:</span><span style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; line-height: 18px; margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline;">operation</span><span style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; line-height: 18px; margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline;">];的时候，target（<wbr>也就是self）的引用计数会自动加1，所以，<wbr>可以使用下面的方法来处理。］</span></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><p style="padding: 0px; border-width: 0px; margin: 0px 0px 1em; clear: both; vertical-align: baseline; line-height: 18px; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; word-wrap: break-word;"><strong style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">UPDATE:</strong>&nbsp;Instruments shows leaks-a-plenty when I do this.&nbsp;<em style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">Proceed with caution!</em>&nbsp;I'm keeping this here in case I'm actually on to something and someone else can figure out how to get over the leak hurdle.</p><p style="padding: 0px; border-width: 0px; margin: 0px 0px 1em; clear: both; vertical-align: baseline; line-height: 18px; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; word-wrap: break-word;">Here's a twisted idea, which I'm re-trying as I type this:</p><p style="padding: 0px; border-width: 0px; margin: 0px 0px 1em; clear: both; vertical-align: baseline; line-height: 18px; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; word-wrap: break-word;">Set the operation as the&nbsp;<em style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">object</em>&nbsp;for&nbsp;<strong style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">NSInvocationOpe<wbr>ration</strong>'s&nbsp;<em style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">initWithTarget:<wbr>selector:object:</em>&nbsp;method. Presuming you already have a&nbsp;<strong style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">NSOperationQueue</strong>&nbsp;(we'll call it&nbsp;<em style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">queue</em>):</p><pre style="white-space: pre-wrap; margin-top: 0px; margin-bottom: 10px; padding: 5px; border-width: 0px; vertical-align: baseline; background-color: #eeeeee; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; overflow: auto; width: auto; max-height: 600px; line-height: 18px;"><code style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;"><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #2b91af;">NSInvocationOperation</span> <span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">*</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">operation </span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">=</span> <span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">[</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #2b91af;">NSInvocationOperation</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;"> alloc</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">];</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;"><br /> operation </span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">=</span> <span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">[</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">operation initWithTarget</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">:</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">self selector</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">:</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #800000;">@selector</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">(</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">myOperation</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;"><wbr>:)</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;"> object</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">:</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">operation</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">];</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;"><br /> </span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">[</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">queue addOperation</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">:</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">operation</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">];</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;"><br /> </span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">[</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">operation release</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">];</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;"><br /> </span></code></pre><p style="padding: 0px; border-width: 0px; margin: 0px 0px 1em; clear: both; vertical-align: baseline; line-height: 18px; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; word-wrap: break-word;">Note that we have to break apart the alloc into its own call. Otherwise we can't set&nbsp;<em style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">object</em>&nbsp;to&nbsp;<em style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">operation</em>!</p><p style="padding: 0px; border-width: 0px; margin: 0px 0px 1em; clear: both; vertical-align: baseline; line-height: 18px; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; word-wrap: break-word;">Then, within your operation method, cast the object back and sprinkle checks for&nbsp;<em style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">isCancelled</em>&nbsp;as desired. For example:</p><pre style="white-space: pre-wrap; margin-top: 0px; margin-bottom: 10px; padding: 5px; border-width: 0px; vertical-align: baseline; background-color: #eeeeee; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; overflow: auto; width: auto; max-height: 600px; line-height: 18px;"><code style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;">&nbsp;<span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">-</span> <span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">(</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #00008b;">void</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">)</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">myOperation</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">:(</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">id</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">)</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">object </span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">{</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;"><br /> &nbsp; &nbsp; </span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #2b91af;">NSInvocationOperation</span> <span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">*</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">operation </span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">=</span> <span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">(</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #2b91af;">NSInvocationOperation</span> <span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">*)</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">object</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">;</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;"><br /> &nbsp; &nbsp; </span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #00008b;">if</span> <span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">([</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">operation isCancelled</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">])</span> <span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent; color: #00008b;">return</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">;</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;"><br /> &nbsp; &nbsp; </span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">...</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;"><br /> &nbsp; </span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">}</span><span style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;"><br /> </span></code></pre><p style="padding: 0px; border-width: 0px; margin: 0px 0px 1em; clear: both; vertical-align: baseline; line-height: 18px; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; word-wrap: break-word;">Make sure your selector ends with a colon back in the&nbsp;<em style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">initWithTarget:...</em>&nbsp;call, since you'll be passing in an object now.</p><p style="padding: 0px; border-width: 0px; margin: 0px 0px 1em; clear: both; vertical-align: baseline; line-height: 18px; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; word-wrap: break-word;">So far, so good. Now if I can force&nbsp;<em style="margin: 0px; padding: 0px; border-width: 0px; vertical-align: baseline; background-color: transparent;">cancelAllOperations</em>, I'll know if this actually works. :)<br /><br />+++++</p></div><img src ="http://www.cppblog.com/walkklookk/aggbug/196780.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-29 10:59 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/29/196780.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>转：Using Blocks in iOS 4 --2</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/29/196779.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sat, 29 Dec 2012 02:58:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/29/196779.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196779.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/29/196779.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196779.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196779.html</trackback:ping><description><![CDATA[<span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">创建：2010-08-09</span><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">修改：2012-04-06<br /><br /><div>转：<a href="http://pragmaticstudio.com/blog/2010/7/28/ios4-blocks-1" target="_blank" style="color: #1155cc;">http://pragmaticstudio.com/<wbr>blog/2010/7/28/ios4-blocks-1</a><br /><br />[自：看原文更清楚，因为有各种颜色的高亮。]<br />［自：几个关键之处：<br />1，Block内可以使用其前声明的变量值，<wbr>但是该变量值即使再后来进行了改动，<wbr>也不会影响到Block中的值，也就是说，<wbr>类似与传递一个一个值传递的参数！<br />2，Block定义时，可以不使用返回值，<wbr>编译器会自动推断出返回值。<br />3，介绍了Block变量类型的声明方法，<wbr>感觉跟函数指针基本一样，只不过是由*编程了^］<br /><div><p>iOS 4 introduces one new feature that will fundamentally change the way you program in general: blocks. Blocks are an extension to the C language and thus fully supported in Objective-C. If you&#8217;re coming from a programming language such as Ruby, Python, or Lisp, then you know the power of blocks. Simply put, blocks let you encapsulate chunks of code and pass them around like any other object. It&#8217;s a different style of programming that you&#8217;ll want to become familiar with to take advantage of new APIs in iOS 4.</p><p>Let&#8217;s start by taking a look at two examples of where you might use blocks in iOS 4: view animations and enumeration.</p><h3>Blocks By Example</h3><p>As our first example, suppose we&#8217;re creating a card game and we want to animate sliding a card from the dealer&#8217;s hand to a player&#8217;s position. Fortunately, the UIKit framework does all the heavy lifting when it comes to performing animations. What gets animated, however, is specific to your application. You specify what will be animated in a block, and toss it over to the&nbsp;<code>animateWithDuration:<wbr>animations:</code>&nbsp;method, like so:</p><div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td style="margin: 0px;"><div><code>[UIView animateWithDuration:2.0</code></div><div><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><code>animations:^ {</code></div><div><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code>self</code><code>.cardView.alpha = 1.0;</code></div><div><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code>self</code><code>.cardView.frame = CGRectMake(176.0, 258.0, 72.0, 96.0);</code></div><div><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code>self</code><code>.cardView.<wbr>transform = CGAffineTransformMakeRotation(<wbr>M_PI);</code></div><div><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><code>}</code></div><div><code>];</code></div></td></tr></tbody></table></div><p>When this animation block is run, our card view will animate in three ways: change its alpha to fade in the card, change its position to the lower-right of the frame (the player&#8217;s position), and rotate itself 180 degrees (to give the dealer style points).</p><p>Our second example of blocks is to enumerate over a collection of cards and print the name and index of each card. You could use a&nbsp;<code>for</code>&nbsp;loop for this, but in iOS 4 the<code>NSArray</code>&nbsp;class has a handy&nbsp;<code>enumerateObjectsUsingBlock:</code>&nbsp;method that takes a block. Here&#8217;s how to use it:</p><div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td style="margin: 0px;"><div><code>NSArray</code>&nbsp;<code>*cards =</code></div><div><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><code>[</code><code>NSArray</code>&nbsp;<code>arrayWithObjects:@</code><code>"Jack"</code><code>, @</code><code>"Queen"</code><code>, @</code><code>"King"</code><code>, @</code><code>"Ace"</code><code>,&nbsp;</code><code>nil</code><code>];</code></div><div>&nbsp;</div><div><code>[cards enumerateObjectsUsingBlock:^(</code><code>i<wbr>d</code>&nbsp;<code>object,&nbsp;</code><code>NSUInteger</code>&nbsp;<code>index,&nbsp;</code><code>BOOL</code>&nbsp;<code>*stop) {</code></div><div><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><code>NSLog</code><code>(@</code><code>"%@ card at index %d"</code><code>, object, index);</code></div><div><code>}];</code></div></td></tr></tbody></table></div><p>As we&#8217;ll explore a bit more later, this block takes three parameters: the current element in the array, its index, and a flag to signal whether enumeration should stop (which we&#8217;ve ignored). The&nbsp;<code>enumerateObjectsUsingBlock:</code>&nbsp;method calls the block once for each element in the array and supplies the parameters.</p><p>So the upshot of using blocks in your Mac and iOS apps is that they allow you to attach arbitrary code to Apple-provided methods. Although similar in concept to delegation, passing short inline blocks of code to methods is often more convenient and elegant.</p><p>That&#8217;s a good start, but it&#8217;s important to understand what&#8217;s going on. Whenever I&#8217;m learning anything new, I like to break it down into its simplest elements, get comfortable with how they work, and then (hopefully) put everything back together again. That way I feel confident with the code I write and can quickly debug any problems. So let&#8217;s step back for a minute and learn how to declare and call basic blocks.</p><h3>Block Basics</h3><p>A block is simply a chunk of executable code. For example, here&#8217;s a block that prints the current date and time:</p><div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td style="margin: 0px;"><div><code>^ {</code></div><div><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><code>NSDate</code>&nbsp;<code>*date = [</code><code>NSDate</code>&nbsp;<code>date];</code></div><div><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><code>NSLog</code><code>(@</code><code>"The date and time is %@"</code><code>, date);</code></div><div><code>};</code></div></td></tr></tbody></table></div><p>The caret (<code>^</code>) introduces a block literal and the curly braces enclose statements that make up the body of the block. You can think of a block as being similar to an anonymous function.</p><p>So if it&#8217;s anonymous, how exactly do we use this block? The most common way to use a block is to pass it to a method that in turn calls the block. We saw how to do that earlier with animations and enumeration. The other way to use a block is to assign it to a block variable and call the block directly. Here&#8217;s how to assign our block to a block variable called&nbsp;<code>now</code>:</p><div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td style="margin: 0px;"><div><code>void</code>&nbsp;<code>(^now)(</code><code>void</code><code>) = ^ {</code></div><div><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><code>NSDate</code>&nbsp;<code>*date = [</code><code>NSDate</code>&nbsp;<code>date];</code></div><div><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><code>NSLog</code><code>(@</code><code>"The date and time is %@"</code><code>, date);</code></div><div><code>};</code></div></td></tr></tbody></table></div><p>Here&#8217;s where things get funky. The syntax for declaring a block variable takes some getting used to. If you&#8217;ve used function pointers, block variables will look familiar. On the right-hand side of the assignment we have our block literal (nothing new there). On the left-hand side of the assignment we&#8217;ve declared a block variable called&nbsp;<code>now</code>.</p><div><img src="http://pragmaticstudio.com/images/blog/block-syntax-1.png" alt="Block Syntax" width="225" height="89" /></div><p>The name of the block variable is always preceded by a&nbsp;<code>^</code>&nbsp;and in parentheses. Block variables have an associated type. In this case, the&nbsp;<code>now</code>&nbsp;variable can reference any block that returns no value (the first&nbsp;<code>void</code>) and takes no parameters (the&nbsp;<code>void</code>&nbsp;in parentheses). Our block conforms to this type, so we can safely assign it to the&nbsp;<code>now</code>variable.</p><p>Once we have a block variable in scope, calling the block is just like calling a function. Here&#8217;s how we call our block:</p><div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td style="margin: 0px;"><code>now();</code></td></tr></tbody></table></div><p>You could declare the block variable in a C function or Objective-C method, for example, and then call it in the same scope. When the block executes, it prints the current date and time. So far, so good.</p><h3>Blocks Are Closures</h3><p>If that&#8217;s all there was to blocks, they&#8217;d be just like functions. But it turns out that blocks are more than just chunks of executable code. Blocks also capture their surrounding state. That is, blocks are closures: they&nbsp;<em>close around</em>&nbsp;variables that are in scope at the time the block is declared. To illustrate, let&#8217;s change the previous example around a bit by moving the initialization of the date outside the block:</p><div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td style="margin: 0px;"><div><code>NSDate</code>&nbsp;<code>*date = [</code><code>NSDate</code>&nbsp;<code>date];</code></div><div>&nbsp;</div><div><code>void</code>&nbsp;<code>(^now)(</code><code>void</code><code>) = ^ {</code></div><div><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><code>NSLog</code><code>(@</code><code>"The date and time is %@"</code><code>, date);</code></div><div><code>};</code></div><div>&nbsp;</div><div><code>now();</code></div></td></tr></tbody></table></div><p>When you call this block the first time, it behaves exactly like the previous version: it prints the current date and time. But there&#8217;s a significant difference here. It becomes evident when we change the date and then call the block again:</p><div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td style="margin: 0px;"><div><code>sleep(5);</code></div><div>&nbsp;</div><div><code>date = [</code><code>NSDate</code>&nbsp;<code>date];</code></div><div><code>&nbsp;</code>&nbsp;</div><div><code>now();</code></div></td></tr></tbody></table></div><p>Even though we&#8217;ve changed the&nbsp;<code>date</code>&nbsp;variable referenced by the block, when the block is called it still prints the original date and time. It&#8217;s as if time stood still when the block was declared. And that&#8217;s effectively what happens. As execution passes over the point where the block is declared, the block takes a (read-only) snapshot of all the variables in scope that the block uses. You can think of the value of the&nbsp;<code>date</code>&nbsp;variable as being frozen inside the block. Therefore, whenever the block is called&#8212;immediately, 5 seconds later, or just before the app quits&#8212;it always prints the original date and time.</p><p>Now, the fact that blocks are closures is not particularly interesting in this example. After all, you could have just passed the date as a parameter to the block (more on that next). But closures become really useful when you start passing blocks around to methods because the captured state goes along for the ride.</p><h3>Block Parameters</h3><p>Just like functions, blocks can take parameters and return values. Say, for example, we want a block that takes a given number and returns the result of tripling that number. Here&#8217;s the block literal:</p><div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td style="margin: 0px;"><div><code>^(</code><code>int</code>&nbsp;<code>number) {</code></div><div><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><code>return</code>&nbsp;<code>number * 3;</code></div><div><code>};</code></div></td></tr></tbody></table></div><p>Assigning this block to a block variable called&nbsp;<code>triple</code>&nbsp;looks like this:</p><div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td style="margin: 0px;"><div><code>int</code>&nbsp;<code>(^triple)(</code><code>int</code><code>) = ^(</code><code>int</code>&nbsp;<code>number) {</code></div><div><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><code>return</code>&nbsp;<code>number * 3;</code></div><div><code>};</code></div></td></tr></tbody></table></div><p>Again, the tricky part is getting comfortable with the block variable syntax on the left-hand side of the assignment. Let&#8217;s break it down from left to right.</p><div><img src="http://pragmaticstudio.com/images/blog/block-syntax-2.png" alt="Block Syntax" width="225" height="82" /></div><p>The first&nbsp;<code>int</code>&nbsp;is the return type. Then in parentheses comes the caret introducing the block variable called&nbsp;<code>triple</code>. Finally we have a list of parameter types in parentheses (one&nbsp;<code>int</code>&nbsp;in this case). The block literal on the right-hand side of the assignment conforms to this type. Note, however, that as a matter of convenience there&#8217;s no need to declare the return type of the block literal. The compiler can infer it from the return statement.</p><p>To call the block, you need to pass the number to be tripled and (ideally) do something with the return value, like so:</p><div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td style="margin: 0px;"><code>int</code>&nbsp;<code>result = triple(2);</code></td></tr></tbody></table></div><p>By way of comparison, here&#8217;s how you would declare and create a block that takes two&nbsp;<code>int</code>&nbsp;parameters, multiplies them together, and returns the result as an&nbsp;<code>int</code>&nbsp;value:</p><div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td style="margin: 0px;"><div><code>int</code>&nbsp;<code>(^multiply)(</code><code>int</code><code>,&nbsp;</code><code>int</code><code>) = ^(</code><code>int</code>&nbsp;<code>x,&nbsp;</code><code>int</code>&nbsp;<code>y) {</code></div><div><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><code>return</code>&nbsp;<code>x * y;</code></div><div><code>};</code></div></td></tr></tbody></table></div><p>And here&#8217;s how you&#8217;d call this block:</p><div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td style="margin: 0px;"><code>int</code>&nbsp;<code>result = multiply(2, 3);</code></td></tr></tbody></table></div><p>Declaring block variables gave us an opportunity to explore block types and how to call blocks. The block variable looks like a function pointer and calling the block is similar to calling a function. But unlike function pointers, blocks are actually Objective-C objects. And that means we can pass them around like other objects.</p><h3>Methods Can Take Blocks</h3><p>Now, in practice blocks are most useful when you pass them as parameters to methods that in turn call the block. And when you&#8217;re passing a block to a method, it&#8217;s usually more convenient to use inline blocks rather than assigning the block to a typed variable and then passing it to the method. For instance, we used inline blocks in the animation and enumeration examples we saw earlier.</p><p>Apple has added methods to their frameworks that take blocks, and you can write APIs that take blocks, too. For example, suppose we want to create a&nbsp;<code>Worker</code>&nbsp;class method that takes a block and repeatedly calls it a given number of times, passing in the repeat count each time. Here&#8217;s how we might call that method with an inline block that triples each number (1 through 10):</p><div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td style="margin: 0px;"><div><code>[Worker repeat:10 withBlock:^(</code><code>int</code>&nbsp;<code>number) {</code></div><div><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><code>return</code>&nbsp;<code>number * 3;</code></div><div><code>}];</code></div></td></tr></tbody></table></div><p>The method could handle any block that takes a single&nbsp;<code>int</code>&nbsp;parameter and returns an<code>int</code>&nbsp;result. Want to double all the numbers? Just give the method a different block.</p><h3>Your Turn</h3><p>OK, so how would you implement the&nbsp;<code>repeat:withBlock:</code>&nbsp;method above to accept and call a passed block? Give it some thought, and we&#8217;ll tackle it in the next installment. In the meantime, practice using blocks by calling the&nbsp;<code>enumerateKeysAndObjectsUsingBl<wbr>ock:</code>method with a block that prints the keys and values of this&nbsp;<code>NSDictionary</code>:</p><div><table border="0" cellpadding="0" cellspacing="0"><tbody><tr><td style="margin: 0px;"><div><code>NSDictionary</code>&nbsp;<code>*cards =</code></div><div><code>&nbsp;&nbsp;&nbsp;&nbsp;</code><code>[</code><code>NSDictionary</code>&nbsp;<code>dictionaryWithObjectsAndKeys:@</code><code><wbr>"Queen"</code><code>,&nbsp; @</code><code>"card"</code><code>,</code></div><div><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<wbr>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code>@</code><code>"Hearts"</code><code>, @</code><code>"suit"</code><code>,</code></div><div><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<wbr>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code><code>@</code><code>"10"</code><code>,&nbsp;&nbsp;&nbsp;&nbsp; @</code><code>"value"</code><code>,&nbsp;</code><code>nil</code><code>];</code></div></td></tr></tbody></table></div><p>Have fun, and&nbsp;<a href="http://feeds.feedburner.com/PragmaticStudio" target="_blank" style="color: #1155cc;">stay tuned</a>&nbsp;for more on blocks&#8230;</p></div><br />＋＋＋＋＋</div></div><img src ="http://www.cppblog.com/walkklookk/aggbug/196779.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-29 10:58 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/29/196779.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>转：块编程指南</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/29/196777.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sat, 29 Dec 2012 02:57:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/29/196777.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196777.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/29/196777.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196777.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196777.html</trackback:ping><description><![CDATA[<span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">创建：2012.04.09</span><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">[自：块跟函数指针类似。</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">普通函数变量传入进去，相当于传值，但是却不可以改。__<wbr>block变量进去相当于传递static指针。<wbr>分配释放等工作其实不必太考虑，底层处理的很好。</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">]</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">转：<a href="http://blog.csdn.net/kmyhy/article/details/6447287" target="_blank" style="color: #1155cc;">http://blog.csdn.net/kmyhy/<wbr>article/details/6447287</a></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><p style="margin: 0px; line-height: 26px; color: #333333; font-family: Arial;"><br /></p>&#8212;&#8212;译自Apple Reference Library《Blocks Programming Topic》<br /><br /><br />简介<br /><br /><br />块对象是C语言的句法和运行时特性。它类似于标准C函数，<wbr>但可以将代码、变量绑定到堆(heap)、栈（stack）。<wbr>一个块还维护了一系列的状态，这些状态或数据影响着执行的结果。<br /><br />可以把块组成函数表达式，用于传递给API，<wbr>或者使用在多线程里。最有用的是回调，<wbr>因为块在回调时能把代码和数据一起传送。<br /><br />在OSX 10.6的Xcode中，可以使用块,它随GCC和&nbsp;<wbr>Clang&nbsp;一起集成。在OSX 10.6及iOS 4.0以后支持块语法。&nbsp;块运行时是开源的，它能被集成到&nbsp;<wbr>LLVM&#8217;s compiler-rt subproject repository中。标准C工作组的&nbsp;N1370: Apple&#8217;s Extensions to C&nbsp;中&nbsp;(&nbsp;其中也包括垃圾回收&nbsp;)&nbsp;对块进行了定义。O-<wbr>C和C++都来自于C，块在3种语言（包括O-C++）<wbr>都能工作。<br /><br />这篇文档中，你会学习到什么是块对象，以及怎样在C，C++<wbr>和O-C中使用它，使代码的性能和可维护性更高。<br /><br /></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">开始<br /><br />声明块<br /><br />^&nbsp;操作符声明一个块变量的开始（跟C一样用; 来表示表达式结束），如代码所示：<br /><br />int multiplier = 7;<br /><br />int (^myBlock)(int) = ^(int num) {<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;return num * multiplier;<br /><br />};<br /><br />解释&nbsp;:<p style="margin: 0px; line-height: 26px; color: #333333; font-family: Arial;"><img src="http://hi.csdn.net/attachment/201105/26/0_130638794577C8.gif" alt="" width="411" height="169" style="border-style: none;" /></p><p style="margin: 0px; line-height: 26px; color: #333333; font-family: Arial;">&nbsp;</p>注意，块可以使用同一作用域内定义的变量。<br />&nbsp;<br /><br />一旦声明了块，你可以象使用函数一样调用它：<br /><br />int multiplier = 7;<br /><br />int (^myBlock)(int) = ^(int num) {<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;return num * multiplier;<br /><br />};<br /><br />printf("%d", myBlock(3));</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><br />直接使用块<br /><br />很多情况下，你不必声明块变量，<wbr>而简单地写一个行内块并把它当作一个参数，如下面的代码所示。<br /><br />gsort_b类似标准的 gsort_r 函数，但它最后一个参数是一个块。<br /><br />char *myCharacters[3] = { "TomJohn", "George", "Charles Condomine" };<br /><br />qsort_b(myCharacters, 3, sizeof(char *), ^(const void *l, const void *r) {<br /><br />&nbsp;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;char *left = *(char **)l;<br /><br />&nbsp;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;char *right = *(char **)r;<br /><br />&nbsp;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;return strncmp(left, right, 1);<br /><br />&nbsp;<br /><br />});<br /><br />// myCharacters is now { "Charles Condomine", "George", TomJohn" }</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><br />Cocoa 和块<br /><br />在Cocoa框架中，有几种把块作为参数的方法。<wbr>典型的是在集合中进行一个操作，或者在操作完成后作为一个回调。<wbr>下列代码显示如何在NSArray的sortedArrayUs<wbr>ingComparator方法中使用块。<wbr>这个方法使用了一个块参数。为了演示，<wbr>在这里把块定义为一个NSComparator本地变量。<br /><br />&nbsp;<br /><br />NSArray *stringsArray = [NSArray arrayWithObjects:@"string 1",&nbsp;&nbsp;@"String 21",@"string 12",<br /><br />@"String 11", @"String 02", nil];<br /><br />static NSStringCompareOptions comparisonOptions = NSCaseInsensitiveSearch | NSNumericSearch |<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<wbr>NSWidthInsensitiveSearch | NSForcedOrderingSearch;<br /><br />NSLocale *currentLocale = [NSLocale currentLocale];<br /><br />NSComparator finderSortBlock = ^(id string1, id string2) {<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;NSRange string1Range = NSMakeRange(0, [string1 length]);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;return [string1 compare:string2 options:comparisonOptions range:string1Range locale:currentLocale];<br /><br />};<br /><br />NSArray *finderSortArray = [stringsArray sortedArrayUsingComparator:<wbr>finderSortBlock];<br /><br />NSLog(@"finderSortArray: %@", finderSortArray);<br /><br />&nbsp;<br /><br />/*Output:<br /><br />finderSortArray: (<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;"string 1",<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;"String 02",<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;"String 11",<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;"string 12",<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;"String 21"<br /><br />)*/</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><br />块变量<br /><br />块的一个强大功能它可以改变在同一作用域内的变量。用__<wbr>block修饰符来标识一个变量能够被块改变。使用下面的代码，<wbr>你可以用一个块变量计算进行比较的字符串中有多少是相同的。<wbr>为了演示，块是直接使用的，<wbr>同时currentLocal变量对于块来说是只读的。<br /><br />NSArray *stringsArray = [NSArray arrayWithObjects:<br /><br />@"string 1",&nbsp;&nbsp;@"String 21", // &lt;-<br /><br />&nbsp;&nbsp;@"string 12",&nbsp;&nbsp;@"String 11",@"Str&#238;ng 21", // &lt;-<br /><br />&nbsp;&nbsp;@"Stri&#241;g 21", // &lt;-<br /><br />@"String 02", nil];<br /><br />NSLocale *currentLocale = [NSLocale currentLocale];<br /><br />__block NSUInteger orderedSameCount = 0;<br /><br />NSArray *diacriticInsensitiveSortArray = [stringsArray sortedArrayUsingComparator:^(<wbr>id string1, id string2) {<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;NSRange string1Range = NSMakeRange(0, [string1 length]);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;NSComparisonResult comparisonResult = [string1 compare:string2 options:<wbr>NSDiacriticInsensitiveSearch range:string1Range locale:currentLocale];<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;if (comparisonResult == NSOrderedSame) {<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;orderedSameCount++;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;return comparisonResult;<br /><br />}];<br /><br />NSLog(@"<wbr>diacriticInsensitiveSortArray: %@", diacriticInsensitiveSortArray)<wbr>;<br /><br />NSLog(@"orderedSameCount: %d", orderedSameCount);<br /><br />/*Output:<br /><br />diacriticInsensitiveSortArray: (<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;"String 02",<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;"string 1",<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;"String 11",<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;"string 12",<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;"String 21",<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;"Str/U00eeng 21",<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;"Stri/U00f1g 21"<br /><br />)<br /><br />orderedSameCount: 2<br /><br />*/</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><br />相关概念<br /><br />块提供了一种方法，允许你创建一种特殊的函数体，<wbr>在C及C派生语言如O-C和C++中，可以把块视为表达式。<wbr>其他语言中，为了不与C术语中的块混淆，<wbr>块也被称作closure（国内译作闭包），<wbr>这里它们都称做blocks。</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><br /><span style="font-size: large;"><br /></span></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="font-size: large;">块的功能</span></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">块是行内的代码集合：<br /><br />&#9642;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;同函数一样，有类型化参数列表<br />&#9642;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;有返回结果或者要申明返回类型<br />&#9642;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;能获取同一作用域（定义块的相同作用域）<wbr>内的状态［自：可以少传入一些参数。］<br />&#9642;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;可以修改同一作用域的状态（变量）［自：<wbr>相当于能够返回多个返回值。］<br />&#9642;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;与同一范围内的其他块同享变量&nbsp;［自：相当于static变量，只不过在块释放之后，<wbr>会自动释放。］<br />&#9642;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<wbr>在作用域释放后能继续共享和改变同一范围内的变量<span style="color: #3333ff;">［自：<wbr>相当于static变量，只不过在块释放之后，会自动释放。］</span><br /><br />甚至可以复制块并传递到其他后续执行的线程。<wbr>编译器和运行时负责把所有块引用的变量保护在所有块的拷贝的生命<wbr>周期内。对于C和C++，块是变量，但对于O-C ，块仍然是对象。<br /><br /><br />块的使用<br /><br />块通常代表小段的、自包含的代码片段。<br /><br />因此，它们封装为可以并行执行的工作单元额外有用，<wbr>要么用于在集合中进行遍历，要么在其他操作完成使作为回调。<br /><br />块代替传统回调函数的意义有两个：<br /><br />1. 它们允许在方法实现的调用中就近地写入代码。<wbr>而且块经常被作为框架中一些方法的参数。<br /><br />2. 它们允许访问本地变量。<span style="background-color: #cccccc;">在进行线程操作时，<wbr>相比回调函数需要把所需的上下文信息植入数据结构中而言，<wbr>块直接访问本地变量显然更加简单。［自：注意，<wbr>只是使避免传递多个参数而已，并不是&#8220;线程存储</span><span style="background-color: #cccccc;">&#8221;技术</span><span style="background-color: #cccccc;">。］</span></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><br /></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">块的声明和创建<br /><br />声明块变量<br /><br />块变量引用了块。它的声明语法类似函数指针，除了需要使用^<wbr>代替*。<br /><br />void (^<wbr>blockReturningVoidWithVoidArgu<wbr>ment)(void);<br /><br />int (^<wbr>blockReturningIntWithIntAndCha<wbr>rArguments)(int, char);<br /><br />void (^<wbr>arrayOfTenBlocksReturningVoidW<wbr>ithIntArgument[10])(int);<br /><br /><span style="background-color: #cccccc;">块支持可变参数（&#8230;）。</span><span style="background-color: #ff6666;">如果块没有参数，<wbr>则必需使用void来代替整个参数列表。</span><br /><br />块是类型安全的，通过设置编译选项，编译器会检查块的调用、<wbr>参数和返回类型。可以把块变量转换为指针类型，<span style="background-color: #cccccc;">但不能使用*<wbr>对其解除引用&#8212;&#8212;块的长度在编译时无法确定。</span><br /><br />可以创建一个块类型，<wbr>这样你就可以把块当作一个可以反复多次使用的符号：<br /><br />typedef float (^MyBlockType)(float, float);<br /><br />MyBlockType myFirstBlock = // ... ;<br /><br />MyBlockType mySecondBlock = // ... ;</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><br />创建块<br /><br />块以^开始，以;结束。下面显示了块的定义：<br /><br />int (^oneFrom)(int);<br /><br />oneFrom = ^(int anInt) {<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;return anInt - 1;<br /><br />};<br /><br />如果未显式地声明块的返回值类型，<wbr>可能会自动从块代码中推断返回类型。<span style="background-color: #cccccc;">如果参数列表为void，<wbr>而且返回类型依靠推断，你可以省略参数列表的void。</span>否则，<wbr>当块中存在return语句时，它们应当是精确匹配的（<wbr>可能需要必要的类型转换）。</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><br />全局块<br /><br /><span style="background-color: #cccccc;">可以把块定义为全局变量，在文件级别上使用。</span><br /><br />#import &lt;stdio.h&gt;<br /><br />int GlobalInt = 0;<br /><br />int (^getGlobalInt)(void) = ^{ return GlobalInt; };<br /></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">块和变量<br /><br />本节描述块和变量之间的交互，包括内存管理。</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">变量类型<br /><br />在块代码内部，变量会被处理为5种不同情况。<br /></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">就像函数一样，可以引用3种标准的变量：<br />&#9642;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;全局变量，包括静态变量<br />&#9642;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;全局函数<br />&#9642;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;本地变量及参数（在块范围内）<br /><br />此外块还支持两种变量：<br />1.&nbsp;&nbsp;&nbsp;&nbsp;在函数级别，是__block变量。<wbr>它们在块范围内是可变的，如果所引用的块被复制到堆后，<wbr>它们也是被保护的。<br />2.&nbsp;&nbsp;&nbsp;&nbsp;const imports.<br /><br />在方法体内，块还可以引用O-C 实例变量，见&nbsp;&#8220;&nbsp;对象和块变量&nbsp;&#8221;.<br /><br />在块中使用变量有以下规则：<br />1.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;可访问在同一范围内的全局变量包括静态变量。<br />2.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;可以访问传递给块的参数（如同函数参数）。<br />3.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="background-color: #ff6666;">同一范围的栈（非static）<wbr>变量视作const变量。它们的值类似块表达式。嵌套块时，<wbr>从最近的作用域取值。</span><br />4.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="background-color: #ff6666;">在同一范围内声明的变量，如果有__<wbr>block修饰符修饰，则值是可变的。<wbr>在该范围内包括同一范围内的其他块对该变量的改变，<wbr>都将影响该作用域。</span>具体见&#8220;__block 存储类型&#8221;。<br />5.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;在块的范围内（块体）声明的本地变量，<wbr>类似于函数中的本地变量。<wbr>块的每次调用都会导致重新拷贝这些变量。<wbr>这些变量可作为const或参考（by-reference）<wbr>变量。<br /><br />下面演示本地非静态变量的使用：<br /><br />int x = 123;<br /><br />void (^printXAndY)(int) = ^(int y) {<br /><br />printf("%d %d/n", x, y);<br /><br />};<br /><br />printXAndY(456); // prints: 123 456<br /><br /><span style="background-color: #ffff99;">注意，试图向x进行赋值将导致错误：</span><br /><br />int x = 123;<br /><br />&nbsp;&nbsp;void (^printXAndY)(int) = ^(int y) {<br /><br />&nbsp;&nbsp;x = x + y;&nbsp;<span style="background-color: #ffff99;">// error</span><br /><br />printf("%d %d/n", x, y);<br /><br />};<br /><br />要想在块内改变x的值，需要使用__block修饰x。见&#8220;__<wbr>block存储类型&#8221;。</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><br />__block 存储类型<br /><br />你可以规定一个外部的变量是否可变&#8212;&#8212;可读写&#8212;&#8212;通过使用__<wbr>block存储类型修饰符。<span style="background-color: #ff6666;">__<wbr>block存储类似于register，<wbr>auto和static存储类型， 但是与它们互斥。</span><br /><br /><span style="background-color: #cccccc;">__block变量在变量声明的作用域、所有同一作用域内的块，<wbr>以及块拷贝之间同享存储。而且这个存储将在栈帧（stack frame）释放时得以保留，<wbr>只要同一帧内申明的块的拷贝仍然存活（例如，<wbr>被入栈以便再次使用）。<wbr>在指定作用域内的多个块能同时使用共享变量。</span><br /><br /><span style="background-color: #cccccc;">作为一种优化，块存储使用栈存储，就如同块自身一样。<wbr>如果使用Block_copy拷贝块（或者在O-<wbr>C向块发送copy消息），变量被拷贝到堆里。而且，__<wbr>block变量的地址随后就会改变。</span><br /><span style="background-color: #cccccc;"><br />__block变量有两个限制：不能是可变长度的数组，<wbr>也不能是包含C99可变长度数组的结构体。</span><br /><br />下面显示了__block变量的使用：<br /><br />__block int x = 123; //&nbsp;&nbsp;x lives in block storage<br /><br />void (^printXAndY)(int) = ^(int y) {<br /><br />x = x + y;<br /><br />printf("%d %d/n", x, y);<br /><br />};<br /><br />printXAndY(456); // prints: 579 456<br /><br />// x is now 579<br /><br />下面显示了在块中使用多种类型的变量：<br /><br />extern NSInteger CounterGlobal;<br /><br />static NSInteger CounterStatic;<br /><br />{<br /><br />NSInteger localCounter = 42;<br /><br />__block char localCharacter;<br /><br />void (^aBlock)(void) = ^(void) {<br /><br />++CounterGlobal;<br /><br />++CounterStatic;<br /><br />CounterGlobal = localCounter; // localCounter fixed at block creation<br /><br />localCharacter = 'a'; // sets localCharacter in enclosing scope<br /><br />};<br /><br />++localCounter; // unseen by the block<br /><br />localCharacter = 'b';<br /><br />aBlock(); // execute the block<br /><br />// localCharacter now 'a'<br /><br />}</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><br />对象和块变量<br /><br />块提供了对O-C和C++对象的支持 。</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><br />O-C对象<br /><br /><span style="background-color: #cccccc;">在引用计数的情况下，当你在块中引用一个O-C对象，<wbr>对象会被retained。<wbr>甚至只是简单引用这个对象的实例变量，也是一样的。</span><br /><br /><span style="background-color: #cccccc;">但对于__block标记的对象变量，就不一样了。</span><br /><br />注意：在垃圾回收的情况下，如果同时用__weak和__<wbr>block修饰变量，块可能不一定保证它是可用的。<br /><br />如果在方法体中使用块，对象实例变量的内存管理规则&nbsp;比较微妙：<br /><br /><span style="background-color: #cccccc;">&#9642;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;如果通过对象引用方式访问实例变量，<wbr>self&nbsp;被&nbsp;retained；<br />&#9642;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;如果通过值引用方式访问实例变量，<wbr>变量是retained;</span><br /><br />下面代码演示了这2种情况：<br /><br />dispatch_async(queue, ^{<br /><br />// instanceVariable is used by reference, self is retained<br /><br />doSomethingWithObject(<wbr>instanceVariable);<br /><br />});<br /><br />id localVariable = instanceVariable;<br /><br />dispatch_async(queue, ^{<br /><br />// localVariable is used by value, localVariable is retained (not self)<br /><br />doSomethingWithObject(<wbr>localVariable);<br /><br />});</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><br />C++ 对象<br /><br />一般，可以在块中使用C++对象。<wbr>在成员函数中对成员变量进行引用，俨然是对指针的引用，<wbr>可以对其进行改变。如果块被拷贝，有两种结果：<br /><br />如果有__block存储类型的类，该类是基于栈的C++对象，<wbr>通常会使用复制构造函数；<br /><br />如果使用了其他块中的基于栈的C++对象，<wbr>它必需有一个const的复制构造函数。该C++<wbr>对象使用该构造函数进行拷贝。<br /><br />块<br /><br />拷贝块时，其引用的其它块可能也被拷贝（从顶部开始）。<wbr>如果有块变量，并且在这个块中引用了一个块，那个块也会被拷贝。<br /><br />拷贝一个基于栈的块时，你得到的是新的块。<wbr>拷贝一个基于堆的块时，只是简单的增加了retain数，<wbr>然后把copy方法/函数的结果返回这个块。<br /><br />&nbsp;<br />&nbsp;<br /><br />使用块<br /><br />块的调用<br /><br />如果把块申明为变量，可以把它当成函数使用，例如：<br /><br />int (^oneFrom)(int) = ^(int anInt) {<br /><br />return anInt - 1;<br /><br />};<br /><br />printf("1 from 10 is %d", oneFrom(10));<br /><br />// Prints "1 from 10 is 9"<br /><br />float (^distanceTraveled) (float, float, float) =<br /><br />^(float startingSpeed, float acceleration, float time) {<br /><br />float distance = (startingSpeed * time) + (0.5 * acceleration * time * time);<br /><br />return distance;<br /><br />};<br /><br />float howFar = distanceTraveled(0.0, 9.8, 1.0);<br /><br />// howFar = 4.9<br /><br />但时常会将块以参数形式传递给一个函数或方法，这样，<wbr>就会使用行内（inline）块。</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><br />把块作为函数参数<br /><br />在这种情况下，不需要块申明。<wbr>简单地在需要把它作为参数的地方实现它就行。如下所示，<wbr>gsort_b是一个类似标准gsort_r的函数，<wbr>它的最后一个参数使用了块。<br /><br />char *myCharacters[3] = { "TomJohn", "George", "Charles Condomine" };<br /><br />qsort_b(myCharacters, 3, sizeof(char *), ^(const void *l, const void *r) {<br /><br />char *left = *(char **)l;<br /><br />char *right = *(char **)r;<br /><br />return strncmp(left, right, 1);<br /><br />});<br /><br />// Block implementation ends at "}"<br /><br />&nbsp;<br /><br />&nbsp;<br /><br />// myCharacters is now { "Charles Condomine", "George", TomJohn" }<br /><br />注意，块包含在函数的参数列表中。<br /><br />接下来的例子显示如何在dispath_<wbr>apply函数中使用块。dispatch_<wbr>apply的声明是：<br /><br />void dispatch_apply(size_t iterations, dispatch_queue_t queue, void (^block)(size_t));<br /><br />这个函数把块提交给dispatch队列以进行调用。<wbr>它有3个参数：要操作的次数；块被提交到的队列；块&#8212;&#8212;<wbr>这个块有一个参数&#8212;&#8212;遍历操作的当前次数。<br /><br />可以用dispatch_<wbr>apply简单地打印出遍历操作的索引：<br /><br />#include &lt;dispatch/dispatch.h&gt;<br /><br />size_t count = 10;<br /><br />dispatch_queue_t queue = dispatch_get_global_queue(<wbr>DISPATCH_QUEUE_PRIORITY_<wbr>DEFAULT, 0);<br /><br />dispatch_apply(count, queue, ^(size_t i) {<br /><br />printf("%u/n", i);<br /><br />});</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><br />把块作为参数使用<br /><br />Cocoa提供了大量使用块的方法。<wbr>把块作为参数使用与使用其他类型的参数并无不同。<br /><br />以下代码判断数组中前5个元素中含有给定filter集合的索引<wbr>。<br /><br />NSArray *array = [NSArray arrayWithObjects: @"A", @"B", @"C", @"A", @"B", @"Z",@"G", @"are", @"Q", nil];<br /><br />NSSet *filterSet = [NSSet setWithObjects: @"A", @"Z", @"Q", nil];<br /><br />BOOL (^test)(id obj, NSUInteger idx, BOOL *stop);<br /><br />test = ^ (id obj, NSUInteger idx, BOOL *stop) {<br /><br />if (idx &lt; 5) {<br /><br />if ([filterSet containsObject: obj]) {<br /><br />return YES;<br /><br />}<br /><br />}<br /><br />return NO;<br /><br />};<br /><br />NSIndexSet *indexes = [array indexesOfObjectsPassingTest:<wbr>test];<br /><br />NSLog(@"indexes: %@", indexes);<br /><br />/*Output:<br /><br />indexes: &lt;NSIndexSet: 0x10236f0&gt;[number of indexes: 2 (in 2 ranges), indexes: (0 3)]<br /><br />*/<br /><br />以下代码判断一个NSSet对象中是否包含指定的本地变量，<wbr>如果是的话把另一个本地变量(found)设置为YES（<wbr>并停止搜索）。注意found被声明为__block变量，<wbr>块是在行内声明的：<br /><br />__block BOOL found = NO;<br /><br />NSSet *aSet = [NSSet setWithObjects: @"Alpha", @"Beta", @"Gamma", @"X", nil];<br /><br />NSString *string = @"gamma";<br /><br />[aSet enumerateObjectsUsingBlock:^(<wbr>id obj, BOOL *stop) {<br /><br />&nbsp;&nbsp;if ([obj localizedCaseInsensitiveCompar<wbr>e:string] ==NSOrderedSame) {<br /><br />*stop = YES;<br /><br />found = YES;<br /><br />&nbsp;&nbsp;}<br /><br />}];<br /><br />// At this point, found == YES<br /><p style="margin: 0px; line-height: 26px; color: #333333; font-family: Arial;">&nbsp;</p><br />块复制<br /><br />一般，你不需要复制块。<wbr>只有当你希望在这个块申明的范围外使用它时需要复制它。<wbr>复制将导致块移动到堆中。<br /><br />可以使用C函数释放和复制块。<br /><br />Block_copy();<br /><br />Block_release();<br /><br />对于O-C，则可向块发送copy，<wbr>retain和release（以及autorelease）<wbr>消息。<br /><br />为避免内存泄露，一个Block_copy()<wbr>总是对应一个Block_release()。每个copy/<wbr>retain总是有对应的release（<wbr>或autorelease）&#8212;&#8212;使用垃圾回收则例外。</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><br />避免的用法<br /><br /><span style="background-color: #ff6666;">一个块声明（即^{&#8230;}）是一个本地栈式数据结构（stack-<wbr>local data structure）的地址，这个地址就代表了块。<wbr>本地栈式数据结构是{}围住的复合语句，因此应该避免如下用法：</span><br /><br />void dontDoThis() {<br /><br />void (^blockArray[3])(void);// array of 3 block&nbsp;&nbsp;&nbsp;references<br /><br />&nbsp;&nbsp;for (int i = 0; i &lt; 3; ++i) {<br /><br />blockArray[i] = ^{ printf("hello, %d/n", i); };<br /><br />// WRONG: The block literal scope is the "for" loop<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />}<br /><br />void dontDoThisEither() {<br /><br />&nbsp;<br /><br />&nbsp;&nbsp;void (^block)(void);<br /><br />&nbsp;&nbsp;int i = random():<br /><br />&nbsp;&nbsp;if (i &gt; 1000) {<br /><br />&nbsp;&nbsp;block = ^{ printf("got i at: %d/n", i); };<br /><br />// WRONG: The block literal scope is the "then" clause<br /><br />}<br /><br />// ...<br /><br />}</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><br />调试<br /><br />可以在块内设置断点，并进行单步调试。在GDB会话中，<wbr>使用invoke-block调用块，比如：<br /><br />$ invoke-block myBlock 10 20<br /><br />如果需要传递C字符串，必需用双引号把它引住。例如，<wbr>向doSomethignWithString块传递一个字符串<wbr>：<br /><br />$ invoke-block doSomethingWithString "/"this string/""<br /><br />&nbsp;<br /><br />完</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">＋＋＋＋＋</div><img src ="http://www.cppblog.com/walkklookk/aggbug/196777.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-29 10:57 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/29/196777.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>原：printf格式化字符串说明</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/29/196776.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sat, 29 Dec 2012 02:56:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/29/196776.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196776.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/29/196776.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196776.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196776.html</trackback:ping><description><![CDATA[<span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">创建：2012.04.11</span><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">［自：NSString的格式说明比这个要多不少。<wbr>但是printf是基础，它们都遵守。］</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">参考：<a href="http://www.cplusplus.com/reference/clibrary/cstdio/printf/" target="_blank" style="color: #1155cc;">http://www.cplusplus.com/<wbr>reference/clibrary/cstdio/<wbr>printf/</a></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">格式：<span style="background-color: #ff6666;"><tt>%[flags][width][.precision]<wbr>[length]specifier</tt>&nbsp;</span></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="font-size: 12px; font-family: verdana, arial, helvetica, sans-serif;"><br /></span></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="font-family: monospace;">++specifier++</span></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="font-size: 12px; font-family: monospace;">specifier：代表值的类型</span></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="font-size: 12px; font-family: monospace;"><br /></span></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><div><span style="font-size: 12px;">specifier<span style="white-space: pre-wrap;">	</span>Output<span style="white-space: pre-wrap;">	</span>Example</span></div><div><span style="font-size: 12px;"><br /></span></div><div><span style="font-size: 12px;"><br /></span></div><div><span style="font-size: 12px;">整数</span></div><div><span style="font-size: 12px;">d或i<span style="white-space: pre-wrap;">	</span>有符号的十进制数字<span style="white-space: pre-wrap;">	</span>392</span></div><div><span style="font-size: 12px;">u<span style="white-space: pre-wrap;">	</span>无符号的十进制数字<span style="white-space: pre-wrap;">	</span>7235</span></div><div><span style="font-size: 12px;">o<span style="white-space: pre-wrap;">	</span>无符号的八进制数字<span style="white-space: pre-wrap;">	</span>610</span></div><div><span style="font-size: 12px;">x<span style="white-space: pre-wrap;">	</span>无符号的十六进制数字，并以小写abcdef表示<span style="white-space: pre-wrap;">	</span>7fa</span></div><div><span style="font-size: 12px;">X<span style="white-space: pre-wrap;">	</span>无符号的十六进制数字，并以大写ABCDEF表示<span style="white-space: pre-wrap;">	</span>7FA</span></div><div><span style="font-size: 12px;">f<span style="white-space: pre-wrap;">	</span>浮点型的参数会被转成十进制数字［自：取到小数点以下六位，<wbr>四舍五入］<span style="white-space: pre-wrap;">	</span>392.65</span></div><div><span style="font-size: 12px;">e<span style="white-space: pre-wrap;">	</span>浮点型的参数以指数形式打印，有一个数字会在小数点前，<wbr>六位数字在小数点后，而在指数部分会以小写的e来表示。<span style="white-space: pre-wrap;">	</span>3.9265e+2</span></div><div><span style="font-size: 12px;">E<span style="white-space: pre-wrap;">	</span>与％e作用相同，唯一区别是指数部分将以大写的E 来表示。<span style="white-space: pre-wrap;">	</span>3.9265E+2</span></div><div><span style="font-size: 12px;">g<span style="white-space: pre-wrap;">	</span>浮点型的参数会自动选择以％f 或％e 的格式来打印，取位数较少的那个。<span style="white-space: pre-wrap;">	</span>392.65</span></div><div><span style="font-size: 12px;">G<span style="white-space: pre-wrap;">	</span>与％g 作用相同，唯一区别在以指数形态打印时会选择％E 格式。<span style="white-space: pre-wrap;">	</span>392.65</span></div><div><span style="font-size: 12px;"><br /></span></div><div><span style="font-size: 12px;">字符及字符串</span></div><div><span style="font-size: 12px;">c<span style="white-space: pre-wrap;">	</span>字符<span style="white-space: pre-wrap;">	</span>a</span></div><div><span style="font-size: 12px;">s<span style="white-space: pre-wrap;">	</span>字符串［自：指向字符串的参数会被逐字输出，<wbr>直到出现NULL字符为止］<span style="white-space: pre-wrap;">	</span>sample</span></div><div><span style="font-size: 12px;">p<span style="white-space: pre-wrap;">	</span>指针地址。<span style="white-space: pre-wrap;">	</span>B800:0000</span></div><div><span style="font-size: 12px;"><br /></span></div><div><span style="font-size: 12px;">n<span style="white-space: pre-wrap;">	</span>Nothing printed. The argument must be a pointer to a signed int, where the number of characters written so far is stored.<span style="white-space: pre-wrap;">	</span></span></div><div><span style="font-size: 12px;">%<span style="white-space: pre-wrap;">	</span>仅用于打印%，即%%。</span></div><div><span style="font-size: 12px;"><br /></span></div><div><span style="font-family: monospace;">++flags, width, .precision 和 length++</span></div><div><span style="font-family: monospace; font-size: 12px;"><br /></span></div><div><span style="font-size: 12px;">flags, width, .precision 和 length是可选参数。</span></div><div><span style="font-size: 12px;"><br /></span></div><div><span style="font-family: monospace;">++flags++</span></div><div><span style="font-size: 12px;">flags<span style="white-space: pre-wrap;">	</span>description</span></div><div><span style="font-size: 12px;">-<span style="white-space: pre-wrap;">	</span>左对齐，默认是右对齐 (参照 width 标志符).</span></div><div><span style="font-size: 12px;">+<span style="white-space: pre-wrap;">	</span>一般在打印负数时，printf（）会加印一个负号，<wbr>整数则不加任何负号。此旗标会使得在打印正数前多一个正号（+）<wbr>。［自：注意，这个符号和－号不是相反的含义，<wbr>而是代表着完全不同的领域的意思。］</span></div><div><span style="font-size: 12px;">空格 若符号为正，则显示空格，负则显示"-"。</span></div><div><span style="font-size: 12px;">#<span style="white-space: pre-wrap;">	</span>此旗标会根据其后转换字符的不同而有不同含义。对于o, x 或 X 类型： 值前面会分别被强制打印出 0, 0x or 0X［自：例如"%#x"则19打印为0x13］［自：<wbr>注意对于o，会打印出0，而不是o。］</span></div><div><span style="font-size: 12px;">对于e, E and f类型：会强制打印出小数点。默认情况下，如果没有小数部分，<wbr>则不会打印小数点。</span></div><div><span style="font-size: 12px;">对于g or G类型：于e和E相同，但是不会去掉尾部的小数点。</span></div><div><span style="font-size: 12px;">0<span style="white-space: pre-wrap;">	</span>当左对齐时，不足的宽度（位数，空间）用0补齐，默认是用空格。<wbr>前提是指定了最小位数，即width.</span></div><div><span style="font-size: 12px;"><br /></span></div><div>++<span style="font-family: monospace;">width++</span></div><div><span style="font-size: 12px;">width<span style="white-space: pre-wrap;">	</span>description</span></div><div><span style="font-size: 12px;">数字<span style="white-space: pre-wrap;">	</span>最小的宽度（位数，空间）。如果值小于这个位数，则用空格补齐。<wbr>如果值大于这个数值，则也不会被截断。</span></div><div><span style="font-size: 12px;">*<span style="white-space: pre-wrap;">	</span>表示以下一个参数当做参数长度。［自：用于指定动态的宽度。］</span></div><div><span style="font-size: 12px;"><br /></span></div><div><span style="font-size: 12px;"><br /></span></div><div>++<span style="font-family: monospace;">precision++</span></div><div><span style="font-size: 12px;">.precision<span style="white-space: pre-wrap;">	</span>description</span></div><div><span style="font-size: 12px;">.number<span style="white-space: pre-wrap;">	</span>对于整数 (d, i, o, u, x, X): precision 表示值的最少位数。如果值的位数小于这个宽度，则用0补齐。<wbr>如果大于，则也不会被截断。如果是precision是，<wbr>意味着如果值是0的时候，什么有不显示。</span></div><div><span style="font-size: 12px;">对于e, E and f：表示小数点后面的位数。</span></div><div><span style="font-size: 12px;">对于g and G :表示数值的有效位数的最大位数。［自：不太明白］</span></div><div><span style="font-size: 12px;">对于s: 表示打印字符串的最长长度。默认情况下会一直打印，<wbr>直到遭遇null标志符。</span></div><div><span style="font-size: 12px;">对于c: 没有效果。</span></div><div><span style="font-size: 12px;">如果没有指定precision, 默认是1。 如果没有指定period则假定是0。［自：不太明白］</span></div><div><span style="font-size: 12px;">.*<span style="white-space: pre-wrap;">	</span>表示以下一个参数当做precision。［自：<wbr>用于指定动态的precision。］</span></div><div><span style="font-size: 12px;"><br /></span></div><div><span style="font-size: 12px;"><br /></span></div><div>++<span style="font-family: monospace;">length++</span></div><div><span style="font-size: 12px;">length<span style="white-space: pre-wrap;">	</span>description</span></div><div><span style="font-size: 12px;">h<span style="white-space: pre-wrap;">	</span>表示参数是short int或者unsigned short int (只对整型有效: i, d, o, u, x and X).</span></div><div><span style="font-size: 12px;">l<span style="white-space: pre-wrap;">	</span>对于 (i, d, o, u, x and X)：表示参数是long int或者unsigned long int。对于c and s：表示是宽字符或字符串。</span></div><div><span style="font-size: 12px;">L<span style="white-space: pre-wrap;">	</span>只对浮点型e, E, f, g and G有效：参数是long double类型.</span></div><div style="font-family: monospace; font-size: 12px;"></div></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="font-size: 12px; font-family: monospace;"><br /></span></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><span style="font-size: 12px; font-family: monospace;">＋＋＋＋＋</span></div><img src ="http://www.cppblog.com/walkklookk/aggbug/196776.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-29 10:56 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/29/196776.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>原：关于时间NSDate, NSCalendar, NSDateComponents --2011.11.06</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/29/196774.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sat, 29 Dec 2012 02:56:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/29/196774.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196774.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/29/196774.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196774.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196774.html</trackback:ping><description><![CDATA[<span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">创建：2011.09.15</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">修改：2011.10.18</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">修改：2011.11.06</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">NSDate对象相当于一个时间点，是绝对的。</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">NSDate考虑事情都是通过转换为NSTimeInterva<wbr>l来进行的.</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">==========</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">NSDate objects store dates in absolute time. For example, the date object created in Listing 16 represents 4:00 PM CDT, 5:00 EDT, and so on.</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">Listing 16&nbsp; Creating a date from components using a specific time zone</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">NSCalendar *gregorian=[[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">[gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"<wbr>CDT"]];</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">NSDateComponents *timeZoneComps=[[<wbr>NSDateComponents alloc] init];</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">[timeZoneComps setHour:16];</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">//specify whatever day, month, and year is appropriate</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">NSDate *date=[gregorian dateFromComponents:<wbr>timeZoneComps];</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">If you need to create a date that is independent of timezone, you can store the date as an NSDateComponents object&#8212;as long as you store some reference to the corresponding calendar.</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">NSDate使用的是绝对的时间。</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">NSDateComponents也是绝对的时间。</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">NSCalendar与TimeZone相关，<wbr>它会根据TimeZone解释NSDateComponents<wbr>的意义。比如例子中，[timeZoneComps setHour:16];此时，<wbr>经过NSCalendar的解释，它就变成了CDT（<wbr>美国中部时间）的下午4点钟，也就是5:00 EDT（美国东部时间）.</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">==========</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">In iOS, NSDateComponents objects can contain a calendar, a timezone, and a date object. You can therefore store the calendar along with the components. If you use the date method of the NSDateComponents class to access the date, make sure that the associated timezone is up-to-date.</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">在iOS中，NSDateComponents并没有很绝对，<wbr>它绑定了a calendar, a timezone, and a date object。因此，使用的时候，<wbr>要确保正确的TimeZone. ［自：这个不熟，暂时不考虑。］</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><div style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">===========<br /><div>NSDateComponents可以认为是一个保存了int year, int month, int day, int hour, int minute, int second变量的一个对象。<br />它没有直接保存为一个date对象。<wbr>当需要获取NSDate的时候，才动态的计算得到。<br />并且，每个变量都可以是正数，也可以是负数和0。<br />但是，不同的变量对于0的解释却不同。对于year，<wbr>month，day它们不可以为0，因此，一旦设置为0之后，<wbr>显示的是上一个对应单位的时间。但是对于hour，<wbr>minute和second却可以为0，只有当设置为－1时，<wbr>才会显示上一个对应单位的时间。<br />正常数值如下：<br />year:1~Max<br />month:1~12<br />day:1~365/366<br />hour:0~23<br />minute:0~59<br />second:0~59<br /><br />测试代码如下：<br />假设当前时间为：2010－10－10 10：10：10<br /><br />===========</div></div><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">常用代码：</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">计算Date偏移：</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><div style="color: #222222; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; background-color: #eeeeee; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 1159px;">&nbsp;&nbsp;&nbsp;&nbsp;NSCalendar&nbsp;*cal&nbsp;=&nbsp;[[<wbr>NSCalendar&nbsp;alloc]&nbsp;<wbr>initWithCalendarIdentifier:<wbr>NSGregorianCalendar];<br />&nbsp;&nbsp;&nbsp;&nbsp;[cal&nbsp;setTimeZone:[<wbr>NSTimeZone&nbsp;timeZoneWithName:@"<span style="background-color: #ff99cc;"><wbr>America/New_York</span>"]];<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;unitFlags&nbsp;=&nbsp;<wbr>NSYearCalendarUnit&nbsp;|&nbsp;<wbr>NSMonthCalendarUnit&nbsp;|&nbsp;<wbr>NSDayCalendarUnit&nbsp;|&nbsp;<wbr>NSHourCalendarUnit&nbsp;|&nbsp;<wbr>NSMinuteCalendarUnit&nbsp;|&nbsp;<wbr>NSSecondCalendarUnit;<br />&nbsp;&nbsp;&nbsp;&nbsp;NSDateComponents&nbsp;*comps&nbsp;=&nbsp;<wbr>[cal&nbsp;components:unitFlags&nbsp;<wbr>fromDate:date];<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;[comps&nbsp;setMonth:([comps&nbsp;<wbr>month]&nbsp;-&nbsp;1)];<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[comps&nbsp;setDay:0];<br />&nbsp;&nbsp;&nbsp;&nbsp;[comps&nbsp;setHour:0];<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;NSDate&nbsp;*resDate&nbsp;&nbsp;=&nbsp;[cal&nbsp;<wbr>dateFromComponents:comps];<br />&nbsp;&nbsp; [cal release];</div><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">通过NSString得到NSDate：</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><div style="color: #222222; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; background-color: #eeeeee; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 1159px; word-break: break-all;">&nbsp;&nbsp;&nbsp;&nbsp;NSDateFormatter&nbsp;*<wbr>formatter&nbsp;=&nbsp;[[NSDateFormatter&nbsp;<wbr>alloc]&nbsp;init];<br />&nbsp;&nbsp;&nbsp;&nbsp;[formatter&nbsp;setDateFormat:@<wbr>"yyyy-MM-dd&nbsp;HH:mm&nbsp;<span style="background-color: #ff99cc;">zzzz</span>"];<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;NSDate&nbsp;*dateFormString&nbsp;=&nbsp;[<wbr>formatter&nbsp;dateFromString:@"<wbr>2011-08-31&nbsp;05:00&nbsp;<span style="background-color: #ff99cc;">-</span><span style="background-color: #ff99cc;">0400</span>"];</div><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">通过NSDate得到NSString：</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><div style="color: #222222; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; background-color: #eeeeee; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 1159px; word-break: break-all;">&nbsp;&nbsp;&nbsp;&nbsp;NSDate&nbsp;*date&nbsp;=&nbsp;[NSDate&nbsp;<wbr>date];<br />&nbsp;&nbsp;&nbsp;&nbsp;NSDateFormatter&nbsp;*<wbr>formatter&nbsp;=&nbsp;[[NSDateFormatter&nbsp;<wbr>alloc]&nbsp;init];<br />&nbsp;&nbsp;&nbsp;&nbsp;[formatter&nbsp;setTimeZone:[<wbr>NSTimeZone&nbsp;timeZoneWithName:@"<wbr>America/New_York"]];<br />&nbsp;&nbsp;&nbsp;&nbsp;[formatter&nbsp;setDateFormat:@<wbr>"MM/dd/yyyy&nbsp;hh:mma"];<br />&nbsp;&nbsp;&nbsp;&nbsp;NSString&nbsp;*dateStr&nbsp;=&nbsp;[<wbr>NSString&nbsp;stringWithFormat:@"%@<wbr>",&nbsp;[formatter&nbsp;stringFromDate:<wbr>date]];</div><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">格林尼治标准时间的时区可以认为是：GMT。即-0000或+<wbr>0000.</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; background-color: #ff99cc;">[cal&nbsp;setTimeZone:[NSTimeZone&nbsp;<wbr>timeZoneWithName:</span><span style="color: #222222; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; background-color: #ff99cc;">@"</span><span style="color: #222222; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; background-color: #ffcc99;">GMT</span><span style="color: #222222; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; background-color: #ff99cc;">"</span><span style="color: #222222; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; background-color: #ff99cc;">]];</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">中国时区横跨东五区、东六区、东七区、东八区、<wbr>东九区等五个地理时区。<wbr>中国大陆所有区域使用的标准时间均为东八区（UTC+8）(+<wbr>0800)时间，比协调世界时或格林尼治标准时间快8小时。</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">美国本土横跨西五区至西十区，共六个时区，<wbr>每个时区对应一个标准时间。从东向西分别为东部时间(EST)(<wbr>西五区时间-0500)、中部时间(CST) (西六区时间-0600)、山地时间(MST)(西七区时间)、<wbr>太平洋时间(西部时间)(PST)(西八区时间)、<wbr>阿拉斯加时间(AKST)(西九区时 间)和夏威夷时间(HST)(西十区时间)，按照&#8220;东早西晚&#8221;<wbr>的规律，各递减一小时。</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">美国从每年3月的第二个星期日至11月的第一个星期日采用夏令时<wbr>，夏令时比正常时间早一小时。</span><span style="color: #222222; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; background-color: #ff99cc;">当处于夏令时时，<wbr>相当于向东移动了一个时区，此时东部时间(EST)(<wbr>西五区时间)可以认为从-0500变成了-0400，依次类推。</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">时区的计算：</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">要计算的区时=已知区时-(已知区时的时区-要计算区时的时区)<wbr>，(注:东时区为正，西时区为负)。&nbsp;</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">北京(东八区)时间为5月10日12:00</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">东京(东九区)时间为5月10日13:00&nbsp; =12:00-(8-9)=13:00</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">伦敦(中时区)时间为5月10日4:00&nbsp;&nbsp;&nbsp; =12:00-(8-0)=4:00</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">纽约(西五区)时间为5月9日23:00&nbsp;&nbsp;&nbsp; =12:00-[8-(-5)]=-1:00+24:00-<wbr>1天=23:00</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><div style="color: #222222; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19px; border: 1px solid #cccccc; padding: 4px 5px 4px 4px; width: 1159px; word-break: break-all; background-color: #eeeeee;">&nbsp; &nbsp; NSCalendar&nbsp;*cal&nbsp;=&nbsp;[NSCalendar&nbsp;<wbr>currentCalendar];<br />&nbsp;&nbsp;&nbsp;&nbsp;[cal&nbsp;setTimeZone:[<wbr>NSTimeZone&nbsp;timeZoneWithName:@"<wbr>GMT"]];<br />&nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;unitFlags&nbsp;=&nbsp;<wbr>NSYearCalendarUnit&nbsp;|&nbsp;<wbr>NSMonthCalendarUnit&nbsp;|<br />&nbsp;&nbsp;&nbsp;&nbsp;NSDayCalendarUnit&nbsp;|&nbsp;<wbr>NSHourCalendarUnit&nbsp;|<br />&nbsp;&nbsp;&nbsp;&nbsp;NSMinuteCalendarUnit&nbsp;|&nbsp;<wbr>NSSecondCalendarUnit;<br />&nbsp;&nbsp;&nbsp;&nbsp;NSDateFormatter&nbsp;*f&nbsp;=&nbsp;[[<wbr>NSDateFormatter&nbsp;alloc]&nbsp;init];<br />&nbsp;&nbsp;&nbsp;&nbsp;[f&nbsp;setDateFormat:@"yyyy-<wbr>MM-dd&nbsp;hh:mm:ss"];<br />&nbsp;&nbsp;&nbsp;&nbsp;NSDate&nbsp;*d&nbsp;=&nbsp;[f&nbsp;<wbr>dateFromString:@"2010-10-10&nbsp;<wbr>10:10:10"];<br />&nbsp;&nbsp;&nbsp;&nbsp;NSDateComponents&nbsp;*comps&nbsp;=&nbsp;<wbr>[cal&nbsp;components:unitFlags&nbsp;<wbr>fromDate:d];<br />&nbsp;&nbsp;&nbsp;&nbsp;NSDate&nbsp;*theStartDate&nbsp;&nbsp;=&nbsp;[<wbr>cal&nbsp;dateFromComponents:comps];<br />&nbsp;&nbsp;&nbsp;&nbsp;NSLog(@"%@&nbsp;Src",&nbsp;<wbr>theStartDate);<br />&nbsp;&nbsp;&nbsp;&nbsp;[comps&nbsp;setYear:1];<br />&nbsp;&nbsp;&nbsp;&nbsp;theStartDate&nbsp;&nbsp;=&nbsp;[cal&nbsp;<wbr>dateFromComponents:comps];<br />&nbsp;&nbsp;&nbsp;&nbsp;NSLog(@"%@&nbsp;setYear:1",&nbsp;<wbr>theStartDate);&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;comps&nbsp;=&nbsp;[cal&nbsp;components:<wbr>unitFlags&nbsp;fromDate:d];<br />&nbsp;&nbsp;&nbsp;&nbsp;[comps&nbsp;setYear:0];<br />&nbsp;&nbsp;&nbsp;&nbsp;theStartDate&nbsp;&nbsp;=&nbsp;[cal&nbsp;<wbr>dateFromComponents:comps];<br />&nbsp;&nbsp;&nbsp;&nbsp;NSLog(@"%@&nbsp;setYear:0"&nbsp;,&nbsp;<wbr>theStartDate);<br />&nbsp;&nbsp;&nbsp;&nbsp;comps&nbsp;=&nbsp;[cal&nbsp;components:<wbr>unitFlags&nbsp;fromDate:d];<br />&nbsp;&nbsp;&nbsp;&nbsp;[comps&nbsp;setYear:-1];<br />&nbsp;&nbsp;&nbsp;&nbsp;theStartDate&nbsp;&nbsp;=&nbsp;[cal&nbsp;<wbr>dateFromComponents:comps];<br />&nbsp;&nbsp;&nbsp;&nbsp;NSLog(@"%@&nbsp;setYear:-1",&nbsp;<wbr>theStartDate);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;comps&nbsp;=&nbsp;[cal&nbsp;components:<wbr>unitFlags&nbsp;fromDate:d];<br />&nbsp;&nbsp;&nbsp;&nbsp;[comps&nbsp;setMonth:1];<br />&nbsp;&nbsp;&nbsp;&nbsp;theStartDate&nbsp;&nbsp;=&nbsp;[cal&nbsp;<wbr>dateFromComponents:comps];<br />&nbsp;&nbsp;&nbsp;&nbsp;NSLog(@"%@&nbsp;setMonth:1",&nbsp;<wbr>theStartDate);<br />&nbsp;&nbsp;&nbsp;&nbsp;comps&nbsp;=&nbsp;[cal&nbsp;components:<wbr>unitFlags&nbsp;fromDate:d];<br />&nbsp;&nbsp;&nbsp;&nbsp;[comps&nbsp;setMonth:0];<br />&nbsp;&nbsp;&nbsp;&nbsp;theStartDate&nbsp;&nbsp;=&nbsp;[cal&nbsp;<wbr>dateFromComponents:comps];&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;NSLog(@"%@&nbsp;setMonth:0",&nbsp;<wbr>theStartDate);<br />&nbsp;&nbsp;&nbsp;&nbsp;comps&nbsp;=&nbsp;[cal&nbsp;components:<wbr>unitFlags&nbsp;fromDate:d];<br />&nbsp;&nbsp;&nbsp;&nbsp;[comps&nbsp;setMonth:-1];<br />&nbsp;&nbsp;&nbsp;&nbsp;theStartDate&nbsp;&nbsp;=&nbsp;[cal&nbsp;<wbr>dateFromComponents:comps];&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;NSLog(@"%@&nbsp;setMonth:-1",&nbsp;<wbr>theStartDate);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;comps&nbsp;=&nbsp;[cal&nbsp;components:<wbr>unitFlags&nbsp;fromDate:d];<br />&nbsp;&nbsp;&nbsp;&nbsp;[comps&nbsp;setDay:1];<br />&nbsp;&nbsp;&nbsp;&nbsp;theStartDate&nbsp;&nbsp;=&nbsp;[cal&nbsp;<wbr>dateFromComponents:comps];&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;NSLog(@"%@&nbsp;setDay:1",&nbsp;<wbr>theStartDate);<br />&nbsp;&nbsp;&nbsp;&nbsp;comps&nbsp;=&nbsp;[cal&nbsp;components:<wbr>unitFlags&nbsp;fromDate:d];<br />&nbsp;&nbsp;&nbsp;&nbsp;[comps&nbsp;setDay:0];<br />&nbsp;&nbsp;&nbsp;&nbsp;theStartDate&nbsp;&nbsp;=&nbsp;[cal&nbsp;<wbr>dateFromComponents:comps];&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;NSLog(@"%@&nbsp;setDay:0",&nbsp;<wbr>theStartDate);<br />&nbsp;&nbsp;&nbsp;&nbsp;comps&nbsp;=&nbsp;[cal&nbsp;components:<wbr>unitFlags&nbsp;fromDate:d];<br />&nbsp;&nbsp;&nbsp;&nbsp;[comps&nbsp;setDay:-1];<br />&nbsp;&nbsp;&nbsp;&nbsp;theStartDate&nbsp;&nbsp;=&nbsp;[cal&nbsp;<wbr>dateFromComponents:comps];&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;NSLog(@"%@&nbsp;setDay:-1",&nbsp;<wbr>theStartDate);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;comps&nbsp;=&nbsp;[cal&nbsp;components:<wbr>unitFlags&nbsp;fromDate:d];<br />&nbsp;&nbsp;&nbsp;&nbsp;[comps&nbsp;setHour:0];<br />&nbsp;&nbsp;&nbsp;&nbsp;theStartDate&nbsp;&nbsp;=&nbsp;[cal&nbsp;<wbr>dateFromComponents:comps];&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;NSLog(@"%@&nbsp;setHour:0",&nbsp;<wbr>theStartDate);<br />&nbsp;&nbsp;&nbsp;&nbsp;comps&nbsp;=&nbsp;[cal&nbsp;components:<wbr>unitFlags&nbsp;fromDate:d];<br />&nbsp;&nbsp;&nbsp;&nbsp;[comps&nbsp;setHour:24];<br />&nbsp;&nbsp;&nbsp;&nbsp;theStartDate&nbsp;&nbsp;=&nbsp;[cal&nbsp;<wbr>dateFromComponents:comps];&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;NSLog(@"%@&nbsp;setHour:24",&nbsp;<wbr>theStartDate);<br />&nbsp;&nbsp;&nbsp;&nbsp;comps&nbsp;=&nbsp;[cal&nbsp;components:<wbr>unitFlags&nbsp;fromDate:d];<br />&nbsp;&nbsp;&nbsp;&nbsp;[comps&nbsp;setHour:-1];<br />&nbsp;&nbsp;&nbsp;&nbsp;theStartDate&nbsp;&nbsp;=&nbsp;[cal&nbsp;<wbr>dateFromComponents:comps];&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;NSLog(@"%@&nbsp;setHour:-1",&nbsp;<wbr>theStartDate);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;comps&nbsp;=&nbsp;[cal&nbsp;components:<wbr>unitFlags&nbsp;fromDate:d];<br />&nbsp;&nbsp;&nbsp;&nbsp;[comps&nbsp;setMinute:0];<br />&nbsp;&nbsp;&nbsp;&nbsp;theStartDate&nbsp;&nbsp;=&nbsp;[cal&nbsp;<wbr>dateFromComponents:comps];&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;NSLog(@"%@&nbsp;setMinute:0",&nbsp;<wbr>theStartDate);<br />&nbsp;&nbsp;&nbsp;&nbsp;comps&nbsp;=&nbsp;[cal&nbsp;components:<wbr>unitFlags&nbsp;fromDate:d];<br />&nbsp;&nbsp;&nbsp;&nbsp;[comps&nbsp;setMinute:60];<br />&nbsp;&nbsp;&nbsp;&nbsp;theStartDate&nbsp;&nbsp;=&nbsp;[cal&nbsp;<wbr>dateFromComponents:comps];&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;NSLog(@"%@&nbsp;setMinute:60",&nbsp;<wbr>theStartDate);<br />&nbsp;&nbsp;&nbsp;&nbsp;comps&nbsp;=&nbsp;[cal&nbsp;components:<wbr>unitFlags&nbsp;fromDate:d];<br />&nbsp;&nbsp;&nbsp;&nbsp;[comps&nbsp;setMinute:-1];<br />&nbsp;&nbsp;&nbsp;&nbsp;theStartDate&nbsp;&nbsp;=&nbsp;[cal&nbsp;<wbr>dateFromComponents:comps];&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;NSLog(@"%@&nbsp;setMinute:-1",&nbsp;<wbr>theStartDate);</div><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">结果如下：</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><div style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;"><div>2010-10-10 14:10:10 +0000 Src</div><div>0001-10-12 14:10:10 +0000 setYear:1</div><div>0001-10-12 14:10:10 +0000 setYear:0</div><div>0002-10-12 14:10:10 +0000 setYear:-1</div><div>2010-01-10 14:10:10 +0000 setMonth:1</div><div>2009-12-10 14:10:10 +0000 setMonth:0&nbsp;// 设置0变成上一年的最后一个月了。</div><div>2009-11-10 14:10:10 +0000 setMonth:-1&nbsp;// 设置－1变成上一年的最后二个月了。</div><div>2010-10-01 14:10:10 +0000 setDay:1</div><div>2010-09-30 14:10:10 +0000 setDay:0&nbsp;// 设置0变成上一月的最后一天了。</div><div>2010-09-29 14:10:10 +0000 setDay:-1&nbsp;// 设置－1变成上一月的最后<span style="background-color: yellow;">二</span>天了。</div><div><span style="background-color: yellow;">// 以上的时间范围都是从0开始，以下的时间范围都是从1开始。<wbr>因此0和－1的概念都发生了变化（例如：－1的意义都变成了从&#8220;<wbr>二&#8221;变为&#8220;一&#8221;）。</span><br />2010-10-10 00:10:10 +0000 setHour:0</div><div>2010-10-11 00:10:10 +0000 setHour:24 &nbsp;// 超过24变成下一天了。</div><div>2010-10-09 23:10:10 +0000 setHour:-1&nbsp;// 设置－1变成上一天的最后<span style="background-color: yellow;">一</span>小时了。</div><div>2010-10-10 14:00:10 +0000 setMinute:0</div><div>2010-10-10 15:00:10 +0000 setMinute:60&nbsp;// 超过60变成下一小时了。</div><div>2010-10-10 13:59:10 +0000 setMinute:-1</div></div><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">＝＝＝＝＝</span><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">week是一年中的第几周，weekOfYear也是一年中的</span><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">第<wbr>几周。在</span><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">Gregorian日历中应该没有区别。<br /></span><div style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;"><p style="margin: 10px 0px;">weekday是该日期是这一周中第几天。范围是从1－7。<wbr>星期日是1。（其实，weekday要由日历的类型来解释的。<wbr>weekday返回的是该日历类型中，<wbr>比weekday大一个单位的单位中，该日期是排名第几。当然，<wbr>对于Gregorian来说，比</p><div style="display: inline-block;">weekday大一的单位就是月。但是，<wbr>对于其他日历来说也许不是这样的</div>）<p>&nbsp;</p><div><div><p style="margin: 10px 0px;">weekOfMonth是指该日期是本月的第几周。<wbr>一周从星期日开始。</p></div>weekdayOrdinal是指该日期是本月的第几个星期几。<wbr>有点混乱啊。结合weekOfMonth举个例子：<br />如果本月是从周二开始的，即1号是周二，2号是周三，<wbr>6号是周日，7号是周一，8号是第二个周二。<br />那么如果日期设定为1，2，3，4，5号的话，<wbr>则weekOfMonth和weekdayOrdinal都是1<wbr>。<br /><div>那么如果日期设定为6，7号的话，<wbr>则weekOfMonth是2，<wbr>但是weekdayOrdinal却是1，<wbr>因为6号虽然是第二周，但是却是本月第一个周日，<wbr>同样7号虽然也是第二周，但是却是本月第一个周一。</div><p style="margin: 10px 0px;"></p></div><p style="margin: 10px 0px;"></p></div><div style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">那么如果日期设定为8，9，10......号的话，<wbr>则weekOfMonth和weekdayOrdinal都是2<wbr>。</div><br style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;" /><span style="color: #222222; background-color: #ffffff; line-height: 19px; font-size: 13px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif;">＋＋＋＋＋</span><img src ="http://www.cppblog.com/walkklookk/aggbug/196774.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-29 10:56 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/29/196774.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>转：ios4与ios3兼容 -4 －－Tips &amp; Tricks for conditional iOS3, iOS3.2 and iOS4 code _iphone</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/29/196772.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sat, 29 Dec 2012 02:54:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/29/196772.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196772.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/29/196772.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196772.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196772.html</trackback:ping><description><![CDATA[<span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">转：</span><a href="http://cocoawithlove.com/2010/07/tips-tricks-for-conditional-ios3-ios32.html" target="_blank" style="color: #1155cc; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">http://cocoawithlove.com/<wbr>2010/07/tips-tricks-for-<wbr>conditional-ios3-ios32.html</a><br style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;" /><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><div><br /><span style="color: #3333ff;">[2012.5.18：</span></div><div><span style="color: #3333ff;">使用</span><span style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; line-height: 18px; margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;">[[</span><span style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; line-height: 18px; margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; color: #2b91af;">UIDevice</span><span style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; line-height: 18px; margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;">&nbsp;currentDevice</span><span style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; line-height: 18px; margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;">]</span><span style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; line-height: 18px; margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;">&nbsp;systemVersion</span><span style="font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; line-height: 18px; margin: 0px; padding: 0px; border: 0px; vertical-align: baseline;">];</span><span style="color: #3333ff;">更为靠谱。参考：</span></div><div><a href="http://stackoverflow.com/questions/3339722/check-iphone-ios-version/3339787#3339787" target="_blank" style="color: #1155cc;">http://stackoverflow.com/<wbr>questions/3339722/check-<wbr>iphone-ios-version/3339787#<wbr>3339787</a></div><div></div><div><p style="clear: both; vertical-align: baseline; line-height: 18px; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; margin: 0px 0px 1em; word-wrap: break-word; border: 0px; padding: 0px;">Apple uses&nbsp;<code style="margin: 0px; padding: 1px 5px; border: 0px; vertical-align: baseline; background-color: #eeeeee; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;">systemVersion</code>&nbsp;in their&nbsp;<a href="http://developer.apple.com/iphone/library/samplecode/GLSprite/Listings/Classes_EAGLView_m.html" rel="nofollow" target="_blank" style="color: #4a6b82; margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; text-decoration: initial;">GLSprite</a>&nbsp;sample code, so my recommendation can't be absolute:</p><pre style="white-space: pre-wrap; margin-top: 0px; margin-bottom: 10px; padding: 5px; border: 0px; vertical-align: baseline; background-color: #eeeeee; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; overflow: auto; width: auto; max-height: 600px; line-height: 18px;"><code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;"><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: gray;">// A system version of 3.1 or greater is required to use CADisplayLink. The NSTimer</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"><br /> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: gray;">// class is used as fallback when it isn't available.</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"><br /> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: #2b91af;">NSString</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">*</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">reqSysVer </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">=</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">@</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: #800000;">"3.1"</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">;</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"><br /> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: #2b91af;">NSString</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">*</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">currSysVer </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">=</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">[[</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: #2b91af;">UIDevice</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> currentDevice</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">]</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> systemVersion</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">];</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"><br /> </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: #00008b;">if</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">([</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">currSysVer compare</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">:</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">reqSysVer options</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">:</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: #2b91af;">NSNumericSearch</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">]</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">!=</span> <span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: #2b91af;">NSOrderedAscending</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">)</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"><br /> &nbsp; &nbsp; displayLinkSupported </span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">=</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> TRUE</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">;</span><span style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"><br /> </span></code></pre><p style="clear: both; vertical-align: baseline; line-height: 18px; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; margin: 0px 0px 1em; word-wrap: break-word; border: 0px; padding: 0px;"><strong style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">Important Note:</strong>&nbsp;If for whatever reason you decide that&nbsp;<code style="margin: 0px; padding: 1px 5px; border: 0px; vertical-align: baseline; background-color: #eeeeee; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;">systemVersion</code>&nbsp;is what you want, make sure to treat it as an string or you risk truncating the minor revision number (eg. 3.1.2 -&gt; 3.1).</p></div><div>不过这种方法对于小版本号码似乎不太好用。但是，<wbr>一般我们也不用细化到小版本号码吧。</div><div><span style="color: #3333ff;">]</span></div><div><span style="color: #3333ff;"><br /></span></div><div><span style="color: #3333ff;">［自：需要代理才能打开。］</span><br /><span style="color: #3333ff;">[自：经过测试，目前整理如下：</span><br style="color: #3333ff;" /><span style="color: #3333ff;">＊根据apple的声明，新的程序必须使用sdk 4编译。</span><br style="color: #3333ff;" /><span style="color: #3333ff;">＊当使用sdk4编译程序时，<wbr>同样可以将程序发布到ios3的机器中。<wbr>方法就是文中提到的更改工程选项"Base SDK"和"iPhone OS Deployment Target"，<wbr>其中后者是指定程序可以运行的程序的最小ios值。<wbr>如果这个值指定为ios3，<wbr>则虽然在xcode的device中的内容是ios4，<wbr>但是也是可以直接部署到ios3上的。</span><br style="color: #3333ff;" /><span style="color: #3333ff;">＊使用#if __IPHONE_OS_VERSION_MAX_<wbr>ALLOWED &gt;= 40000可以保证使用sdk4编译的程序一定可以执行到这里的<wbr>代码，但是使用sdk3（<wbr>需要使用旧版本的xcode才可以指定sdk3）<wbr>的代码则不会执行到这里。但是，由于apple声明，<wbr>必须使用ios4的代码，所以这个选项基本上是没有什么用处的，<wbr>因为一定要使用ios4，所以一定会走这个宏内部定义的代码的。<wbr>除非，你还要兼容旧版本的xcode。</span><br style="color: #3333ff;" /><span style="background-color: #cccccc; color: #3333ff;">＊使用if (<wbr>kCFCoreFoundationVersionNumber &gt;= kCFCoreFoundationVersionNumber<wbr>_iPhoneOS_4_0)<wbr>可以动态的判断当前的ios是3还是4。<wbr>这样可以根据情况使用不同sdk平台的代码。这个才是关键！！。</span><br style="color: #3333ff;" /><span style="color: #3333ff;">＊作者给出的</span><br style="color: #3333ff;" /><span style="color: #3333ff;">IF_IOS4_OR_GREATER其实没有什么用。<wbr>因为作为__IPHONE_OS_VERSION_MAX_<wbr>ALLOWED的判断是多余的。</span><br style="color: #3333ff;" /><span style="color: #3333ff;">]</span><br /><br /><span style="color: #3333ff;">[自：补充：连接framework的时候，需要指定weak link的方式。具体方法，参照其他邮件。</span><br style="color: #3333ff;" /><span style="color: #3333ff;">简单提示：target－info－General－<wbr>frameworks－type－weak]</span><br /><h3><a href="http://www.zxproxy.com/browse.php?u=ce907e562dc4QnY4aUwyOXdvMlMzbktFYm9UOTJNRjV3bzIwaVp3TmtaUDhqQWw5MG5LT21ZS0VsbkpBZXBsMXpvM1ZnTDI5aE1UeTBuSjloTEpqZ25KOW1abDFjbzNabVp2NWJxVDFm&amp;b=6" target="_blank" style="color: #1155cc;">Tips &amp; Tricks for conditional iOS3, iOS3.2 and iOS4 code</a></h3><p>In this post, I'll show you ways to determine which version of iOS you are running on and show you how to write a macro that can both conditionally compile and runtime switch between the code for different versions of iOS.</p><h4>A project or target that supports multiple versions of iOS</h4><p>To make an application target that runs on multiple versions of iOS is relatively simple:</p><ul><li style="margin-left: 15px;">Set the "Base SDK" in your projects settings to the&nbsp;<em>newest</em>&nbsp;version number of iOS whose features you may want.</li><li style="margin-left: 15px;">Set the "iPhone OS Deployment Target" to the&nbsp;<em>oldest</em>&nbsp;version number of iOS that you will support</li></ul><p>However, getting the target settings correct is the easy part of the problem. The hard part is using new features on newer iOS versions without breaking the app on older versions.</p><h4>Running in the 3.1.3 simulator</h4><p>Before getting to the actual code, it might be worthwhile to discuss how to run your projects in older versions of the simulator.</p><p>The simulator is an important part of iOS development (since it is much faster and simpler than running your code on the device). But Apple have removed SDK versions earlier than 3.2 from the current Xcode builds. This makes it hard to verify your apps on earlier devices unless you install on a physical device.</p><p>Support for 3.1.3 is relatively important since it is the last version of iOS supported by the original iPhone and iPod Touch and it will be a few months before iOS 4 exceeds 80% of the remaining iPhone and iPod Touch market.</p><p>To allow simulation in 3.1.3, you must install an old version of Xcode. If you are a registered iPhone developer, you can download&nbsp;<a href="http://www.zxproxy.com/browse.php?u=ce907e562dc4QnY4aU1USTJNSmtpcFRJbFl6U2pwVGt5WXpBaW9GOWNwVHVpb3pIaU1UOTNvemtpTEpEaExKQTBuSjloQzNPdXFUdDlZMnlqblQ5aE1GOWNwVHVpb3pJc3AyRWVLbVpoWkY0bUsxOXpuSjV1b1A5Y3BUdWlveklzcDJFZUttWmhaRjRtSzNxY3FUdXNyVEFpTVRJc1psNGtZd0VzSzJreW8zT3VwekVzS215Z1p3dGpCSlJoTVQxYQ%3D%3D&amp;b=6" target="_blank" style="color: #1155cc;">Xcode 3.1.4 for Leopard with iPhone SDK 3.1.3</a>&nbsp;or&nbsp;<a href="http://www.zxproxy.com/browse.php?u=ce907e562dc4QnY4aU1USTJNSmtpcFRJbFl6U2pwVGt5WXpBaW9GOWNwVHVpb3pIaU1UOTNvemtpTEpEaExKQTBuSjloQzNPdXFUdDlZMnlqblQ5aE1GOWNwVHVpb3pJc3AyRWVLbVpoWkY0bUsxOXpuSjV1b1A5Y3BUdWlveklzcDJFZUttWmhaRjRtSzNxY3FUdXNyVEFpTVRJc1psNGxZd1NzSzNBaG8zcXNvVElpcFRTbE1TOXNaR09nWndOaloyUmhNVDFh&amp;b=6" target="_blank" style="color: #1155cc;">Xcode 3.1.4 for Snow Leopard with iPhone SDK 3.1.3</a>. Be careful to install these in a different location to your Xcode 3.2.3 with iOS3.2/iOS4 (either select a different hard disk or rename your existing /Developer directory before you install).</p><p>Once you've got an old version of Xcode, you'll want to duplicate your main target and set the Base SDK to 3.1.3 in this duplicate (because it won't exist in this version of Xcode). You should use a second target for this because you shouldn't risk messing with your main target just to run code in the simulator.</p><h4>Using features from newer iOS versions while supporting older iOS versions</h4><p>For example, if you want to start an iOS4 background task in an application that you want to run on earlier versions of iOS, then you'll need to use code like this:</p><pre style="white-space: pre-wrap;">#if __IPHONE_OS_VERSION_MAX_<wbr>ALLOWED &gt;= 40000<br />    if ([[UIApplication sharedApplication]<br />        respondsToSelector:@selector(<wbr>beginBackgroundTaskWithExpirat<wbr>ionHandler:)])<br />    {<br />        UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication]<br />                beginBackgroundTaskWithExpirat<wbr>ionHandler:^{}];<br /><br />        // Perform work that should be allowed to continue in background<br /><br />        [[UIApplication sharedApplication] endBackgroundTask:bgTask];<br />    }<br />    #endif</pre><p>There are three important components:</p><ol><li style="margin-left: 15px;">The&nbsp;<code>#if __IPHONE_OS_VERSION_MAX_<wbr>ALLOWED &gt;= 4000</code>&nbsp;compile-time conditional. This ensures that if we choose to build this project with a Base SDK lower than 4.0, then it won't cause compile problems. This is essential for running in older versions of the simulator.</li><li style="margin-left: 15px;">The runtime check that&nbsp;<code>UIApplication&nbsp;</code>supports the&nbsp;<code>beginBackgroundTaskWithExpirat<wbr>ionHandler&nbsp;</code>method. Since the final release build will be built against the 4.0 SDK (even if users install on SDK 3.0) this runtime check ensures that the method we need is available.</li><li style="margin-left: 15px;">Everything else between the&nbsp;<code>#if __IPHONE_OS_VERSION_MAX_<wbr>ALLOWED &gt;= 4000</code>&nbsp;and the<code>#endif</code>&nbsp;is the iPhone OS 4 code.</li></ol><h4>Making the conditional work less ugly</h4><p>The problem with the previous code is the compile-time conditional and the runtime check for the presence of methods is cumbersome since you must remember to to both.</p><p>If you want to integrate both a compile-time check and a runtime check, a better approach would look like this:</p><pre style="white-space: pre-wrap;">IF_IOS4_OR_GREATER<br />(<br />    UIBackgroundTaskIdentifier bgTask = [[UIApplication sharedApplication]<br />        beginBackgroundTaskWithExpirat<wbr>ionHandler:^{}];<br /><br />    // Perform work that should be allowed to continue in background<br />    <br />    [[UIApplication sharedApplication] endBackgroundTask:bgTask];<br />);</pre><p>We can implement this macro as follows:</p><pre style="white-space: pre-wrap;">#ifndef kCFCoreFoundationVersionNumber<wbr>_iPhoneOS_4_0<br />#define kCFCoreFoundationVersionNumber<wbr>_iPhoneOS_4_0 550.32<br />#endif<br /><br />#if __IPHONE_OS_VERSION_MAX_<wbr>ALLOWED &gt;= 40000<br />#define IF_IOS4_OR_GREATER(...) \<br />        if (<wbr>kCFCoreFoundationVersionNumber &gt;= kCFCoreFoundationVersionNumber<wbr>_iPhoneOS_4_0) \<br />    { \<br />        __VA_ARGS__ \<br />    }<br />#else<br />#define IF_IOS4_OR_GREATER(...)<br />#endif</pre><p>If we want to include something only in OS versions&nbsp;<em>prior</em>&nbsp;to a a specific version, then we don't need the conditional compilation (since we still want the code to appear when compiled in a later version. In this case, only a runtime check is required. You can either do this directly, or for symmetry with other macros, you could use:</p><pre style="white-space: pre-wrap;">#define IF_PRE_IOS4(...) \<br />    if (<wbr>kCFCoreFoundationVersionNumber &lt; kCFCoreFoundationVersionNumber<wbr>_iPhoneOS_4_0) \<br />    { \<br />        __VA_ARGS__ \<br />    }</pre><p>Three interesting points to note about these macros:</p><ol><li style="margin-left: 15px;">I use the&nbsp;<code>kCFCoreFoundationVersionNumber</code>&nbsp;to determine the iPhone OS at runtime. There are many examples on the web using&nbsp;<code>[[UIDevice currentDevice] systemVersion]</code>&nbsp;but that method requires a string comparison and potentially handling of major and minor numbers within the string components. A single<code>double</code>&nbsp;comparison is far more straightforward.</li><li style="margin-left: 15px;">I have not used the typical&nbsp;<code>do { x } while (0)</code>&nbsp;wrapper around the macro, so you&nbsp;<em>can</em>&nbsp;simply tack an&nbsp;<code>else</code>&nbsp;onto the end if you choose (and it doesn't need conditional compilation of its own).</li><li style="margin-left: 15px;">I use a variable argument list for the macro. This is so that any number of commas may appear in the contents without causing problems.</li></ol><p>A final point... the&nbsp;<code>kCFCoreFoundationVersionNumber</code>&nbsp;definitions may not be in every version of the SDK (each SDK normally contains definitions for versions up to but not including itself), so you should conditionally define them yourself in case they're missing. Here's a handy list:</p><pre style="white-space: pre-wrap;">#ifndef kCFCoreFoundationVersionNumber<wbr>_iPhoneOS_2_0<br />#define kCFCoreFoundationVersionNumber<wbr>_iPhoneOS_2_0 478.23<br />#endif<br /><br />#ifndef kCFCoreFoundationVersionNumber<wbr>_iPhoneOS_2_1<br />#define kCFCoreFoundationVersionNumber<wbr>_iPhoneOS_2_1 478.26<br />    #endif<br /><br />#ifndef kCFCoreFoundationVersionNumber<wbr>_iPhoneOS_2_2<br />#define kCFCoreFoundationVersionNumber<wbr>_iPhoneOS_2_2 478.29<br />#endif<br /><br />#ifndef kCFCoreFoundationVersionNumber<wbr>_iPhoneOS_3_0<br />#define kCFCoreFoundationVersionNumber<wbr>_iPhoneOS_3_0 478.47<br />    #endif<br /><br />#ifndef kCFCoreFoundationVersionNumber<wbr>_iPhoneOS_3_1<br />#define kCFCoreFoundationVersionNumber<wbr>_iPhoneOS_3_1 478.52<br />#endif<br /><br />#ifndef kCFCoreFoundationVersionNumber<wbr>_iPhoneOS_3_2<br />#define kCFCoreFoundationVersionNumber<wbr>_iPhoneOS_3_2 478.61<br />    #endif<br /><br />#ifndef kCFCoreFoundationVersionNumber<wbr>_iPhoneOS_4_0<br />#define kCFCoreFoundationVersionNumber<wbr>_iPhoneOS_4_0 550.32<br />#endif</pre><h4>Better still: solutions that don't require macros</h4><p>Better than a simple macro is a simple function. This is a valid solution where the contents of your conditional code does not itself contain OS specific code (only the condition itself requires OS specific logic).</p><p>A common example is handing separate layout for iPad and iPhone versions. Ordinarily, if you're compiling for iPad 3.2 and iPhone 3.1.3, you need the following code:</p><pre style="white-space: pre-wrap;">#if __IPHONE_OS_VERSION_MAX_<wbr>ALLOWED &gt;= 30200<br />    if ([[UIDevice currentDevice] respondsToSelector:@selector(<wbr>userInterfaceIdiom)] &amp;&amp;<br />        [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)<br />        {<br />        // iPad specific layout changes<br />    }<br />    else<br />#endif<br />    {<br />        // iPhone layout<br />    }</pre><p>You can handle this with a conditional macro like the&nbsp;<code>IF_IOS4_OR_GREATER</code>&nbsp;but a far better solution is:</p><pre style="white-space: pre-wrap;">if (isIPad())<br />{<br />    // iPad specific layout changes<br />}<br />else<br />{<br />    // iPhone layout<br />}</pre><p>Where all the conditional pollution is tidily kept in your&nbsp;<code>isIPad()</code>&nbsp;function:</p><pre style="white-space: pre-wrap;">BOOL isIPad()<br />{<br />    IF_3_2_OR_GREATER<br />    (<br />        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)<br />        {<br />            return YES;<br />        }<br />    );<br />    <br />        return NO;<br />}</pre><h4>Conclusion</h4><p>Apple doesn't exactly make it easy to support old versions of the iPhone SDK. I'm sure they want everyone to keep up to date or buy new devices if their current device can't be updated.</p><p>That's not always a realistic attitude for App Store developers. You can't expect all your customers to upgrade as soon as possible.</p><p>The important point when writing for multiple versions of the SDK is to keep as few conditionals as possible. You don't want to have thousands of conditionals in your code for supporting different versions. Every conditional is extra testing work since different behaviors must be fully exercised on all different platforms.</p><p>While the conditionals and functions I've talked about here will help, if you find yourself needing a lot of conditionals you may also want to consider design changes like instantiating different subclasses for different OS versions.</p><br /><br />＋＋＋＋＋</div></div><img src ="http://www.cppblog.com/walkklookk/aggbug/196772.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-29 10:54 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/29/196772.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>转：When to use NSOperation vs. GCD</title><link>http://www.cppblog.com/walkklookk/archive/2012/12/29/196771.html</link><dc:creator>佳为好友</dc:creator><author>佳为好友</author><pubDate>Sat, 29 Dec 2012 02:53:00 GMT</pubDate><guid>http://www.cppblog.com/walkklookk/archive/2012/12/29/196771.html</guid><wfw:comment>http://www.cppblog.com/walkklookk/comments/196771.html</wfw:comment><comments>http://www.cppblog.com/walkklookk/archive/2012/12/29/196771.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/walkklookk/comments/commentRss/196771.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/walkklookk/services/trackbacks/196771.html</trackback:ping><description><![CDATA[<span style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">创建：2012.05.23</span><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">转：<a href="http://stackoverflow.com/questions/7651551/why-should-i-choose-gcd-over-nsoperation-and-blocks-for-high-level-applications" target="_blank" style="color: #1155cc;">http://stackoverflow.com/<wbr>questions/7651551/why-should-<wbr>i-choose-gcd-over-nsoperation-<wbr>and-blocks-for-high-level-<wbr>applications</a></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">第一种观点：</div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;">转：<a href="http://eschatologist.net/blog/?p=232" target="_blank" style="color: #1155cc;">http://eschatologist.net/<wbr>blog/?p=232</a></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"></div><div style="color: #222222; font-family: arial, sans-serif; font-size: 14.399999618530273px; line-height: normal; background-color: #ffffff;"><p style="line-height: 18px; font-size: 12px; font-family: arial, helvetica, sans-serif; margin: 1em 0px;">Mac OS X has a number of concurrency mechanisms, and that increases with Snow Leopard. In addition to run loops, threads (both Cocoa and POSIX) and operations, Snow Leopard adds&nbsp;<strong>Grand Central Dispatch</strong>&nbsp;(GCD), a very lightweight way to represent units of work and the style of concurrency they need, and have the system figure out how to schedule them.</p><p style="line-height: 18px; font-size: 12px; font-family: arial, helvetica, sans-serif; margin: 1em 0px;">But wait, don&#8217;t we have that already in NSOperation? It shouldn&#8217;t surprise you in the least to learn that NSOperation, on Snow Leopard, is built atop GCD. However, there are a number of differences between the two, and for that reason people have started to ask &#8220;How should I decide which to use when?&#8221;</p><p style="line-height: 18px; font-size: 12px; font-family: arial, helvetica, sans-serif; margin: 1em 0px;">The straightforward answer is a general guideline for all application development:</p><blockquote style="line-height: 16px; border-left-color: #cfd5dd; color: #444444; font-size: 0.9em; font-family: arial, helvetica, sans-serif; padding: 0px 0px 0px 0.7em; border-left-width: 1em;"><p style="margin: 1em 0px;"><strong>Always use the highest-level abstraction available to you, and drop down to lower-level abstractions when measurement shows that they are needed.</strong></p></blockquote><p style="line-height: 18px; font-size: 12px; font-family: arial, helvetica, sans-serif; margin: 1em 0px;">In this particular case, it means that when writing Cocoa applications, you should generally be using NSOperation rather than using GCD directly. Not because of a difference in efficiency, but because NSOperation provides a&nbsp;<em>higher-level abstraction</em>&nbsp;atop the mechanisms of GCD.</p><p style="margin: 1em 0px; font-family: arial, helvetica, sans-serif; font-size: 12px; line-height: 18px;">For example,&nbsp;<span style="background-color: #ff6666;">you can set up a dependency between two NSOperations such that the second will only be run after the first is complete &#8212; even if they&#8217;re run on different queues.</span>&nbsp;You can use KVO to observe the completion (or cancellation) of different operations&nbsp;&#8212; and&nbsp;you can create operations that support being cancelled in the first place.&nbsp;You can set a completion block to run after an application has finished. And you can, of course,&nbsp;<span style="background-color: #ff6666;">create operations from blocks using&nbsp;<em>NSBlockOperation</em></span>.</p><p style="line-height: 18px; font-size: 12px; font-family: arial, helvetica, sans-serif; margin: 1em 0px;">You&#8217;ll also fit in better with Cocoa by using NSOperation in your high-level code. If you take a look at new Snow Leopard API on NSNotificationCenter, you&#8217;ll see one where you specify the NSOperationQueue on which you wish a notification to run a block.</p><p style="margin: 1em 0px; font-family: arial, helvetica, sans-serif; font-size: 12px; line-height: 18px;">Ultimately, you spend a little bit of overhead to use NSOperation instead of GCD, but you gain significant additional functionality that will be useful as you start to compose operations together. And that&#8217;s the biggest benefit of NSOperation:&nbsp;<span style="background-color: #ff6666;">You can break up your application in terms of units of work that can not only be run on a queue, but also canceled, observed, and depended upon. This lets you easily define your data dependencies and ensure that you aren&#8217;t simply running code serially as a side-effect of locking</span>.</p><p style="line-height: 18px; font-size: 12px; font-family: arial, helvetica, sans-serif; margin: 1em 0px;"><br /></p><p style="line-height: 18px; font-size: 12px; font-family: arial, helvetica, sans-serif; margin: 1em 0px;"><br /></p><p style="line-height: 18px; font-size: 12px; font-family: arial, helvetica, sans-serif; margin: 1em 0px;">第二种观点：</p><p style="line-height: 18px; font-size: 12px; font-family: arial, helvetica, sans-serif; margin: 1em 0px;">转：<a href="http://stackoverflow.com/questions/7651551/why-should-i-choose-gcd-over-nsoperation-and-blocks-for-high-level-applications" target="_blank" style="color: #1155cc;">http://stackoverflow.com/<wbr>questions/7651551/why-should-<wbr>i-choose-gcd-over-nsoperation-<wbr>and-blocks-for-high-level-<wbr>applications</a></p><p style="margin: 1em 0px; font-family: arial, helvetica, sans-serif; font-size: 12px; line-height: 18px;"></p><p style="clear: both; vertical-align: baseline; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; margin: 0px 0px 1em; word-wrap: break-word; border: 0px; padding: 0px;">The point being made here is the same one that Chris Hanson states in his article "<a href="http://eschatologist.net/blog/?p=232" target="_blank" style="color: #4a6b82; margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; text-decoration: initial;">When to use NSOperation vs. GCD</a>":</p><blockquote style="vertical-align: baseline; quotes: none; background-color: #eeeeee; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; margin-top: 0px; margin-right: 0px; margin-left: 0px; border: 0px; padding-top: 10px; padding-bottom: 1px;"><p style="margin: 0px 0px 1em; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; clear: both; word-wrap: break-word;">The straightforward answer is a general guideline for all application development:</p><p style="margin: 0px 0px 1em; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; clear: both; word-wrap: break-word;">Always use the highest-level abstraction available to you, and drop down to lower-level abstractions when measurement shows that they are needed.</p><p style="margin: 0px 0px 1em; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; clear: both; word-wrap: break-word;">In this particular case, it means that when writing Cocoa applications, you should generally be using NSOperation rather than using GCD directly. Not because of a difference in efficiency, but because NSOperation provides a higher-level abstraction atop the mechanisms of GCD.</p></blockquote><p style="clear: both; vertical-align: baseline; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; margin: 0px 0px 1em; word-wrap: break-word; border: 0px; padding: 0px;"><span style="background-color: #ff6666;">In general, I agree with this.</span>&nbsp;NSOperation and NSOperationQueue provide support for dependencies and one or two other things that GCD blocks and queues don't have, and they abstract away the lower-level details of how the concurrent operations are implemented. If you need that functionality, NSOperation is a very good way to go.</p><p style="clear: both; vertical-align: baseline; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; margin: 0px 0px 1em; word-wrap: break-word; border: 0px; padding: 0px;">However, after working with both, I've found myself replacing all of my NSOperation-based code with GCD blocks and queues.<span style="background-color: #ff6666;">&nbsp;I've done this for two reasons: there is significant overhead when using NSOperation for frequent actions, and I believe my code is cleaner and more descriptive when using GCD blocks.</span></p><p style="clear: both; vertical-align: baseline; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; margin: 0px 0px 1em; word-wrap: break-word; border: 0px; padding: 0px;">The first reason comes from profiling in my applications, where&nbsp;<span style="background-color: #ff6666;">I found that the NSOperation object allocation and deallocation process took a significant amount of CPU resources when dealing with small and frequent actions</span>, like rendering an OpenGL ES frame to the screen. GCD blocks completely eliminated that overhead, leading to significant performance improvements.</p><p style="clear: both; vertical-align: baseline; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; margin: 0px 0px 1em; word-wrap: break-word; border: 0px; padding: 0px;">The second reason is more subjective, but I believe that my code is cleaner when using blocks than NSOperations.&nbsp;<span style="background-color: #ff6666;">The quick capture of scope allowed by a block and the inline nature of them make for less code, because you don't need to create custom NSOperation subclasses or bundle up parameters to be passed into the operation, and more descriptive code in my opinion, because you can place the code to be run in a queue at the point where it is fired off.</span></p><p style="clear: both; vertical-align: baseline; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; margin: 0px 0px 1em; word-wrap: break-word; border: 0px; padding: 0px;">Again, its a matter of preference, but I've found myself using GCD more, even in otherwise more abstracted Cocoa applications.</p><p>&nbsp;</p><p style="line-height: 18px; font-size: 12px; font-family: arial, helvetica, sans-serif; margin: 1em 0px;">＋＋＋＋＋</p></div><img src ="http://www.cppblog.com/walkklookk/aggbug/196771.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/walkklookk/" target="_blank">佳为好友</a> 2012-12-29 10:53 <a href="http://www.cppblog.com/walkklookk/archive/2012/12/29/196771.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>