﻿<?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++博客-逛奔的蜗牛-随笔分类-Cocoa</title><link>http://www.cppblog.com/biao/category/18269.html</link><description> 我不聪明，但我会很努力</description><language>zh-cn</language><lastBuildDate>Sat, 03 Aug 2013 13:51:48 GMT</lastBuildDate><pubDate>Sat, 03 Aug 2013 13:51:48 GMT</pubDate><ttl>60</ttl><item><title>Qt：FontMetrics</title><link>http://www.cppblog.com/biao/archive/2013/08/03/202322.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Sat, 03 Aug 2013 10:27:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2013/08/03/202322.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/202322.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2013/08/03/202322.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/202322.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/202322.html</trackback:ping><description><![CDATA[<div>在一个Rect的正中间绘制文本: rect的中点与文本的中点重合.</div>
<div>
<div>int cx = rect.x + rect.width / 2;</div>
<div>int fx = cx - fm.width(text);</div>
</div>
<div><br />
</div>
<div>int cy = rect.y + rect.height / 2;</div>
<div>int fy = cy + fm.xHeight&nbsp;/ 2;</div>
<div><br />
</div>
<div><br />
</div>
<div><br />
</div>
<img src="http://www.cppblog.com/images/cppblog_com/biao/FontMetrics.png" width="506" height="274" alt="" />
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/202322.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2013-08-03 18:27 <a href="http://www.cppblog.com/biao/archive/2013/08/03/202322.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cocoa：Simple NSUserDefaults example</title><link>http://www.cppblog.com/biao/archive/2011/12/22/162622.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 22 Dec 2011 15:33:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2011/12/22/162622.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/162622.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2011/12/22/162622.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/162622.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/162622.html</trackback:ping><description><![CDATA[<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #008d00">//</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #008d00">//&nbsp; AppController.h</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #008d00">//&nbsp; SimpleUserDefaults</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #008d00">//</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #008d00">//&nbsp; Created by Biao Huang on 12/22/11.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #008d00">//&nbsp; Copyright (c) 2011 __MyCompanyName__. All rights reserved.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #008d00">//</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br />
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #e50011"><span style="color: #804528">#import </span>&lt;Foundation/Foundation.h&gt;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br />
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco"><span style="color: #ce00a4">extern</span> <span style="color: #7d1bac">NSString</span> * <span style="color: #ce00a4">const</span> DefaultsFavoriteBookKey;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco"><span style="color: #ce00a4">extern</span> <span style="color: #7d1bac">NSString</span> * <span style="color: #ce00a4">const</span> DefaultsFavoriteCityKey;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco"><span style="color: #ce00a4">extern</span> <span style="color: #7d1bac">NSString</span> * <span style="color: #ce00a4">const</span> DefaultsFavoriteFoodKey;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco"><span style="color: #ce00a4">extern</span> <span style="color: #7d1bac">NSString</span> * <span style="color: #ce00a4">const</span> DefaultsFavoriteColorKey;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br />
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco"><span style="color: #ce00a4">@interface</span> AppController : <span style="color: #7d1bac">NSObject</span> {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; <span style="color: #ce00a4">IBOutlet</span> <span style="color: #7d1bac">NSTextField</span> *bookField;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; <span style="color: #ce00a4">IBOutlet</span> <span style="color: #7d1bac">NSTextField</span> *cityField;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; <span style="color: #ce00a4">IBOutlet</span> <span style="color: #7d1bac">NSTextField</span> *foodField;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; <span style="color: #ce00a4">IBOutlet</span> <span style="color: #7d1bac">NSTextField</span> *colorField;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px">&nbsp;&nbsp; &nbsp;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; <span style="color: #7d1bac">NSUserDefaults</span> *preferences;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br />
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">- (<span style="color: #ce00a4">IBAction</span>)textFieldChanged:(<span style="color: #ce00a4">id</span>)sender;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #ce00a4">@end</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #ce00a4"><br />
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #ce00a4">
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #008d00">//</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #008d00">//&nbsp; AppController.m</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #008d00">//&nbsp; SimpleUserDefaults</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #008d00">//</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #008d00">//&nbsp; Created by Biao Huang on 12/22/11.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #008d00">//&nbsp; Copyright (c) 2011 __MyCompanyName__. All rights reserved.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #008d00">//</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br />
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #e50011"><span style="color: #804528">#import </span>"AppController.h"</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br />
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco"><span style="color: #7d1bac">NSString</span> * <span style="color: #ce00a4">const</span> DefaultsFavoriteBookKey = <span style="color: #e50011">@"Favorite Book"</span>;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco"><span style="color: #7d1bac">NSString</span> * <span style="color: #ce00a4">const</span> DefaultsFavoriteCityKey = <span style="color: #e50011">@"Favorite City"</span>;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco"><span style="color: #7d1bac">NSString</span> * <span style="color: #ce00a4">const</span> DefaultsFavoriteFoodKey = <span style="color: #e50011">@"Favorite Food"</span>;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco"><span style="color: #7d1bac">NSString</span> * <span style="color: #ce00a4">const</span> DefaultsFavoriteColorKey = <span style="color: #e50011">@"Favorite Color"</span>;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br />
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #ce00a4">@implementation<span style="color: #000000"> AppController</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br />
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">+ (<span style="color: #ce00a4">void</span>)initialize {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; <span style="color: #ce00a4">if</span> (<span style="color: #ce00a4">self</span> != [<span style="color: #3c8388">AppController</span> <span style="color: #470084">class</span>]) {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ce00a4">return</span>;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; }</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px">&nbsp;&nbsp; &nbsp;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #7d1bac"><span style="color: #000000">&nbsp; &nbsp; </span>NSMutableDictionary<span style="color: #000000"> *defaultPrefs = [</span>NSMutableDictionary<span style="color: #000000"> </span><span style="color: #470084">dictionary</span><span style="color: #000000">];</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; [defaultPrefs <span style="color: #470084">setObject</span>:<span style="color: #e50011">@"Cocoa Programming"</span> <span style="color: #470084">forKey</span>:<span style="color: #3c8388">DefaultsFavoriteBookKey</span>];</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; [defaultPrefs <span style="color: #470084">setObject</span>:<span style="color: #e50011">@"Braunschweig"</span> <span style="color: #470084">forKey</span>:<span style="color: #3c8388">DefaultsFavoriteCityKey</span>];</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; [defaultPrefs <span style="color: #470084">setObject</span>:<span style="color: #e50011">@"Fleisch"</span> <span style="color: #470084">forKey</span>:<span style="color: #3c8388">DefaultsFavoriteFoodKey</span>];</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #3c8388"><span style="color: #000000">&nbsp; &nbsp; [defaultPrefs </span><span style="color: #470084">setObject</span><span style="color: #000000">:</span><span style="color: #e50011">@"Schwarz"</span><span style="color: #000000"> </span><span style="color: #470084">forKey</span><span style="color: #000000">:</span>DefaultsFavoriteColorKey<span style="color: #000000">];</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px">&nbsp;&nbsp; &nbsp;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #008d00"><span style="color: #000000">&nbsp; &nbsp; </span>// Regisgter factory preferences.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #470084"><span style="color: #000000">&nbsp; &nbsp; [[</span><span style="color: #7d1bac">NSUserDefaults</span><span style="color: #000000"> </span>standardUserDefaults<span style="color: #000000">] </span>registerDefaults<span style="color: #000000">:defaultPrefs];</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br />
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">- (<span style="color: #ce00a4">id</span>)init {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; <span style="color: #ce00a4">self</span> = [<span style="color: #ce00a4">super</span> <span style="color: #470084">init</span>];</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; <span style="color: #ce00a4">if</span> (<span style="color: #ce00a4">self</span>) { &nbsp; &nbsp; &nbsp; &nbsp;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #470084"><span style="color: #000000">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color: #3c8388">preferences</span><span style="color: #000000"> = [[</span><span style="color: #7d1bac">NSUserDefaults</span><span style="color: #000000"> </span>standardUserDefaults<span style="color: #000000">] </span>retain<span style="color: #000000">];</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; }</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #ce00a4"><span style="color: #000000">&nbsp; &nbsp; </span>return<span style="color: #000000"> </span>self<span style="color: #000000">;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br />
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">- (<span style="color: #ce00a4">void</span>)dealloc {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #3c8388"><span style="color: #000000">&nbsp; &nbsp; [</span>preferences<span style="color: #000000"> </span><span style="color: #470084">release</span><span style="color: #000000">];</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; [<span style="color: #ce00a4">super</span> <span style="color: #470084">dealloc</span>];</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br />
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">- (<span style="color: #ce00a4">void</span>)awakeFromNib {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #3c8388"><span style="color: #000000">&nbsp; &nbsp; [</span>bookField<span style="color: #000000"> </span><span style="color: #470084">setStringValue</span><span style="color: #000000">:[</span>preferences<span style="color: #000000"> </span><span style="color: #470084">stringForKey</span><span style="color: #000000">:</span>DefaultsFavoriteBookKey<span style="color: #000000">]];</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #3c8388"><span style="color: #000000">&nbsp; &nbsp; [</span>cityField<span style="color: #000000"> </span><span style="color: #470084">setStringValue</span><span style="color: #000000">:[</span>preferences<span style="color: #000000"> </span><span style="color: #470084">stringForKey</span><span style="color: #000000">:</span>DefaultsFavoriteCityKey<span style="color: #000000">]];</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #3c8388"><span style="color: #000000">&nbsp; &nbsp; [</span>foodField<span style="color: #000000"> </span><span style="color: #470084">setStringValue</span><span style="color: #000000">:[</span>preferences<span style="color: #000000"> </span><span style="color: #470084">stringForKey</span><span style="color: #000000">:</span>DefaultsFavoriteFoodKey<span style="color: #000000">]];</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #3c8388"><span style="color: #000000">&nbsp; &nbsp; [</span>colorField<span style="color: #000000"> </span><span style="color: #470084">setStringValue</span><span style="color: #000000">:[</span>preferences<span style="color: #000000"> </span><span style="color: #470084">stringForKey</span><span style="color: #000000">:</span>DefaultsFavoriteColorKey<span style="color: #000000">]]; &nbsp; &nbsp;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br />
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">- (<span style="color: #ce00a4">IBAction</span>)textFieldChanged:(<span style="color: #ce00a4">id</span>)sender {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; <span style="color: #7d1bac">NSString</span> *favorite = [sender <span style="color: #470084">stringValue</span>];</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px">&nbsp;&nbsp; &nbsp;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; <span style="color: #ce00a4">if</span> (sender == <span style="color: #3c8388">bookField</span>) {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #3c8388"><span style="color: #000000">&nbsp; &nbsp; &nbsp; &nbsp; [</span>preferences<span style="color: #000000"> </span><span style="color: #470084">setObject</span><span style="color: #000000">:favorite </span><span style="color: #470084">forKey</span><span style="color: #000000">:</span>DefaultsFavoriteBookKey<span style="color: #000000">];</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; } <span style="color: #ce00a4">else</span> <span style="color: #ce00a4">if</span> (sender == <span style="color: #3c8388">cityField</span>) {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #3c8388"><span style="color: #000000">&nbsp; &nbsp; &nbsp; &nbsp; [</span>preferences<span style="color: #000000"> </span><span style="color: #470084">setObject</span><span style="color: #000000">:favorite </span><span style="color: #470084">forKey</span><span style="color: #000000">:</span>DefaultsFavoriteCityKey<span style="color: #000000">];</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; } <span style="color: #ce00a4">else</span> <span style="color: #ce00a4">if</span> (sender == <span style="color: #3c8388">foodField</span>) {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #3c8388"><span style="color: #000000">&nbsp; &nbsp; &nbsp; &nbsp; [</span>preferences<span style="color: #000000"> </span><span style="color: #470084">setObject</span><span style="color: #000000">:favorite </span><span style="color: #470084">forKey</span><span style="color: #000000">:</span>DefaultsFavoriteFoodKey<span style="color: #000000">];</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; } <span style="color: #ce00a4">else</span> <span style="color: #ce00a4">if</span> (sender == <span style="color: #3c8388">colorField</span>) {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #3c8388"><span style="color: #000000">&nbsp; &nbsp; &nbsp; &nbsp; [</span>preferences<span style="color: #000000"> </span><span style="color: #470084">setObject</span><span style="color: #000000">:favorite </span><span style="color: #470084">forKey</span><span style="color: #000000">:</span>DefaultsFavoriteColorKey<span style="color: #000000">];</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; }</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #ce00a4">@end</p>
<div><br />
</div>
</p>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/162622.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2011-12-22 23:33 <a href="http://www.cppblog.com/biao/archive/2011/12/22/162622.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cocoa：Parse XML using NSXMLParser</title><link>http://www.cppblog.com/biao/archive/2011/12/18/162350.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Sun, 18 Dec 2011 09:57:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2011/12/18/162350.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/162350.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2011/12/18/162350.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/162350.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/162350.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: There's a class called NSXMLParser. It's used to parse XML files. However, NSXMLParser is stupid. All it knows how to do is parse XML, but it doesn't know what it's supposed to do with the information...&nbsp;&nbsp;<a href='http://www.cppblog.com/biao/archive/2011/12/18/162350.html'>阅读全文</a><img src ="http://www.cppblog.com/biao/aggbug/162350.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2011-12-18 17:57 <a href="http://www.cppblog.com/biao/archive/2011/12/18/162350.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cocoa：Cocoa Snippets 01-10</title><link>http://www.cppblog.com/biao/archive/2011/12/18/162338.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Sun, 18 Dec 2011 06:55:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2011/12/18/162338.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/162338.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2011/12/18/162338.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/162338.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/162338.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 1. Open URL in Safari&nbsp; &nbsp;&nbsp;NSURL&nbsp;*url = [ [&nbsp;NSURLalloc&nbsp;]&nbsp;initWithString:&nbsp;@"http://www.cnn.com"&nbsp;];&nbsp; &nbsp; [[UIApplication sharedApplication] openU...&nbsp;&nbsp;<a href='http://www.cppblog.com/biao/archive/2011/12/18/162338.html'>阅读全文</a><img src ="http://www.cppblog.com/biao/aggbug/162338.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2011-12-18 14:55 <a href="http://www.cppblog.com/biao/archive/2011/12/18/162338.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>C++：浮点数的存储方式</title><link>http://www.cppblog.com/biao/archive/2011/12/18/162330.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Sun, 18 Dec 2011 03:20:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2011/12/18/162330.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/162330.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2011/12/18/162330.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/162330.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/162330.html</trackback:ping><description><![CDATA[<span  style="font-family: simsun; line-height: 23px; ">float内存存储结构(共4字节，32位分别如下)：<br />
31 30 29----23 22----0 &nbsp;&nbsp;<br />
<br />
实数符号位 指数符号位 指数位 有效数位<br />
其中实数符号位0表示正，1表示负；指数符号位1表示正，0表示负<br />
<br />
float在计算机中的存储计算方法：<br />
1. 先将这个实数的绝对值化为二进制格式，方法是：实数的整数部分是除2取余和小数部分是乘2取整<br />
2. 将这个二进制格式实数的小数点左移或右移n位，直到小数点移动到第一个有效数字的右边。<br />
3. 从小数点右边第一位开始数出二十三位数字放入第22到第0位。<br />
4. 如果实数是正的，则在第31位放入&#8220;0&#8221;，否则放入&#8220;1&#8221;。<br />
5. 如果n 是左移得到的，说明指数是正的，第30位放入&#8220;1&#8221;。如果n是右移得到的或n=0，则第30位放入&#8220;0&#8221;。<br />
6. 如果n是左移得到的，则将n减去1后化为二进制，并在左边加&#8220;0&#8221;补足七位，放入第29到第23位。如果n是右移得到的或n=0，<br />
<br />
则将n化为二进制后在左边加&#8220;0&#8221;补足七位，再各位求反，再放入第29到第23位。&nbsp;&nbsp;<br />
<br />
根据上面那个步骤得：<br />
(1). 23.172001二进制形式为：0001,0111.0010,1100,0000,1000,0100,0001....<br />
(2)左移4位得：1.0111.0010,1100,0000,1000,0100,0001<br />
(3)取小数点后23位得： 0111.0010,1100,0000,1000,010（这23位为float数二进制码的后23位）<br />
(4)第31位： 0<br />
(5)第30位： 1<br />
(6)左移4位，4的二进制0011，不足7位则：0000,011<br />
最后得23.172001在计算机中的二进制表现形式为：0100,0001,1011,1001,0110,0000,0100,0010<br />
二进制转16进制得：41A96042</span>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
<div><span  style="font-family: simsun; line-height: 23px; "><br />
</span></div>
<div><span  style="font-family: simsun; line-height: 23px; ">From:&nbsp;http://topic.csdn.net/u/20111217/19/6c5221d6-a779-4e6c-9eb0-72949857dc6e.html?95401</span></div><img src ="http://www.cppblog.com/biao/aggbug/162330.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2011-12-18 11:20 <a href="http://www.cppblog.com/biao/archive/2011/12/18/162330.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cocoa：Objective-C属性</title><link>http://www.cppblog.com/biao/archive/2011/12/15/162190.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 15 Dec 2011 14:53:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2011/12/15/162190.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/162190.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2011/12/15/162190.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/162190.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/162190.html</trackback:ping><description><![CDATA[<span style="color: #333333; font-family: verdana, Arial, Helvetica, sans-serif; line-height: 25px; ">我们知道在Objective-C中，使用@property配合@synthesize可以让编译器自动实现getter/setter方法，使用的时候也很方便，可以直接使用<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: red; "><strong style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">对象.属性</strong></span>的方法调用。
<div class="cnblogs_code" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; background-color: #f5f5f5; font-family: 'Courier New'; font-size: 12px; border-left-color: #cccccc; word-break: break-all; overflow-x: auto; overflow-y: auto; ">
<div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Courier New'; font-size: 12px; line-height: 1.5; color: #000000; ">NSString</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Courier New'; font-size: 12px; line-height: 1.5; color: #000000; ">*</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Courier New'; font-size: 12px; line-height: 1.5; color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;name;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />
NSUInteger&nbsp;&nbsp;&nbsp;&nbsp;age;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />
<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />
@property(nonatomic,copy)NSString</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Courier New'; font-size: 12px; line-height: 1.5; color: #000000; ">*</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Courier New'; font-size: 12px; line-height: 1.5; color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />
@property(assign)NSUInteger&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;age;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />
<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />
@synthesize&nbsp;name;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />
</span></div>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: verdana, 'courier new'; font-size: 14px; line-height: 1.5; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Courier New'; font-size: 13px; line-height: 1.5; ">@synthesize&nbsp;age;</span>&nbsp;</span>&nbsp;</p>
<div class="cnblogs_code_toolbar" style="margin-top: 5px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span class="cnblogs_code_copy" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 0px; font-family: 'Courier New'; font-size: 12px; line-height: 1.5; "><a href="http://www.cnblogs.com/kaixuan/archive/2011/03/02/1969072.html" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-decoration: none; color: #000000; border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: #333333; ">复制代码</a></span></div>
</div>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; "><font face="'Courier New'" size="2" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />
</font></p>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; ">那如果我们想要<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: red; "><strong style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">对象.方法</strong></span>的方式来调用一个方法并获取到方法的返回值，那就需要使用@property配合@dynamic了。</p>
<div class="cnblogs_code" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; background-color: #f5f5f5; font-family: 'Courier New'; font-size: 12px; border-left-color: #cccccc; word-break: break-all; overflow-x: auto; overflow-y: auto; ">
<div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Courier New'; font-size: 12px; line-height: 1.5; color: #000000; ">@property(</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Courier New'; font-size: 12px; line-height: 1.5; color: #0000ff; ">readonly</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Courier New'; font-size: 12px; line-height: 1.5; color: #000000; ">)NSString</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Courier New'; font-size: 12px; line-height: 1.5; color: #000000; ">*</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Courier New'; font-size: 12px; line-height: 1.5; color: #000000; ">&nbsp;&nbsp;&nbsp;&nbsp;firstArrayValue;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />
<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />
@dynamic&nbsp;&nbsp;&nbsp;&nbsp;firstArrayValue;<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />
<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />
</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Courier New'; font-size: 12px; line-height: 1.5; color: #000000; ">-</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Courier New'; font-size: 12px; line-height: 1.5; color: #000000; ">&nbsp;(NSString</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Courier New'; font-size: 12px; line-height: 1.5; color: #000000; ">*</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Courier New'; font-size: 12px; line-height: 1.5; color: #000000; ">)firstArrayValue<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />
{<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />
&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Courier New'; font-size: 12px; line-height: 1.5; color: #0000ff; ">return</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Courier New'; font-size: 12px; line-height: 1.5; color: #000000; ">&nbsp;[_array&nbsp;objectAtIndex:</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Courier New'; font-size: 12px; line-height: 1.5; color: #800080; ">0</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Courier New'; font-size: 12px; line-height: 1.5; color: #000000; ">];<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />
</span></div>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; ">}&nbsp;</p>
<div class="cnblogs_code_toolbar" style="margin-top: 5px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span class="cnblogs_code_copy" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 5px; padding-bottom: 0px; padding-left: 0px; font-family: 'Courier New'; font-size: 12px; line-height: 1.5; "><a href="http://www.cnblogs.com/kaixuan/archive/2011/03/02/1969072.html" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-decoration: none; color: #000000; border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: #333333; ">复制代码</a></span></div>
</div>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; "><font face="'Courier New'" size="2" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; " />
</font></p>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; ">&nbsp;这样就可以使用对象.firstArrayValue来获取到_array数组中的第一个值了，很显然，这种方法并不适用于需要传递参数的方法。</p>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; ">&nbsp;</p>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; ">其实使用@dynamic关键字是告诉编译器由我们自己来实现访问方法。</p>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; ">如果使用的是@synthesize，那么这个工作编译器就会帮你实现了。 <span style="color: #ff0000; ">这时候你也可以自己实现此属性的Accessor，系统会调用你的实现代替系统的实现。虽然使用@property减少了代码，但是在我们的代码中，为了统一，最好还是使用[obj attribute]的方式来调用Accessors，不要一会使用obj.attribute，一会使用[obj attribute]，让代码不够统一。</span></p>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; ">&nbsp;</p>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; ">说明：代码只为示例代码，实际使用时每句代码要放到相应位置的。</p>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; ">&nbsp;</p>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; "><font color="#808080" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><strong style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "></strong></font></p>
<div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><font color="#808080" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><strong style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #333333; font-family: verdana, ����, Arial; font-weight: normal; ">
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; "><strong style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #808080; ">===== 最后转载下关于@property(*)括号中的属性内容介绍 =====</strong></p>
<div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; "><strong style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10pt; font-family: 'Times New Roman'; ">readonly</span></strong></p>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; ">此标记说明属性是只读的，默认的标记是读写，如果你指定了只读，在</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: 'Times New Roman'; ">@implementation</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; ">中只需要一个读取器。或者如果你使用</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: 'Times New Roman'; ">@synthesize</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; ">关键字，也是有读取器方法被解析。而且如果你试图使用点操作符为属性赋值，你将得到一个编译错误。</span></p>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; ">&nbsp;</p>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; "><strong style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: 'Times New Roman'; ">readwrite</span></strong></p>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; ">此标记说明属性会被当成读写的，这也是默认属性。设置器和读取器都需要在</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: 'Times New Roman'; ">@implementation</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; ">中实现。如果使用</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: 'Times New Roman'; ">@synthesize</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; ">关键字，读取器和设置器都会被解析。</span></p>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; ">&nbsp;</p>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; "><strong style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: 'Times New Roman'; ">assign</span></strong></p>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; ">此标记说明设置器直接进行赋值，这也是默认值。在使用垃圾收集的应用程序中，如果你要一个属性使用</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: 'Times New Roman'; ">assign</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; ">，且这个类符合</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: 'Times New Roman'; ">NSCopying</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; ">协议，你就要明确指出这个标记，而不是简单地使用默认值，否则的话，你将得到一个编译警告。这再次向编译器说明你确实需要赋值，即使它是可拷贝的。</span></p>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; ">&nbsp;</p>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; "><strong style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: 'Times New Roman'; ">retain</span></strong></p>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; ">指定</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: Verdana; ">retain</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; ">会在赋值时唤醒传入值的</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: Verdana; ">retain</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; ">消息。此属性只能用于</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: Verdana; ">Objective-C</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; ">对象类型，而不能用于</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: Verdana; ">Core Foundation</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; ">对象。</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: Verdana; ">(</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; ">原因很明显，</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: Verdana; ">retain</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; ">会增加对象的引用计数，而基本数据类型或者</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: Verdana; ">Core Foundation</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; ">对象都没有引用计数</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: Verdana; ">&#8212;&#8212;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; ">译者注</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: Verdana; ">)</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; ">。</span></p>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; ">&nbsp;</p>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; "><strong style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: 'Times New Roman'; ">copy</span></strong><strong style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "></strong></p>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; ">它指出，在赋值时使用传入值的一份拷贝。拷贝工作由</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: Verdana; ">copy</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; ">方法执行，此属性只对那些实行了</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: Verdana; ">NSCopying</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; ">协议的对象类型有效。更深入的讨论，请参考</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: Verdana; ">&#8220;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; ">复制</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: Verdana; ">&#8221;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; ">部分。</span></p>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; ">&nbsp;</p>
<p style="margin-top: 5px; margin-right: auto; margin-bottom: 5px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; text-indent: 0px; "><strong style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: 'Times New Roman'; ">nonatomic</span></strong></p>
<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: STSong; ">指出访问器不是原子操作，而默认地，访问器是原子操作。这也就是说，在多线程环境下，解析的访问器提供一个对属性的安全访问，从获取器得到的返回值或者通过设置器设置的值可以一次完成，即便是别的线程也正在对其进行访问。如果你不指定</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: Verdana; ">nonatomic</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: STSong; ">，在自己管理内存的环境中，解析的访问器保留并自动释放返回的值，如果指定了</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: Verdana; ">nonatomic</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: STSong; ">，那么访问器只是简单地返回这个值。</span></div>
<div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: STSong; "><br />
</span></div>
<div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.5; font-size: 10.5pt; font-family: STSong; ">From:&nbsp;http://www.cnblogs.com/kaixuan/archive/2011/03/02/1969072.html</span></div>
</span></strong></font></div>
</span><br />
<div>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #e50011"><span style="color: #804528">#import </span>&lt;Foundation/Foundation.h&gt;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br />
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco"><span style="color: #ce00a4">@interface</span> Employee : <span style="color: #7d1bac">NSObject</span> {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; <span style="color: #7d1bac">NSString</span> *firstName;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; <span style="color: #7d1bac">NSString</span> *lastName;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br />
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #ce00a4">@property<span style="color: #000000"> (</span>retain<span style="color: #000000">, </span>readwrite<span style="color: #000000">, </span>nonatomic<span style="color: #000000">) </span><span style="color: #7d1bac">NSString</span><span style="color: #000000"> *firstName;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #ce00a4">@property<span style="color: #000000"> (</span>retain<span style="color: #000000">, </span>readwrite<span style="color: #000000">, </span>nonatomic<span style="color: #000000">) </span><span style="color: #7d1bac">NSString</span><span style="color: #000000"> *lastName;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br />
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #ce00a4">@end</p>
</div>
<div><br />
</div>
<div>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #008d00">//</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #008d00">//&nbsp; Employee.m</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #008d00">//&nbsp; PropertyTest</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #008d00">//</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #008d00">//&nbsp; Created by Biao Huang on 12/15/11.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #008d00">//&nbsp; Copyright (c) 2011 __MyCompanyName__. All rights reserved.</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #008d00">//</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br />
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #e50011"><span style="color: #804528">#import </span>"Employee.h"</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br />
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #ce00a4">@implementation<span style="color: #000000"> Employee</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br />
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco"><span style="color: #ce00a4">@dynamic</span> firstName;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #ce00a4">@synthesize<span style="color: #000000"> lastName;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br />
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">- (<span style="color: #ce00a4">id</span>)init {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #ce00a4"><span style="color: #000000">&nbsp; &nbsp; </span>self<span style="color: #000000"> = [</span>super<span style="color: #000000"> </span>self<span style="color: #000000">];</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px">&nbsp;&nbsp; &nbsp;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; <span style="color: #ce00a4">if</span> (<span style="color: #ce00a4">self</span>) {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px 'Heiti SC Light'; color: #008d00"><span style="font: 14.0px Monaco; color: #000000">&nbsp; &nbsp; &nbsp; &nbsp; </span><span style="font: 14.0px Monaco">// </span>测试代码，三种形式都可以使用</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ce00a4">self</span>.<span style="color: #3c8388">firstName</span> = <span style="color: #e50011">@"biao"</span>;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #470084">NSLog</span>(<span style="color: #e50011">@"%@"</span>, <span style="color: #3c8388">firstName</span>);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #470084">NSLog</span>(<span style="color: #e50011">@"%@"</span>, [<span style="color: #ce00a4">self</span> <span style="color: #215b5e">firstName</span>]);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #470084">NSLog</span>(<span style="color: #e50011">@"%@"</span>, <span style="color: #ce00a4">self</span>.<span style="color: #3c8388">firstName</span>);</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; }</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px">&nbsp;&nbsp; &nbsp;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #ce00a4"><span style="color: #000000">&nbsp; &nbsp; </span>return<span style="color: #000000"> </span>self<span style="color: #000000">;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br />
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">- (<span style="color: #ce00a4">void</span>)setFirstName:(<span style="color: #7d1bac">NSString</span> *)newFirstName {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #e50011"><span style="color: #000000">&nbsp; &nbsp; </span><span style="color: #470084">NSLog</span><span style="color: #000000">(</span>@"[Employee setFirstName:]"<span style="color: #000000">);</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; [newFirstName <span style="color: #470084">retain</span>];</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #3c8388"><span style="color: #000000">&nbsp; &nbsp; [</span>firstName<span style="color: #000000"> </span><span style="color: #470084">release</span><span style="color: #000000">];</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">&nbsp; &nbsp; <span style="color: #3c8388">firstName</span> = newFirstName;</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br />
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">- (<span style="color: #7d1bac">NSString</span> *)firstName {</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #e50011"><span style="color: #000000">&nbsp; &nbsp; </span><span style="color: #470084">NSLog</span><span style="color: #000000">(</span>@"[Employee firstName]"<span style="color: #000000">);</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #3c8388"><span style="color: #000000">&nbsp; &nbsp; </span><span style="color: #ce00a4">return</span><span style="color: #000000"> </span>firstName<span style="color: #000000">;</span></p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco">}</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; min-height: 19.0px"><br />
</p>
<p style="margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Monaco; color: #ce00a4">@end</p>
</div>
<div><br />
</div><img src ="http://www.cppblog.com/biao/aggbug/162190.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2011-12-15 22:53 <a href="http://www.cppblog.com/biao/archive/2011/12/15/162190.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cocoa：[系统控件重绘教程]：重绘NSWindow</title><link>http://www.cppblog.com/biao/archive/2011/12/09/161808.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 08 Dec 2011 20:17:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2011/12/09/161808.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/161808.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2011/12/09/161808.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/161808.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/161808.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: NSWindow的风格一直只有两种，很难看。Panel倒是有种HUD风格的，但window没有，毕竟window和panel的titile大小还是不一样的，网上有关于HUDWindow的代码，不过那是把window的titlebar给隐藏掉，自己画上三个button和右下角可以resize的东东，如果用这个window来弹出sheet的话会很诡异的出现&#8230;&#8230;&nbsp...&nbsp;&nbsp;<a href='http://www.cppblog.com/biao/archive/2011/12/09/161808.html'>阅读全文</a><img src ="http://www.cppblog.com/biao/aggbug/161808.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2011-12-09 04:17 <a href="http://www.cppblog.com/biao/archive/2011/12/09/161808.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cocoa：[系统控件重绘教程(二)]：重绘NSButton</title><link>http://www.cppblog.com/biao/archive/2011/12/09/161807.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 08 Dec 2011 20:00:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2011/12/09/161807.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/161807.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2011/12/09/161807.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/161807.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/161807.html</trackback:ping><description><![CDATA[@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
<span style="border-collapse: collapse; color: #333333; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Arial, Verdana, sans-serif; line-height: 24px; ">首先大家看Apple关于NSButton的描述，NSButton跟NSWindow一样，它的外观形式也是委托给NSButtonCell来处理的，自身只包含逻辑代码。<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
所以重绘NSButton就是重绘NSButtonCell啦，然后把NSButton的cell设置位你自己的cell就好了。<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<strong style="outline-style: none; outline-width: initial; outline-color: initial; "><span style="outline-style: none; outline-width: initial; outline-color: initial; color: #0000ff; ">1）重绘目标</span></strong><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
首先观察一下系统NSButton的行为和外观表现，可以发现默认Button（快捷健设置为return）是有一个一闪一闪的效果，鼠标点击其他非默认button的时候同window上默认button的蓝色消失，同时被点中button变成蓝色。放开鼠标，默认button恢复蓝色背景并闪烁，被点击button变白色。<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
重绘一个控件最好是不要改变其默认行为，也最好不要违反Apple的关于界面设计的建议文档。所以我们的目标是重绘出来的button是灰色渐变背景，默认button有一个黄色的圈圈围在周围，不闪烁。被点中的button显示黄色圈圈，默认button黄色圈圈消失。<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
效果如下图：<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<strong style="outline-style: none; outline-width: initial; outline-color: initial; ">鼠标未按下效果</strong><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<span id="att_7551" style="outline-style: none; outline-width: initial; outline-color: initial; "><strong style="outline-style: none; outline-width: initial; outline-color: initial; "></strong><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<img src="http://cc.cocimg.com/bbs/attachment/Fid_2/2_1860_b49a1650922cd3b.png" border="0" style="outline-style: none; outline-width: initial; outline-color: initial; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " alt="" /></span>&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<strong style="outline-style: none; outline-width: initial; outline-color: initial; ">鼠标按下效果</strong><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<span id="att_7553" style="outline-style: none; outline-width: initial; outline-color: initial; "><strong style="outline-style: none; outline-width: initial; outline-color: initial; "></strong><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<img src="http://cc.cocimg.com/bbs/attachment/Fid_2/2_1860_667c463ff3cdf18.png" border="0" style="outline-style: none; outline-width: initial; outline-color: initial; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " alt="" /></span>&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<strong style="outline-style: none; outline-width: initial; outline-color: initial; "><span style="outline-style: none; outline-width: initial; outline-color: initial; color: #0000ff; ">2）渐变背景</span></strong><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
NSButtonCell的重绘方法很简单，重写下面的方法即可。<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
逻辑就是<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
1）检测当前button的类型（普通button，checkbox，radiobutton等）<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
2）画button的基本形状和颜色<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
3）如果当前button被click了，那么显然的画一个黄色的圈圈上去<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
4）如果没有被click，那么检测是否为默认button，如果是，并且当前window没有被click的其他button，那么为自己画一个黄色的圈圈，否则不画。<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #ff0000; ">// buttoncell有一个私有方法来标示当前button的类型<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
// 这里只列出关心的三种类型</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
t<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #0000ff; ">ypedef enum KAButtonType{<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;KACheckBox = 3,<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;KARadioButton = 4,<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;KARoundButton = 7<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
};<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
- (void)drawWithFrame: (NSRect)cellFrame inView: (NSView *)controlView<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
{<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
switch ([self _buttonType]) {</span><span style="outline-style: none; outline-width: initial; outline-color: initial; color: #ff0000; ">&nbsp;// buttonCell的私有函数，可以确定button类型，10.4/10.5/10.6都可用</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #0000ff; ">case KACheckBox:<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[self drawCheckInFrame:cellFrame isRadio:NO];&nbsp;</span><span style="outline-style: none; outline-width: initial; outline-color: initial; color: #ff0000; ">// 画checkbox的形状，这里忽略不画</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #0000ff; ">reak;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case KARadioButton:<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[self drawCheckInFrame:cellFrame isRadio:YES];</span>&nbsp;<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #ff0000; ">// 画radiobutton的形状，这里忽略不画</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #0000ff; ">&nbsp;&nbsp;&nbsp;&nbsp;break;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default:<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch ([buttonCell bezelStyle]) {</span><span style="outline-style: none; outline-width: initial; outline-color: initial; color: #0000ff; ">&nbsp;</span><span style="outline-style: none; outline-width: initial; outline-color: initial; color: #ff0000; ">// 这就是button啦，默认的形状，这个参数可以在IB里设置，</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&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;&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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #ff0000; ">// 所以button的类型必须为NSRoundedBezelStyle，当然你可以改为其他的</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #0000ff; ">case NSRoundedBezelStyle:<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[self drawRoundedButtonInFrame: cellFrame inView: controlView];<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case NSRegularSquareBezelStyle:<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[self drawHyperLinkButtonInFrame: cellFrame];<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default:<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #ff0000; ">&nbsp;&nbsp;// 画Button的图片哦<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Comment by yoyokko<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// if [buttonCell _normalImage] is nil, that to say there is a missing&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// field in nib file for this check box --&gt;&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// NSButtonCell uses function &lt;(int)_buttonType&gt; to determine button type.<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// After hacking, I found that 3==Checkbox, 4==Radio, 7==RoundedButton</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #0000ff; ">&nbsp;&nbsp;&nbsp;if([buttonCell _buttonType] == KARoundButton)<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if([buttonCell imagePosition] != NSNoImage) {<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[self drawImage: [buttonCell image] withFrame: cellFrame inView: [buttonCell controlView]];<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
}</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #ff0000; ">// 查询当前window上有没有被click的button</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #6666cc; ">- (void)travelSubViews: (NSView*)view<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
{<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;NSArray *items = [view subviews];<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;NSEnumerator *enumerator = [items objectEnumerator];<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;id anObject = nil;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;while (anObject = [enumerator nextObject])&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;{<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ([anObject isKindOfClass: [NSButton class]])<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NSButtonCell *buttonCell = [anObject cell];<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NSBezelStyle buttonStyle = [buttonCell bezelStyle];<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ([buttonCell isHighlighted] &amp;&amp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(buttonStyle == NSRoundedBezelStyle || buttonStyle == NSTexturedRoundedBezelStyle))<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[self setMIsFound: YES];<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[self travelSubViews: anObject];<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
}</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #ff0000; ">// 画渐变的button和黄色圈圈</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #660000; ">-(void)drawRoundedButtonInFrame:(NSRect)frame inView: (NSView *)controlView<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
{&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;NSRect textFrame;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;//Adjust Rect so strokes are true and<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;//shadows are visible<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;frame.origin.x += .5f;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;frame.origin.y += .5f;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;frame.size.height -= 1;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;frame.size.width -= 1;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;//Adjust Rect based on ControlSize so that<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;//my controls match as closely to apples<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;//as possible.<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;switch ([buttonCell controlSize]) {<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default: // Silence uninitialized variable warnings for textFrame fields.<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case NSRegularControlSize:<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frame.origin.x += 4;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frame.origin.y += 4;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frame.size.width -= 8;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frame.size.height -= 12;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;textFrame = frame;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case NSSmallControlSize:<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frame.origin.x += 4;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frame.origin.y += 4;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frame.size.width -= 8;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frame.size.height -= 11;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;textFrame = frame;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;textFrame.origin.y += 1;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case NSMiniControlSize:<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;frame.origin.y -= 1;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;textFrame = frame;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;textFrame.origin.y += 1;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;}<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;//Create Path<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;NSBezierPath *path = [[NSBezierPath alloc] init];<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;[path appendBezierPathWithRoundedRect: frame cornerRadius:6.0f];<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;if([buttonCell isEnabled])&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// draw inner part of button first</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #ff0000; ">&nbsp;&nbsp;&nbsp;// 画button的灰色渐变部分</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #660000; ">&nbsp;&nbsp;[self drawShadingWithStartingColor: [self colorVlaueWithRed: 239 green: 239 blue: 239]//[NSColor blackColor]<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&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; withEndingColor: [self colorVlaueWithRed: 93 green: 93 blue: 93]//[NSColor whiteColor]<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&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;inBezierPath: path];</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// draw focus ring second<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #ff0000; ">// 当当前button被click时，画那个黄色的圈圈</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// if the button is highlighted, then draw a ring around the button<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #660000; ">&nbsp;if([buttonCell isHighlighted])</span><span style="outline-style: none; outline-width: initial; outline-color: initial; color: #660000; ">&nbsp;</span><span style="outline-style: none; outline-width: initial; outline-color: initial; color: #ff0000; ">// 当button被click时，isHighlighted返回YES</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #660000; ">{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[[self colorVlaueWithRed: 246 green: 186 blue: 55] set];<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[path setLineWidth: 3.0f];<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[path stroke];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #ff0000; ">// button没有被click，那就检查是否为默认的button</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// otherwise, check if it is a default button<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #660000; ">&nbsp;&nbsp;&nbsp;id btnControl = [buttonCell controlView];<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ([btnControl respondsToSelector: @selector(keyEquivalent)] &amp;&amp; [[btnControl keyEquivalent] isEqualToString: @"\r"])<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #ff0000; ">// 如果是默认button</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #660000; ">NSView *superView = controlView;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NSView *tempView = nil;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for (tempView = superView; tempView != nil; tempView = [tempView superview])<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;superView = tempView;</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #ff0000; ">&nbsp;// 找到当前window的contentview</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #660000; ">&nbsp;&nbsp;if (superView)<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[buttonCell setMIsFound:NO];<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[buttonCell travelSubViews: superView];<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #ff0000; ">// 看当前window中有没有被click的button，没有就把自己这个默认button画一个黄圈</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #660000; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (![buttonCell mIsFound])<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[[self colorVlaueWithRed: 246 green: 186 blue: 55] set];<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[path setLineWidth: 3.0f];<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[path stroke];<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[buttonCell setMIsFound:NO];<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;else&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #ff0000; ">// button 没有enable</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #660000; ">&nbsp;&nbsp;&nbsp;[self drawShadingWithStartingColor: [self colorVlaueWithRed: 220 green: 220 blue: 220]//[NSColor blackColor]<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&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; withEndingColor: [self colorVlaueWithRed: 112 green: 112 blue: 112]//[NSColor whiteColor]<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&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;inBezierPath: path];<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;}<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;[path release];</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #ff0000; ">&nbsp;// 画button的text，这里忽略不画</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #660000; ">&nbsp;&nbsp;if([buttonCell imagePosition] != NSImageOnly) {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[self drawTitle: [buttonCell attributedTitle] withFrame: textFrame inView: [buttonCell controlView]];<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;}<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
}</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
至此，所有绘制的代码工作都已经完成了，包括黄色圈圈和点击其他button的行为都写好了～<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
但这样做会有一个问题&#8230;&#8230;<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<strong style="outline-style: none; outline-width: initial; outline-color: initial; "><span style="outline-style: none; outline-width: initial; outline-color: initial; color: #0000ff; ">3）更改系统默认画黄色圈圈的行为</span></strong><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
释下面一段代码的行为，这个很重要，否则会出现非常巧妙的bug&#8230;&#8230;很奇妙，困扰了我两个星期的bug，恨哪～<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<span style="outline-style: none; outline-width: initial; outline-color: initial; color: #0000ff; ">- (void)heartBeat:(CDAnonymousStruct7 *)fp8<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
{<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;id btnControl = [self controlView];<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;if ([btnControl respondsToSelector: @selector(keyEquivalent)] &amp;&amp; [[btnControl keyEquivalent] isEqualToString: @"\r"])// &amp;&amp; !oneButtonClicked)<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;{<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[btnControl setNeedsDisplay:YES];<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
&nbsp;&nbsp;&nbsp;&nbsp;}<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
}</span><br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
首先探索一下系统默认button的一闪一闪的行为是怎么做的，blabla一大堆，经过hack发现，每个程序在起来之后都会启动一个叫做HeartBeat的线程。每个control都有一个heartBeat:的函数。<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
这个线程负责默认button的一闪一闪的刷新，spin的旋转等，所以在你的主界面block住的时候你会发现button还在闪，spin还在转，而你自己用timer写的progressspin是不会转的。对于一个window来说，它上面的button不会一直刷新，只是显示的时候刷几次，而默认button会被heartbeat线程调用一直刷新。<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
问题就出在这里，这是一个线程啊，我们重写了buttoncell的绘制函数，但我们并没有做处理并保证这个函数是原子的调用啊，所以这里会发生非常极品的问题（当用多线程绘制界面时一定要注意是原子操作）<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
首先有一个程序弹出了一个sheet，然后这个sheet上有一个button，点击button会再次弹出一个sheet，不知道是不是apple的这里的消息循环有问题，在点击这个button弹出sheet的同时，button所在的window或者新弹出的window上有的button会被刷成别的形状，比如某个radiobutton的字变成了OK，或者就变成了一个拉长版的普通button，并且只会变成默认button的字或者形状。<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
这就是因为多线程的原因造成的。在刷当前button的时候，heartbeat来捣乱了，不知道怎么搞得就把默认button的字或者形状刷到了当前button的信息上面（button的text就是被改变了）。不太清楚默认的heartBeat：里面做了些什么。<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
所以这里只能重写heartBeat：函数（亦或把重绘函数变成原子的，没试过），在这个函数里面啥都不做，只是检测当前button是否为默认button，是的画就通知主线程来刷新。<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
因为这里只是加一个黄色圈圈而已，所以即使主线程block住也没什么问题。<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
JB，非常JB～<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
PS：在10.4上程序起来时heartbeat线程不能正常起来，所以需要在程序结束launching之后谈一个sheet，再把之关闭就可以了（很奇怪，估计Tiger上的消息循环还是有很大的问题的）。</span>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&amp;file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
<div><span style="border-collapse: collapse; color: #333333; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Arial, Verdana, sans-serif; line-height: 24px; "><br />
</span></div>
<div><span style="border-collapse: collapse; color: #333333; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Arial, Verdana, sans-serif; line-height: 24px; ">From:&nbsp;http://www.cocoachina.com/bbs/read.php?tid=14590</span></div><img src ="http://www.cppblog.com/biao/aggbug/161807.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2011-12-09 04:00 <a href="http://www.cppblog.com/biao/archive/2011/12/09/161807.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cocoa：NSData, NSValue, NSKeyedArchiver and non objects</title><link>http://www.cppblog.com/biao/archive/2011/12/09/161806.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 08 Dec 2011 19:53:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2011/12/09/161806.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/161806.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2011/12/09/161806.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/161806.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/161806.html</trackback:ping><description><![CDATA[<span  style="color: #333333; line-height: 24px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; font-size: medium; ">
<p style="margin-top: 20px; margin-right: 0px; margin-bottom: 20px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; font-size: 14px; line-height: 1.8; ">当你想使用Cocoa的集合来存储非对象型数据时，NSValue和NSNumber是非常有用的。NSNumber是NSValue的子类，所以 NSValue更灵活一些。<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
我们先看看NSValue能做什么：<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<em style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">一个NSValue对 象是用来存储一个C或者Objective－C数据的简单容器。它可以保存任意类型的数据，<strong style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; color: #ff0000; ">比如int，float，char，当然也可以是指pointers, structures, and object ids</span></strong>。NSValue类的目标就是<strong style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; color: #ff0000; ">允许以上数据类型的数据结构能够被添加到集合里</span></strong>，例如那些需要其元素是对象的数据结构，如NSArray或者NSSet 的实例。需要注意的是NSValue对象一直是不可枚举的。</em><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
所以下面的代码是可行的：</p>
<table cellspacing="0" cellpadding="0" width="100%" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">
    <tbody style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">
        <tr style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">
            <td style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">
            <div class="blockquote2" id="code1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana; font-size: 12px; ">
            <pre class="objc" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; word-wrap: break-word; word-break: break-all; ">&nbsp;
            <span class="co1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">// assume ImaginaryNumber defined:</span>
            <span class="kw4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">typedef</span> <span class="kw4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">struct</span> <span class="br0" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">{</span>
            <span class="kw4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">float</span> real;
            <span class="kw4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">float</span> imaginary;
            <span class="br0" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">}</span> ImaginaryNumber;
            &nbsp;
            ImaginaryNumber miNumber;
            miNumber.real <span class="sy0" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">=</span> <span class="nu0" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">1.1</span>;
            miNumber.imaginary <span class="sy0" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">=</span> <span class="nu0" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">1.41</span>;
            &nbsp;
            <span class="kw4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">NSValue</span> <span class="sy0" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">*</span>miValue <span class="sy0" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">=</span> <span class="br0" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">[</span><span class="kw4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">NSValue</span> <span class="kw4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">value</span><span class="sy0" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">:</span>miNumber
            <span class="kw4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">withObjCType</span><span class="sy0" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">:</span><span class="kw4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">@encode</span><span class="br0" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">(</span>ImaginaryNumber<span class="br0" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">)</span><span class="br0" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">]</span>; <span class="co1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">// encode using the type name</span>
            &nbsp;
            ImaginaryNumber miNumber2;
            <span class="br0" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">[</span>miValue <span class="kw4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">getValue</span><span class="sy0" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">:&amp;</span>miNumber2<span class="br0" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">]</span>;
            &nbsp;</pre>
            </div>
            </td>
        </tr>
    </tbody>
</table>
<p style="margin-top: 20px; margin-right: 0px; margin-bottom: 20px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; font-size: 14px; line-height: 1.8; "><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
是不是影像很深刻呢？然而不管怎样，苹果的文档里有一 行看起来有点含混的解释：<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
<em style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">时刻记住你的struct类型必须是定长的。你不可以存储C字符串，不定长数组和结构和其他的一些不定长 的数据类型到NSValue中去。你应该使用NSString或者NSData来存储此类不定长数据。当然你可以把一个指向变长对象的指针存储在 NSValue对象中。</em><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
这是什么意思呢？如果你的数据不是定长的会发生什么？它能被正确的存储下来吗？</p>
<table cellspacing="0" cellpadding="0" width="100%" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">
    <tbody style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">
        <tr style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">
            <td style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">
            <div class="blockquote2" id="code2" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana; font-size: 12px; ">
            <pre class="objc" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; word-wrap: break-word; word-break: break-all; ">&nbsp;
            <span class="kw4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">typedef</span> <span class="kw4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">struct</span> <span class="br0" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">{</span>
            <span class="kw4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">int</span> dataSize;
            <span class="kw4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">char</span> <span class="sy0" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">*</span><span class="kw4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">data</span>;
            <span class="kw4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">int</span> <span class="kw4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">year</span>;
            <span class="br0" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">}</span> myStructType1;
            &nbsp;</pre>
            </div>
            </td>
        </tr>
    </tbody>
</table>
<p style="margin-top: 20px; margin-right: 0px; margin-bottom: 20px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; font-size: 14px; line-height: 1.8; "><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
当data指向一个字符数组时，它能被正确的编码吗？<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
回 答是很简单的，它是变长的，所以它指向的数据不会被编码。<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
只有这个指针地址被编码了。所以，如果你有一个服务线程编码了一个 myStructTyle1的数据发布出去，并释放了这快内存，那么客户线程拿到这个数据解码并试图获取data的原始数据时，那就只能得到data的指 针地址，而不是数据内容。所以不要期望它能存储你的data。你应该使用NSData或者NSArchiver来代替NSValue以达到期望目标。<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
我 们再看这个例子：</p>
<table cellspacing="0" cellpadding="0" width="100%" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">
    <tbody style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">
        <tr style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">
            <td style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">
            <div class="blockquote2" id="code3" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana; font-size: 12px; ">
            <pre class="objc" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; word-wrap: break-word; word-break: break-all; ">&nbsp;
            <span class="kw4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">typedef</span> <span class="kw4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">struct</span> <span class="br0" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">{</span>
            <span class="kw4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">int</span> age;
            <span class="kw4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">int</span> <span class="kw4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">month</span>;
            <span class="kw4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">int</span> <span class="kw4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">day</span>;
            <span class="br0" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">}</span> innerType;
            &nbsp;
            <span class="kw4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">typedef</span> <span class="kw4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">struct</span> <span class="br0" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">{</span>
            <span class="kw4" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">int</span> dataSize;
            innerType <span class="sy0" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">*</span>innerData;
            <span class="br0" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; ">}</span> myStructType2;
            &nbsp;</pre>
            </div>
            </td>
        </tr>
    </tbody>
</table>
<p style="margin-top: 20px; margin-right: 0px; margin-bottom: 20px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; font-size: 14px; line-height: 1.8; "><br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
恩，innerTyle是一个定长的类型变量，那么它会被正确 编码吗？<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
不会，苹果的文档并没有说明此类情况。它依然只编码指针而不是内容。<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
所以在这种情况下，依然得使用NSData。<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
总结， 使用NSValue只能是对那些没有变量是指针的struct。<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
Then how the NSValue stores? It is kind of shallow copy. Please read this.<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
Here the address of myCString is passed (&amp;myCString), so the address of the first character of the string is stored in theValue. Note that the NSValue object doesn&#8217;t copy the contents of the string, but the pointer itself. If you create an NSValue object with an allocated data item, don&#8217;t deallocate its memory while the NSValue object exists.<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝ 我是引用分割线＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
不管是NSValue还是NSData，都是可以对非对象进行编码存储的。但在我的工 程里，线程之间通信的数据是需要进行序列化的，我使用了NSKeyedArchiver来序列化。<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
在使用过程中发现NSValue存储的数据不可 被序列化，而NSData可以。我的struct是定长的。<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
<br style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; " />
所以最好包装时都使用NSData吧，如果时rect， point之类的倒是可以用NSvalue，它已经提供好接口供你使用了。</p>
<p style="margin-top: 20px; margin-right: 0px; margin-bottom: 20px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; font-size: 14px; line-height: 1.8; "><br />
</p>
</span><span  style="border-collapse: collapse; color: #333333; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Arial, Verdana, sans-serif; line-height: 24px; ">用NSValue试了半天.保存CGSize不行.<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
后来改用NSData好了..<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<table cellspacing="0" cellpadding="0" width="80%" style="outline-style: none; outline-width: initial; outline-color: initial; border-collapse: collapse; ">
    <tbody style="outline-style: none; outline-width: initial; outline-color: initial; ">
        <tr style="outline-style: none; outline-width: initial; outline-color: initial; ">
            <td style="outline-style: none; outline-width: initial; outline-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-break: break-all; word-wrap: break-word; ">
            <div class="c" style="outline-style: none; outline-width: initial; outline-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-break: break-all; word-wrap: break-word; clear: both; font: normal normal normal 0px/0px Arial; overflow-x: hidden; overflow-y: hidden; height: 0px; width: 0px; "></div>
            <span class="f10 s8" style="outline-style: none; outline-width: initial; outline-color: initial; font-size: 11px; "><a href="http://www.cocoachina.com/bbs/read.php?tid-13738.html" style="outline-style: none; outline-width: initial; outline-color: initial; text-decoration: none; color: #0070af; ">复制代码</a></span>
            <div class="blockquote2" id="code4" style="outline-style: none; outline-width: initial; outline-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 2em; word-break: break-all; word-wrap: break-word; border-color: initial; border-left-color: #c0c0c0; line-height: 2em; overflow-x: hidden; overflow-y: hidden; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; ">
            <ol style="outline-style: none; outline-width: initial; outline-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 1.5em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: decimal; list-style-position: initial; list-style-image: initial; ">
                <li style="outline-style: none; outline-width: initial; outline-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 1em; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #cccccc; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #f7f7f7; font-size: 12px; list-style-type: decimal; background-position: initial initial; background-repeat: initial initial; "></li>
                <li style="outline-style: none; outline-width: initial; outline-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 1em; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #cccccc; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #f7f7f7; font-size: 12px; list-style-type: decimal; background-position: initial initial; background-repeat: initial initial; ">UIImage *currentImg = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i]];</li>
                <li style="outline-style: none; outline-width: initial; outline-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 1em; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #cccccc; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #f7f7f7; font-size: 12px; list-style-type: decimal; background-position: initial initial; background-repeat: initial initial; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CGSize imageSize = currentImg.size;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li>
                <li style="outline-style: none; outline-width: initial; outline-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 1em; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #cccccc; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #f7f7f7; font-size: 12px; list-style-type: decimal; background-position: initial initial; background-repeat: initial initial; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NSData *pointObjectIn = [NSData dataWithBytes:&amp;imageSize length:sizeof(CGSize)];</li>
                <li style="outline-style: none; outline-width: initial; outline-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 1em; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #cccccc; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #f7f7f7; font-size: 12px; list-style-type: decimal; background-position: initial initial; background-repeat: initial initial; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[persistentArray addObject:pointObjectIn];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</li>
            </ol>
            </div>
            </td>
        </tr>
    </tbody>
</table>
<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<br style="outline-style: none; outline-width: initial; outline-color: initial; " />
<table cellspacing="0" cellpadding="0" width="80%" style="outline-style: none; outline-width: initial; outline-color: initial; border-collapse: collapse; ">
    <tbody style="outline-style: none; outline-width: initial; outline-color: initial; ">
        <tr style="outline-style: none; outline-width: initial; outline-color: initial; ">
            <td style="outline-style: none; outline-width: initial; outline-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-break: break-all; word-wrap: break-word; ">
            <div class="c" style="outline-style: none; outline-width: initial; outline-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-break: break-all; word-wrap: break-word; clear: both; font: normal normal normal 0px/0px Arial; overflow-x: hidden; overflow-y: hidden; height: 0px; width: 0px; "></div>
            <span class="f10 s8" style="outline-style: none; outline-width: initial; outline-color: initial; font-size: 11px; "><a href="http://www.cocoachina.com/bbs/read.php?tid-13738.html" style="outline-style: none; outline-width: initial; outline-color: initial; text-decoration: none; color: #0070af; ">复制代码</a></span>
            <div class="blockquote2" id="code5" style="outline-style: none; outline-width: initial; outline-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 2em; word-break: break-all; word-wrap: break-word; border-color: initial; border-left-color: #c0c0c0; line-height: 2em; overflow-x: hidden; overflow-y: hidden; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #ffffff; ">
            <ol style="outline-style: none; outline-width: initial; outline-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 1.5em; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; list-style-type: decimal; list-style-position: initial; list-style-image: initial; ">
                <li style="outline-style: none; outline-width: initial; outline-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 1em; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #cccccc; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #f7f7f7; font-size: 12px; list-style-type: decimal; background-position: initial initial; background-repeat: initial initial; ">NSData* getImgeData = [arrayImageCGSize objectAtIndex:i] ;</li>
                <li style="outline-style: none; outline-width: initial; outline-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 1em; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #cccccc; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #f7f7f7; font-size: 12px; list-style-type: decimal; background-position: initial initial; background-repeat: initial initial; ">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CGSize imageSize = *(CGSize*)[getImgeData bytes];</li>
                <li style="outline-style: none; outline-width: initial; outline-color: initial; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 1em; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #cccccc; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #f7f7f7; font-size: 12px; list-style-type: decimal; background-position: initial initial; background-repeat: initial initial; "></li>
            </ol>
            </div>
            </td>
        </tr>
    </tbody>
</table>
</span><span  style="color: #333333; line-height: 24px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; font-size: medium; ">
<p style="margin-top: 20px; margin-right: 0px; margin-bottom: 20px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Lucida Grande', Geneva, Arial, Verdana, sans-serif; font-size: 14px; line-height: 1.8; ">From:&nbsp;http://www.cocoachina.com/bbs/read.php?tid-13738.html</p>
</span>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/161806.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2011-12-09 03:53 <a href="http://www.cppblog.com/biao/archive/2011/12/09/161806.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cocoa：用NSBeginAlertSheet来做代理控制应用程序是否退出</title><link>http://www.cppblog.com/biao/archive/2011/12/09/161805.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 08 Dec 2011 19:05:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2011/12/09/161805.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/161805.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2011/12/09/161805.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/161805.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/161805.html</trackback:ping><description><![CDATA[<span  style="font-family: 'times new roman', times, serif; line-height: 18px; ">
<div class="entry-content">
<p style="margin-top: 1em; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; ">在创建Cocoa应用程序的时候，有时候我们需要在应用程序退出前让用户确认是否真正退出。这个可以用多种方法来实现。但是如果利用AlertSheet的话可以尽量使得过程更舒服。如下图。</p>
<p style="margin-top: 1em; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; "><img class="aligncenter size-full wp-image-170" title="application-quit-confirm-sheet" src="http://yeeyaa.me/wp-content/uploads/2011/03/application-quit-confirm-sheet.png" alt="" width="484" height="392" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; max-width: 99%; display: block; margin-top: 1em; margin-right: auto; margin-bottom: 1em; margin-left: auto; " />我们可以利用xcode里面已经为你创建好的Application的代理类来调用NSBeginAlertSheer来做做代理处理请求。接下来我们来看看具体的做法。</p>
<p style="margin-top: 1em; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; "><span id="more-169"></span></p>
<p style="margin-top: 1em; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; ">在Interface Builder里面从Application按下Control拉一条线到已经有的代理类上面，连接delegate的outlet。如下图：</p>
<p style="margin-top: 1em; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; "><img class="aligncenter size-full wp-image-172" title="application-delegate-nib-view" src="http://yeeyaa.me/wp-content/uploads/2011/03/application-delegate-nib-view.png" alt="" width="286" height="219" style="border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; border-width: initial; border-color: initial; max-width: 99%; display: block; margin-top: 1em; margin-right: auto; margin-bottom: 1em; margin-left: auto; " /></p>
<p style="margin-top: 1em; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; ">然后转向代理类的代码中加入如下代码：</p>
<div class="wp_syntax" style="color: #110000; background-color: #f9f9f9; border-left-color: silver; margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; overflow-x: auto; overflow-y: hidden; width: 792px; ">
<div class="code" style="vertical-align: top; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px; ">
<pre class="objc" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; width: auto; float: none; clear: none; overflow-x: visible; overflow-y: visible; font-size: 12px; line-height: 1.333; white-space: pre; font: normal normal normal 1em/130% 'courier new', courier, monospace; font-family: monospace; "><span style="color: #002200; ">-</span> <span style="color: #002200; ">(</span>NSApplicationTerminateReply<span style="color: #002200; ">)</span>applicationShouldTerminate<span style="color: #002200; ">:</span><span style="color: #002200; ">(</span><span style="color: #400080; ">NSApplication</span> <span style="color: #002200; ">*</span><span style="color: #002200; ">)</span>sender
<span style="color: #002200; ">{</span>
<span style="color: #400080; ">NSString</span> <span style="color: #002200; ">*</span>msg <span style="color: #002200; ">=</span> <span style="color: #bf1d1a; ">@</span><span style="color: #bf1d1a; ">"Should this applicaion quit?"</span>;
<span style="color: #a61390; ">SEL</span> sel <span style="color: #002200; ">=</span> <span style="color: #a61390; ">@selector</span><span style="color: #002200; ">(</span>sheetClosed<span style="color: #002200; ">:</span>returnCode<span style="color: #002200; ">:</span>contextInfo<span style="color: #002200; ">:</span><span style="color: #002200; ">)</span>;
&nbsp;
NSBeginAlertSheet<span style="color: #002200; ">(</span><span style="color: #bf1d1a; ">@</span><span style="color: #bf1d1a; ">"Close"</span>,
<span style="color: #bf1d1a; ">@</span><span style="color: #bf1d1a; ">"OK"</span>,
<span style="color: #bf1d1a; ">@</span><span style="color: #bf1d1a; ">"Cancel"</span>,
<span style="color: #a61390; ">nil</span>,
window,
self,
sel,
<span style="color: #a61390; ">NULL</span>,
sender,
msg,
<span style="color: #a61390; ">nil</span><span style="color: #002200; ">)</span>;
<span style="color: #a61390; ">return</span> NSTerminateLater;
<span style="color: #002200; ">}</span>
&nbsp;
<span style="color: #002200; ">-</span> <span style="color: #002200; ">(</span><span style="color: #a61390; ">void</span><span style="color: #002200; ">)</span>sheetClosed<span style="color: #002200; ">:</span><span style="color: #002200; ">(</span><span style="color: #400080; ">NSWindow</span> <span style="color: #002200; ">*</span><span style="color: #002200; ">)</span>sheet returnCode<span style="color: #002200; ">:</span><span style="color: #002200; ">(</span><span style="color: #a61390; ">int</span><span style="color: #002200; ">)</span>returnCode contextInfo<span style="color: #002200; ">:</span><span style="color: #002200; ">(</span><span style="color: #a61390; ">void</span> <span style="color: #002200; ">*</span><span style="color: #002200; ">)</span>contextInfo
<span style="color: #002200; ">{</span>
<span style="color: #a61390; ">if</span> <span style="color: #002200; ">(</span>returnCode <span style="color: #002200; ">==</span> NSAlertDefaultReturn<span style="color: #002200; ">)</span>
<span style="color: #002200; ">{</span>
<span style="color: #002200; ">[</span><span style="color: #002200; ">(</span><span style="color: #400080; ">NSApplication</span> <span style="color: #002200; ">*</span><span style="color: #002200; ">)</span>contextInfo replyToApplicationShouldTerminate<span style="color: #002200; ">:</span><span style="color: #a61390; ">YES</span><span style="color: #002200; ">]</span>;
<span style="color: #002200; ">}</span>
<span style="color: #a61390; ">else</span>
<span style="color: #002200; ">{</span>
<span style="color: #002200; ">[</span><span style="color: #002200; ">(</span><span style="color: #400080; ">NSApplication</span> <span style="color: #002200; ">*</span><span style="color: #002200; ">)</span>contextInfo replyToApplicationShouldTerminate<span style="color: #002200; ">:</span><span style="color: #a61390; ">NO</span><span style="color: #002200; ">]</span>;
<span style="color: #002200; ">}</span>
<span style="color: #002200; ">}</span></pre>
</div>
</div>
<p style="margin-top: 1em; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; ">其中：</p>
<div class="wp_syntax" style="color: #110000; background-color: #f9f9f9; border-left-color: silver; margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; overflow-x: auto; overflow-y: hidden; width: 792px; ">
<div class="code" style="vertical-align: top; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px; ">
<pre class="objc" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; width: auto; float: none; clear: none; overflow-x: visible; overflow-y: visible; font-size: 12px; line-height: 1.333; white-space: pre; font: normal normal normal 1em/130% 'courier new', courier, monospace; font-family: monospace; "><span style="color: #002200; ">-</span> <span style="color: #002200; ">(</span>NSApplicationTerminateReply<span style="color: #002200; ">)</span>applicationShouldTerminate<span style="color: #002200; ">:</span><span style="color: #002200; ">(</span><span style="color: #400080; ">NSApplication</span> <span style="color: #002200; ">*</span><span style="color: #002200; ">)</span>sender;</pre>
</div>
</div>
<p style="margin-top: 1em; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; ">是Application类得到是否退出消息的控制函数。其返回值为：<code style="font: normal normal normal 1em/130% 'courier new', courier, monospace; ">NSTerminateNow</code>,&nbsp;<code style="font: normal normal normal 1em/130% 'courier new', courier, monospace; ">NSTerminateCancel</code>和<code style="font: normal normal normal 1em/130% 'courier new', courier, monospace; ">NSTerminateLater</code>。前两个顾名思义，就是立刻退出和取消推出。最后一个则被用来使用回调/选择函数处理后再用<code style="font: normal normal normal 1em/130% 'courier new', courier, monospace; ">replyToApplicationShouldTerminate</code>来决定程序是否退出。</p>
<p style="margin-top: 1em; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; ">下面就是如何用<code style="font: normal normal normal 1em/130% 'courier new', courier, monospace; ">NSBeginAlertSheet</code>来接受用户选择以决定程序是否退出。其函数原型定义如下：</p>
<div class="wp_syntax" style="color: #110000; background-color: #f9f9f9; border-left-color: silver; margin-top: 0px; margin-right: 0px; margin-bottom: 1.5em; margin-left: 0px; overflow-x: auto; overflow-y: hidden; width: 792px; ">
<div class="code" style="vertical-align: top; padding-top: 2px; padding-right: 4px; padding-bottom: 2px; padding-left: 4px; ">
<pre class="objc" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; width: auto; float: none; clear: none; overflow-x: visible; overflow-y: visible; font-size: 12px; line-height: 1.333; white-space: pre; font: normal normal normal 1em/130% 'courier new', courier, monospace; font-family: monospace; "><span style="color: #a61390; ">void</span> NSBeginCriticalAlertSheet <span style="color: #002200; ">(</span>
<span style="color: #400080; ">NSString</span> <span style="color: #002200; ">*</span>title, <span style="color: #11740a; font-style: italic; ">//标题</span>
<span style="color: #400080; ">NSString</span> <span style="color: #002200; ">*</span>defaultButton, <span style="color: #11740a; font-style: italic; ">//默认按钮</span>
<span style="color: #400080; ">NSString</span> <span style="color: #002200; ">*</span>alternateButton, <span style="color: #11740a; font-style: italic; ">//第二按钮</span>
<span style="color: #400080; ">NSString</span> <span style="color: #002200; ">*</span>otherButton, <span style="color: #11740a; font-style: italic; ">//其他按钮</span>
<span style="color: #400080; ">NSWindow</span> <span style="color: #002200; ">*</span>docWindow, <span style="color: #11740a; font-style: italic; ">//用于attach该sheet的窗体</span>
<span style="color: #a61390; ">id</span> modalDelegate, <span style="color: #11740a; font-style: italic; ">//modal代理</span>
<span style="color: #a61390; ">SEL</span> didEndSelector, <span style="color: #11740a; font-style: italic; ">//Selector结束时（sheet消失前）调用的modalDelegate中的函数</span>
<span style="color: #a61390; ">SEL</span> didDismissSelector, <span style="color: #11740a; font-style: italic; ">//Selector结束时（sheet消失前）调用的modalDelegate中的函数</span>
<span style="color: #a61390; ">void</span> <span style="color: #002200; ">*</span>contextInfo, <span style="color: #11740a; font-style: italic; ">//传给didEnd和didDismiss的额外数据</span>
<span style="color: #400080; ">NSString</span> <span style="color: #002200; ">*</span>msg, <span style="color: #11740a; font-style: italic; ">//sheet的提示内容</span>
...
<span style="color: #002200; ">)</span>;</pre>
</div>
</div>
<p style="margin-top: 1em; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; ">我们把当前的类作为<code style="font: normal normal normal 1em/130% 'courier new', courier, monospace; ">modalDelegate</code>。然后用其中的<code style="font: normal normal normal 1em/130% 'courier new', courier, monospace; ">sheetClosed</code>函数来作为didEnd的回调函数。最后在<code style="font: normal normal normal 1em/130% 'courier new', courier, monospace; ">applicationShouldTerminate</code>最后返回<code style="font: normal normal normal 1em/130% 'courier new', courier, monospace; ">NSTerminateLater</code>来等待做进一步的处理。</p>
<p style="margin-top: 1em; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; ">在我们的实际判断过程中。通过判断用户按下的是否是<code style="font: normal normal normal 1em/130% 'courier new', courier, monospace; ">NSAlertDefaultReturn</code>来判断用户选择的按钮。如果是默认的退出按钮，则给<code style="font: normal normal normal 1em/130% 'courier new', courier, monospace; ">replyToApplicationShouldTerminate</code>传YES以立刻退出，否则传NO取消退出。</p>
<div><br />
</div>
<div>From:&nbsp;http://yeeyaa.me/articles/2011/03/confirm-application-quit-using-nsbeginalertsheet-as-delegate/</div>
</div>
</span>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/161805.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2011-12-09 03:05 <a href="http://www.cppblog.com/biao/archive/2011/12/09/161805.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cocoa：生成随机数</title><link>http://www.cppblog.com/biao/archive/2011/12/07/161704.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Wed, 07 Dec 2011 15:23:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2011/12/07/161704.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/161704.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2011/12/07/161704.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/161704.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/161704.html</trackback:ping><description><![CDATA[<span  style="color: #545454; font-size: 12px; line-height: 19px; font-family: ff-tisa-web-pro-1, ff-tisa-web-pro-2, 'Lucida Grande', 'Hiragino Sans GB', 'Hiragino Sans GB W3'; ">
<table cellspacing="0" cellpadding="0" id="blogContentTable" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-collapse: collapse; table-layout: fixed; width: 878px; position: relative; ">
    <tbody style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">
        <tr style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">
            <td valign="top" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; ">
            <div id="blogContainer" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; position: relative; overflow-x: hidden; overflow-y: hidden; height: 467px; ">
            <div id="paperTitleArea" align="center" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span id="paperTitle" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; display: block; word-break: break-all; font-weight: bolder; "></span></div>
            <div id="blogDetailDiv" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 14px; ">
            <div class="blog_details_20110920" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-collapse: collapse; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; line-height: 18px; ">You should use the arc4random() function. It uses a superior algorithm to rand.&nbsp;<strong style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">You don't even need to set a seed</strong>.</span>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-collapse: collapse; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; line-height: 18px; ">
            <pre class="lang-c prettyprint" style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #eeeeee; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; overflow-x: auto; overflow-y: auto; width: auto; max-height: 600px; "><code style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-style: normal; font-weight: normal; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #eeeeee; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; "><span class="pln" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #000000; ">arc4random</span><span class="pun" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #000000; ">,</span><span class="pln" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #000000; "> arc4random_stir</span><span class="pun" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #000000; ">,</span><span class="pln" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #000000; "> arc4random_addrandom </span><span class="pun" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #000000; ">--</span><span class="pln" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #000000; "> arc4 random number generator</span></code></pre>
            </span></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-collapse: collapse; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; line-height: 18px; "><br />
            </span></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-collapse: collapse; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; line-height: 18px; ">Same as C, you would do<br />
            </span></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-collapse: collapse; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; line-height: 18px; ">
            <pre class="lang-c prettyprint" style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #eeeeee; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; overflow-x: auto; overflow-y: auto; width: auto; max-height: 600px; "><code style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-style: normal; font-weight: normal; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #eeeeee; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; "><span class="kwd" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #00008b; ">int</span><span class="pln" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #000000; "> r </span><span class="pun" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #000000; ">=</span><span class="pln" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #000000; "> rand</span><span class="pun" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #000000; ">()</span><span class="pln" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #000000; "> </span><span class="pun" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #000000; ">%</span><span class="pln" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #000000; "> </span><span class="lit" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #800000; ">74; // [0, 74)</span></code></pre>
            </span></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-collapse: collapse; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; line-height: 18px; "><br />
            </span></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-collapse: collapse; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; line-height: 18px; ">Feel free to substitute&nbsp;<code style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 1px; padding-right: 5px; padding-bottom: 1px; padding-left: 5px; font-style: normal; font-weight: normal; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #eeeeee; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; ">random()</code>&nbsp;or&nbsp;<code style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 1px; padding-right: 5px; padding-bottom: 1px; padding-left: 5px; font-style: normal; font-weight: normal; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #eeeeee; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; ">arc4random()</code>&nbsp;for&nbsp;<code style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 1px; padding-right: 5px; padding-bottom: 1px; padding-left: 5px; font-style: normal; font-weight: normal; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #eeeeee; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; ">rand()</code>&nbsp;(which is, as others have pointed out, quite sucky).<br />
            </span></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-collapse: collapse; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; line-height: 18px; ">
            <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 14px/normal Monaco; color: #ce00a4; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #470084; ">srandom</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #000000; ">((</span>unsigned<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #000000; ">&nbsp;</span>int<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #000000; ">)</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #470084; ">time</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #000000; ">(</span>NULL<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #000000; ">)); // 防止出现警告</span></p>
            <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 14px/normal Monaco; color: #ce00a4; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #000000; "><br />
            </span></p>
            <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 14px/normal Monaco; color: #ce00a4; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #000000; "><br />
            </span></p>
            </span></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-collapse: collapse; line-height: 18px; ">
            <pre class="lang-c prettyprint" style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 5px; padding-right: 5px; padding-bottom: 5px; padding-left: 5px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #eeeeee; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; overflow-x: auto; overflow-y: auto; width: auto; max-height: 600px; "><code style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-style: normal; font-weight: normal; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #eeeeee; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; "><span class="pln" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #000000; ">arc4random_uniform</span><span class="pun" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #000000; ">(</span><span class="lit" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #800000; ">74</span><span class="pun" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #000000; ">); // [0, 74)</span><span class="pln" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: #000000; "><br />
            </span></code></pre>
            <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; clear: both; word-wrap: break-word; "><code style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 1px; padding-right: 5px; padding-bottom: 1px; padding-left: 5px; font-style: normal; font-weight: normal; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #eeeeee; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; ">arc4random_uniform(upper_bound)</code>&nbsp;avoids modulo bias as described in the man page:</p>
            <blockquote style="margin-top: 0px; margin-right: 0px; margin-bottom: 10px; margin-left: 0px; padding-top: 10px; padding-right: 10px; padding-bottom: 1px; padding-left: 10px; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: #eeeeee; quotes: none; ">
            <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 1em; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; clear: both; word-wrap: break-word; ">arc4random_uniform() will return a uniformly distributed random number less than upper_bound. arc4random_uniform() is recommended over constructions like ``arc4random() % upper_bound'' as it avoids "modulo bias" when the upper bound is not a power of two.</p>
            </blockquote></span></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-collapse: collapse; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; line-height: 18px; "><br />
            </span></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-collapse: collapse; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; line-height: 18px; ">生成[0, 1)之间的随机数</span></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-collapse: collapse; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; line-height: 18px; ">
            <p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 14px/normal Monaco; color: #470084; ">arc4random_uniform<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #000000; ">(</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #3c00dc; ">100</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #000000; ">) /&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #3c00dc; ">100.0</span></p>
            </span></div>
            </div>
            </div>
            </div>
            </td>
        </tr>
    </tbody>
</table>
</span>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/161704.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2011-12-07 23:23 <a href="http://www.cppblog.com/biao/archive/2011/12/07/161704.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cocoa：10个迷惑新手的Cocoa，Objective-c开发难点和问题</title><link>http://www.cppblog.com/biao/archive/2011/12/07/161666.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Wed, 07 Dec 2011 07:07:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2011/12/07/161666.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/161666.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2011/12/07/161666.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/161666.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/161666.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 在你继续深入学习之前，请停下脚步弄清这些问题。如果你是新手，这个教程不要希望一次能看的非常透彻，学一定阶段反回来再看看又会有新的体会的。1. c,c++ background很多人问 &#8220;没有任何语言基础，我不想学c直接学objective-c&#8221;这里简单几句，objc 90%代码是c、众多开源代码是c,c++。你不学好c在unix世界里只能是个二流开发者！...&nbsp;&nbsp;<a href='http://www.cppblog.com/biao/archive/2011/12/07/161666.html'>阅读全文</a><img src ="http://www.cppblog.com/biao/aggbug/161666.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2011-12-07 15:07 <a href="http://www.cppblog.com/biao/archive/2011/12/07/161666.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cocoa：CoreData实例分析学习（1）</title><link>http://www.cppblog.com/biao/archive/2011/12/07/161638.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Wed, 07 Dec 2011 00:49:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2011/12/07/161638.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/161638.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2011/12/07/161638.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/161638.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/161638.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: @import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);Core Data是个好东西，在数据储存操作上速度快，容易操作，是一种类似关系数据库的东西。但是有些不那...&nbsp;&nbsp;<a href='http://www.cppblog.com/biao/archive/2011/12/07/161638.html'>阅读全文</a><img src ="http://www.cppblog.com/biao/aggbug/161638.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2011-12-07 08:49 <a href="http://www.cppblog.com/biao/archive/2011/12/07/161638.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cocoa：Objective-C</title><link>http://www.cppblog.com/biao/archive/2011/12/02/161308.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 01 Dec 2011 16:17:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2011/12/02/161308.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/161308.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2011/12/02/161308.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/161308.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/161308.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Cocoa Dev CentralObjective-CObjective-C is the primary language used to write Mac software. If you're comfortable with basic object-oriented concepts and the C language, Objective-C will make a ...&nbsp;&nbsp;<a href='http://www.cppblog.com/biao/archive/2011/12/02/161308.html'>阅读全文</a><img src ="http://www.cppblog.com/biao/aggbug/161308.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2011-12-02 00:17 <a href="http://www.cppblog.com/biao/archive/2011/12/02/161308.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cocoa：Introduction to Quartz II</title><link>http://www.cppblog.com/biao/archive/2011/12/02/161307.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 01 Dec 2011 16:16:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2011/12/02/161307.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/161307.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2011/12/02/161307.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/161307.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/161307.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Cocoa Dev CentralIntro to Quartz IIThis tutorial is the second entry in the&nbsp;Introduction to Quartz&nbsp;series.Intro to Quartz Part I&nbsp;describes ideas that are essential to understand b...&nbsp;&nbsp;<a href='http://www.cppblog.com/biao/archive/2011/12/02/161307.html'>阅读全文</a><img src ="http://www.cppblog.com/biao/aggbug/161307.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2011-12-02 00:16 <a href="http://www.cppblog.com/biao/archive/2011/12/02/161307.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cocoa：Introduction to Quartz I</title><link>http://www.cppblog.com/biao/archive/2011/12/02/161306.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 01 Dec 2011 16:15:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2011/12/02/161306.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/161306.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2011/12/02/161306.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/161306.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/161306.html</trackback:ping><description><![CDATA[<div style="background-color: #e8e8e8; color: #303030; background-image: url(http://www.cocoadevcentral.com/images/layout/background-glow.png); font-family: 'Lucida Grande', sans-serif; font-size: 11px; line-height: normal; background-repeat: repeat no-repeat; ">
<div id="masthead" style="margin-left: auto; margin-right: auto; width: 369px; text-align: center; "><img src="http://www.cocoadevcentral.com/images/articles/000090/title-learn-quartz.png" width="394" height="60" alt="Learn Quartz" style="margin-top: 52px; margin-bottom: 20px; " /></div>
<div class="contentcontainer" style="width: 652px; margin-top: 30px; margin-right: auto; margin-bottom: 0px; margin-left: auto; ">
<div class="contentsection" style="margin-top: 60px; width: 650px; background-color: #f5f5f5; border-left-color: #bcbcbc; line-height: 21px; padding-top: 21px; padding-bottom: 0px; ">
<h3 style="font-family: 'Helvetica Neue', 'Lucida Grande', sans-serif; font-weight: bold; color: #005bb1; padding-left: 21px; float: left; margin-top: 0px; width: 190px; font-size: 16px; ">Introduction to Quartz</h3>
<div class="contentbody" style="margin-left: 190px; color: #636363; font-size: 12px; padding-right: 20px; padding-left: 21px; ">Quartz is at the center of all graphics in Cocoa. It provides basic graphics data structures and drawing routines, as well Mac OS X's window server.	<br />
<br />
This beginner-level tutorial introduces basic Cocoa graphics concepts: rectangles, points, colors, and coordinate systems.&nbsp;<br />
<br />
This tutorial is written and illustrated by&nbsp;<a href="http://theocacao.com/" style="color: #005bb1; text-decoration: none; ">Scott Stevenson</a><br />
</div>
<div style="clear: both; ">&nbsp;</div>
<div class="credits" style="margin-left: 0px; color: #636363; font-size: 11px; padding-top: 10px; padding-right: 20px; padding-bottom: 20px; padding-left: 211px; ">Copyright &#169; 2006 Scott Stevenson</div>
</div>
<div class="contentsection codesection" style="margin-top: 60px; width: 650px; background-color: #f5f5f5; border-left-color: #bcbcbc; line-height: 21px; padding-top: 21px; padding-bottom: 21px; ">
<h3 style="font-family: 'Helvetica Neue', 'Lucida Grande', sans-serif; font-weight: bold; color: #005bb1; padding-left: 21px; float: none; margin-top: 0px; width: auto; font-size: 16px; ">Rectangles and Points</h3>
<div class="contentbody" style="margin-left: 0px; color: #636363; font-size: 12px; padding-right: 190px; padding-left: 21px; ">All drawing in Quartz involves rectangles. In Cocoa, you use the&nbsp;<cite style="color: #005bb1; font-style: normal; ">NSRect</cite>struct to describe a rectangle's location and dimensions:&nbsp;<br />
<br />
(Rects don't automatically draw themselves. These are just diagrams to go with the examples.)</div>
<div class="cl" style="clear: both; ">&nbsp;</div>
<div class="codeimagecombo" style="background-color: #ebebeb; padding-top: 21px; padding-right: 21px; padding-bottom: 0px; padding-left: 21px; margin-bottom: 42px; ">
<div class="codeblock" style="color: #424242; background-color: #ebebeb; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 12px; font-family: Monaco, monospace; white-space: pre; margin-bottom: 0px; line-height: 18px; margin-top: 0px; margin-right: 0px; margin-left: 0px; float: left; width: 298px; ">typedef struct {
NSPoint origin;
NSSize size;
} <em style="color: #005bb1; font-style: normal; ">NSRect</em>;
<strong style="color: #787878; padding-top: 2px; padding-right: 8px; padding-bottom: 2px; padding-left: 2px; background-color: #f2f2f2; font-weight: normal; ">// make a rect at (0,0) which is 20x20</strong>
<em style="color: #005bb1; font-style: normal; ">NSRect</em> myRect;
myRect.origin.x    = 0;
myRect.origin.y    = 0;
myRect.size.width  = 20;
myRect.size.height = 20;</div>
<img src="http://www.cocoadevcentral.com/images/articles/000090/quartz-rect.png" style="float: right; "  alt="" />
<div class="cl" style="clear: both; ">&nbsp;</div>
</div>
<div class="contentbody" style="margin-left: 0px; color: #636363; font-size: 12px; padding-right: 190px; padding-left: 21px; ">The&nbsp;<em style="font-style: normal; color: black; ">origin</em>&nbsp;field is the "anchor point" of the rect, where the drawing starts. A point is described by&nbsp;<cite style="color: #005bb1; font-style: normal; ">NSPoint</cite>, which has x and y coordinates:</div>
<div class="cl" style="clear: both; ">&nbsp;</div>
<div class="codeimagecombo" style="background-color: #ebebeb; padding-top: 21px; padding-right: 21px; padding-bottom: 0px; padding-left: 21px; margin-bottom: 42px; ">
<div class="codeblock" style="color: #424242; background-color: #ebebeb; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 12px; font-family: Monaco, monospace; white-space: pre; margin-bottom: 0px; line-height: 18px; margin-top: 0px; margin-right: 0px; margin-left: 0px; float: left; width: 298px; ">typedef struct {
float x;
float y;
} <em style="color: #005bb1; font-style: normal; ">NSPoint</em>;
<strong style="color: #787878; padding-top: 2px; padding-right: 8px; padding-bottom: 2px; padding-left: 2px; background-color: #f2f2f2; font-weight: normal; ">// make three points on the canvas</strong>
<em style="color: #005bb1; font-style: normal; ">NSPoint</em> point1;
point1.x = 4;
point1.y = 11;
<em style="color: #005bb1; font-style: normal; ">NSPoint</em> point2;
point2.x = 12;
point2.y = 21;
<em style="color: #005bb1; font-style: normal; ">NSPoint</em> point3;
point3.x = 19;
point3.y = 8;</div>
<img src="http://www.cocoadevcentral.com/images/articles/000090/quartz-point.png" style="float: right; margin-top: 42px; "  alt="" />
<div class="cl" style="clear: both; ">&nbsp;</div>
</div>
<div class="contentbody" style="margin-left: 0px; color: #636363; font-size: 12px; padding-right: 190px; padding-left: 21px; ">The&nbsp;<em style="font-style: normal; color: black; ">size</em>&nbsp;field of a rect is an&nbsp;<cite style="color: #005bb1; font-style: normal; ">NSSize</cite>, which holds a width and a height. There's no way to depict an instance of NSSize, it has to be part of a rect to be useful.</div>
<div style="clear: both; ">&nbsp;</div>
<div class="codeblock" style="color: #424242; background-color: #ebebeb; padding-top: 11px; padding-right: 21px; padding-bottom: 11px; padding-left: 21px; font-size: 12px; font-family: Monaco, monospace; white-space: pre; margin-bottom: 42px; line-height: 18px; ">typedef struct {
float width;
float height;
} <em style="color: #005bb1; font-style: normal; ">NSSize</em>;</div>
<div class="contentbody" style="margin-left: 0px; color: #636363; font-size: 12px; padding-right: 190px; padding-left: 21px; ">Much of 2D drawing in Cocoa is based on these three structs. Remember these are&nbsp;<em style="font-style: normal; color: black; ">not Objective-C classes</em>. You can't call methods on them directly, but there are functions that go with them.&nbsp;<br />
<br />
All measurements in Quartz are&nbsp;<em style="font-style: normal; color: black; ">float</em>&nbsp;values, which gives you finer control of drawing than integer-based coordinates.</div>
</div>
<div class="contentsection codesection" style="margin-top: 60px; width: 650px; background-color: #f5f5f5; border-left-color: #bcbcbc; line-height: 21px; padding-top: 21px; padding-bottom: 21px; ">
<h3 style="font-family: 'Helvetica Neue', 'Lucida Grande', sans-serif; font-weight: bold; color: #005bb1; padding-left: 21px; float: none; margin-top: 0px; width: auto; font-size: 16px; ">Convenience Functions</h3>
<div class="contentbody" style="margin-left: 0px; color: #636363; font-size: 12px; padding-right: 190px; padding-left: 21px; ">Cocoa has a number of functions for creating geometry structs. Most of them are listed in Foundation's&nbsp;<em style="font-style: normal; color: black; ">NSGeometry.h</em>&nbsp;file.</div>
<div style="clear: both; ">&nbsp;</div>
<div class="codeblock" style="color: #424242; background-color: #ebebeb; padding-top: 11px; padding-right: 21px; padding-bottom: 11px; padding-left: 21px; font-size: 12px; font-family: Monaco, monospace; white-space: pre; margin-bottom: 42px; line-height: 18px; "><strong style="color: #787878; padding-top: 2px; padding-right: 8px; padding-bottom: 2px; padding-left: 2px; background-color: #f2f2f2; font-weight: normal; ">// make a point at coordinate 20,20</strong>
NSPoint newPoint = <em style="color: #005bb1; font-style: normal; ">NSMakePoint</em> ( 20, 20 );
<strong style="color: #787878; padding-top: 2px; padding-right: 8px; padding-bottom: 2px; padding-left: 2px; background-color: #f2f2f2; font-weight: normal; ">// make a size of 100 wide x 100 high</strong>
NSSize  newSize  = <em style="color: #005bb1; font-style: normal; ">NSMakeSize</em>  ( 100, 100 );
<strong style="color: #787878; padding-top: 2px; padding-right: 8px; padding-bottom: 2px; padding-left: 2px; background-color: #f2f2f2; font-weight: normal; ">// use the previous point and size to make a rect</strong>
NSRect  newRect  = <em style="color: #005bb1; font-style: normal; ">NSMakeRect</em> ( newPoint.x,
newPoint.y,
newSize.width,
newSize.height );
<strong style="color: #787878; padding-top: 2px; padding-right: 8px; padding-bottom: 2px; padding-left: 2px; background-color: #f2f2f2; font-weight: normal; ">// also can just do this</strong>
NSRect  newRect  = <em style="color: #005bb1; font-style: normal; ">NSMakeRect</em> ( 20, 20, 100, 100 );</div>
<div class="contentbody" style="margin-left: 0px; color: #636363; font-size: 12px; padding-right: 190px; padding-left: 21px; ">Using these functions instead of creating the structs manually makes the code a bit more obvious and makes searching easier.</div>
<div class="cl" style="clear: both; ">&nbsp;</div>
</div>
<div class="contentsection" style="margin-top: 60px; width: 650px; background-color: #f5f5f5; border-left-color: #bcbcbc; line-height: 21px; padding-top: 21px; padding-bottom: 21px; ">
<h3 style="font-family: 'Helvetica Neue', 'Lucida Grande', sans-serif; font-weight: bold; color: #005bb1; padding-left: 21px; float: left; margin-top: 0px; width: 190px; font-size: 16px; ">Coordinates in Quartz</h3>
<div class="contentbody" style="margin-left: 190px; color: #636363; font-size: 12px; padding-right: 20px; padding-left: 21px; ">The drawing area of a view in Cocoa is treated as a rect. Quartz calls this drawing area the "bounds." An NSPoint can represent any location in the view bounds.&nbsp;<br />
<br />
The&nbsp;<cite style="color: #005bb1; font-style: normal; ">standard</cite>&nbsp;Quartz coordinate system is based on PDF model, which means drawing in a view starts in the&nbsp;<em style="font-style: normal; color: black; ">bottom-left</em>. This is what you see in geometry textbooks.&nbsp;<br />
<br />
Sometimes it's easier to write drawing code if the origin is in the&nbsp;<em style="font-style: normal; color: black; ">top-left</em>. This is how things work in web page design, for example. Quartz calls this a&nbsp;<cite style="color: #005bb1; font-style: normal; ">flipped</cite>&nbsp;coordinate system.</div>
<div class="cl" style="clear: both; ">&nbsp;</div>
<div style="background-color: #f0f0f0; border-left-color: initial; text-align: center; margin-bottom: 21px; "><img src="http://www.cocoadevcentral.com/images/articles/000090/quartz-coord.png" style="margin-top: 21px; margin-right: 0px; margin-bottom: 21px; margin-left: 0px; "  alt="" /></div>
<div class="contentbody" style="margin-left: 190px; color: #636363; font-size: 12px; padding-right: 20px; padding-left: 21px; ">You can easily convert points between standard and flipped views using NSView's&nbsp;<em style="font-style: normal; color: black; ">convertPoint:fromView:</em>&nbsp;and&nbsp;<em style="font-style: normal; color: black; ">convertPoint:toView:</em>&nbsp;methods.</div>
</div>
<div class="contentsection codesection" style="margin-top: 60px; width: 650px; background-color: #f5f5f5; border-left-color: #bcbcbc; line-height: 21px; padding-top: 21px; padding-bottom: 21px; ">
<h3 style="font-family: 'Helvetica Neue', 'Lucida Grande', sans-serif; font-weight: bold; color: #005bb1; padding-left: 21px; float: none; margin-top: 0px; width: auto; font-size: 16px; ">Rects as Objects</h3>
<div class="contentbody" style="margin-left: 0px; color: #636363; font-size: 12px; padding-right: 190px; padding-left: 21px; ">Because they're not objects, you can't store the geometry structs in an NSArray, NSDictionary, or NSSet directly, but you can wrap them in an<em style="font-style: normal; color: black; ">NSValue</em>&nbsp;object:</div>
<div style="clear: both; ">&nbsp;</div>
<div class="codeblock" style="color: #424242; background-color: #ebebeb; padding-top: 11px; padding-right: 21px; padding-bottom: 11px; padding-left: 21px; font-size: 12px; font-family: Monaco, monospace; white-space: pre; margin-bottom: 42px; line-height: 18px; ">NSRect    newRect    = NSMakeRect ( 20, 20, 100, 100 );
NSValue * <em style="color: #005bb1; font-style: normal; ">rectObject</em> = [NSValue <em style="color: #005bb1; font-style: normal; ">valueWithRect</em>: newRect];
NSMutableArray * myArray = [NSMutableArray array];
[myArray addObject: <em style="color: #005bb1; font-style: normal; ">rectObject</em>];
NSRect originalRect = [[myArray objectAtIndex: 0] <em style="color: #005bb1; font-style: normal; ">rectValue</em>];</div>
<div class="contentbody" style="margin-left: 0px; color: #636363; font-size: 12px; padding-right: 190px; padding-left: 21px; ">NSValue has similar methods for NSPoint and NSSize. You can also log information about rects using the&nbsp;<em style="font-style: normal; color: black; ">NSStringFromRect</em>&nbsp;function:</div>
<div style="clear: both; ">&nbsp;</div>
<div class="codeblock" style="color: #424242; background-color: #ebebeb; padding-top: 11px; padding-right: 21px; padding-bottom: 11px; padding-left: 21px; font-size: 12px; font-family: Monaco, monospace; white-space: pre; margin-bottom: 42px; line-height: 18px; ">NSRect newRect = NSMakeRect ( 20, 20, 100, 100 );
NSLog (@"%@", <em style="color: #005bb1; font-style: normal; ">NSStringFromRect</em>( newRect ));</div>
<div class="contentbody" style="margin-left: 0px; color: #636363; font-size: 12px; padding-right: 190px; padding-left: 21px; ">Another function,&nbsp;<em style="font-style: normal; color: black; ">NSRectFromString</em>&nbsp;takes a properly-formatted rect description and returns an NSRect. Both sets of functions also exist for NSPoint and NSSize.</div>
</div>
<div class="contentsection codesection" style="margin-top: 60px; width: 650px; background-color: #f5f5f5; border-left-color: #bcbcbc; line-height: 21px; padding-top: 21px; padding-bottom: 21px; ">
<h3 style="font-family: 'Helvetica Neue', 'Lucida Grande', sans-serif; font-weight: bold; color: #005bb1; padding-left: 21px; float: none; margin-top: 0px; width: auto; font-size: 16px; ">Derived Rects</h3>
<div class="contentbody" style="margin-left: 0px; color: #636363; font-size: 12px; padding-right: 190px; padding-left: 21px; ">Cocoa provides functions to create new rects based on existing ones. Here's how to make a rect which has the same dimensions as the original, but is shifted down and to the right (offset).</div>
<div class="cl" style="clear: both; ">&nbsp;</div>
<div class="codeimagecombo" style="background-color: #ebebeb; padding-top: 21px; padding-right: 21px; padding-bottom: 0px; padding-left: 21px; margin-bottom: 42px; ">
<div class="codeblock" style="color: #424242; background-color: #ebebeb; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 12px; font-family: Monaco, monospace; white-space: pre; margin-bottom: 0px; line-height: 18px; margin-top: 0px; margin-right: 0px; margin-left: 0px; float: left; width: 298px; "><strong style="color: #787878; padding-top: 2px; padding-right: 8px; padding-bottom: 2px; padding-left: 2px; background-color: #f2f2f2; font-weight: normal; ">// create a rect, then get the 5x5 offset</strong>
NSRect rect1;
rect1.origin.x    = 0;
rect1.origin.y    = 0;
rect1.size.width  = 30;
rect1.size.height = 25;
NSRect rect2;
rect2 = <em style="color: #005bb1; font-style: normal; ">NSOffsetRect</em> ( rect1, 5, 5 );</div>
<img src="http://www.cocoadevcentral.com/images/articles/000090/quartz-offset-rect.png" style="float: right; "  alt="" />
<div class="cl" style="clear: both; ">&nbsp;</div>
</div>
<div class="contentbody" style="margin-left: 0px; color: #636363; font-size: 12px; padding-right: 190px; padding-left: 21px; ">You can use negative values for the offset if you want to move in the opposite directions.&nbsp;<br />
<br />
Here's how to get the intersection area of two rects:</div>
<div class="cl" style="clear: both; ">&nbsp;</div>
<div class="codeimagecombo" style="background-color: #ebebeb; padding-top: 21px; padding-right: 21px; padding-bottom: 0px; padding-left: 21px; margin-bottom: 42px; ">
<div class="codeblock" style="color: #424242; background-color: #ebebeb; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 12px; font-family: Monaco, monospace; white-space: pre; margin-bottom: 0px; line-height: 18px; margin-top: 0px; margin-right: 0px; margin-left: 0px; float: left; width: 298px; "><strong style="color: #787878; padding-top: 2px; padding-right: 8px; padding-bottom: 2px; padding-left: 2px; background-color: #f2f2f2; font-weight: normal; ">// get the common area between two rects</strong>
NSRect rect1;
rect1.origin.x    = 0;
rect1.origin.y    = 0;
rect1.size.width  = 30;
rect1.size.height = 25;
NSRect rect2;
rect2 = NSOffsetRect ( rect1, 5, 5 );
NSRect rect3;
rect3 = <cite style="color: #005bb1; font-style: normal; ">NSIntersectionRect</cite> ( rect1, rect2 );</div>
<img src="http://www.cocoadevcentral.com/images/articles/000090/quartz-intersect-rect.png" style="float: right; margin-top: 21px; "  alt="" />
<div class="cl" style="clear: both; ">&nbsp;</div>
</div>
<div class="contentbody" style="margin-left: 0px; color: #636363; font-size: 12px; padding-right: 190px; padding-left: 21px; ">Here's how to create a rect which encloses two other rects (a union).</div>
<div class="cl" style="clear: both; ">&nbsp;</div>
<div class="codeimagecombo" style="background-color: #ebebeb; padding-top: 21px; padding-right: 21px; padding-bottom: 0px; padding-left: 21px; margin-bottom: 42px; ">
<div class="codeblock" style="color: #424242; background-color: #ebebeb; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 12px; font-family: Monaco, monospace; white-space: pre; margin-bottom: 0px; line-height: 18px; margin-top: 0px; margin-right: 0px; margin-left: 0px; float: left; width: 298px; "><strong style="color: #787878; padding-top: 2px; padding-right: 8px; padding-bottom: 2px; padding-left: 2px; background-color: #f2f2f2; font-weight: normal; ">// get a combination of two rects</strong>
NSRect rect1;
rect1.origin.x    = 0;
rect1.origin.y    = 0;
rect1.size.width  = 30;
rect1.size.height = 25;
NSRect rect2;
rect2 = NSOffsetRect ( rect1, 5, 5 );
NSRect rect3;
rect3 = <cite style="color: #005bb1; font-style: normal; ">NSUnionRect</cite> ( rect1, rect2 );</div>
<img src="http://www.cocoadevcentral.com/images/articles/000090/quartz-union-rect.png" style="float: right; "  alt="" />
<div class="cl" style="clear: both; ">&nbsp;</div>
</div>
<div class="contentbody" style="margin-left: 0px; color: #636363; font-size: 12px; padding-right: 190px; padding-left: 21px; ">An inset rect is helpful if you want to create a outer boundry, then create a rect for the content inside:</div>
<div class="cl" style="clear: both; ">&nbsp;</div>
<div class="codeimagecombo" style="background-color: #ebebeb; padding-top: 21px; padding-right: 21px; padding-bottom: 0px; padding-left: 21px; margin-bottom: 42px; ">
<div class="codeblock" style="color: #424242; background-color: #ebebeb; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 12px; font-family: Monaco, monospace; white-space: pre; margin-bottom: 0px; line-height: 18px; margin-top: 0px; margin-right: 0px; margin-left: 0px; float: left; width: 298px; "><strong style="color: #787878; padding-top: 2px; padding-right: 8px; padding-bottom: 2px; padding-left: 2px; background-color: #f2f2f2; font-weight: normal; ">// get a contracted version of a rect</strong>
NSRect rect1;
rect1.origin.x    = 0;
rect1.origin.y    = 0;
rect1.size.width  = 30;
rect1.size.height = 25;
NSRect rect2;
rect2 = <cite style="color: #005bb1; font-style: normal; ">NSInsetRect</cite> ( rect1, 5, 5 );</div>
<img src="http://www.cocoadevcentral.com/images/articles/000090/quartz-inset-rect.png" style="float: right; "  alt="" />
<div class="cl" style="clear: both; ">&nbsp;</div>
</div>
</div>
<div class="contentsection codesection" style="margin-top: 60px; width: 650px; background-color: #f5f5f5; border-left-color: #bcbcbc; line-height: 21px; padding-top: 21px; padding-bottom: 0px; ">
<h3 style="font-family: 'Helvetica Neue', 'Lucida Grande', sans-serif; font-weight: bold; color: #005bb1; padding-left: 21px; float: none; margin-top: 0px; width: auto; font-size: 16px; ">Comparing Rects and Points</h3>
<div class="contentbody" style="margin-left: 0px; color: #636363; font-size: 12px; padding-right: 190px; padding-left: 21px; ">Foundation provides a group of functions to check the equality of points and rects, as well as functions to see if points and rects are inside in other rects.</div>
<div class="cl" style="clear: both; ">&nbsp;</div>
<div class="codeimagecombo" style="background-color: #ebebeb; padding-top: 21px; padding-right: 21px; padding-bottom: 0px; padding-left: 21px; margin-bottom: 42px; ">
<div class="codeblock" style="color: #424242; background-color: #ebebeb; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 12px; font-family: Monaco, monospace; white-space: pre; margin-bottom: 0px; line-height: 18px; margin-top: 0px; margin-right: 0px; margin-left: 0px; float: left; width: 298px; ">NSRect rect1;
rect1.origin.x    = 0;
rect1.origin.y    = 0;
rect1.size.width  = 30;
rect1.size.height = 25;
NSPoint point1 = NSMakePoint ( 8,21 );
BOOL isInRect;
isInRect = <em style="color: #005bb1; font-style: normal; ">NSPointInRect</em> ( point1, rect1 );</div>
<img src="http://www.cocoadevcentral.com/images/articles/000090/quartz-point-in-rect.png" style="float: right; "  alt="" />
<div class="cl" style="clear: both; ">&nbsp;</div>
</div>
<div class="contentbody" style="margin-left: 0px; color: #636363; font-size: 12px; padding-right: 190px; padding-left: 21px; ">Below is a table of the most useful comparison functions. All of these functions return a YES or NO value.&nbsp;<br />
<br />
<table style="border-collapse: collapse; font-size: 12px; margin-left: 21px; margin-right: 21px; color: #636363; ">
    <tbody>
        <tr class="header" style="border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #bebebe; border-right-color: #bebebe; border-bottom-color: #bebebe; border-left-color: #bebebe; ">
            <th colspan="2" style="text-align: left; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 12px; color: #454545; background-color: #e9e9e9 !important; background-image: url(http://www.cocoadevcentral.com/images/layout/column-background.png); background-repeat: repeat no-repeat; ">Comparison Functions</th>
        </tr>
        <tr>
            <td class="highlight" style="color: #005bb1; font-style: normal; background-color: #eeeeee; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 12px; ">NSEqualRects</td>
            <td style="background-color: #eeeeee; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 12px; ">Are rects identical?</td>
        </tr>
        <tr>
            <td class="highlight" style="color: #005bb1; font-style: normal; background-color: #eeeeee; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 12px; ">NSEqualPoints</td>
            <td style="background-color: #eeeeee; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 12px; ">Are points identical?</td>
        </tr>
        <tr>
            <td class="highlight" style="color: #005bb1; font-style: normal; background-color: #eeeeee; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 12px; ">NSEqualSizes</td>
            <td style="background-color: #eeeeee; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 12px; ">Are sizes identical?</td>
        </tr>
        <tr>
            <td class="highlight" style="color: #005bb1; font-style: normal; background-color: #eeeeee; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 12px; ">NSContainsRect</td>
            <td style="background-color: #eeeeee; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 12px; ">Does the first rect contain the other?</td>
        </tr>
        <tr>
            <td class="highlight" style="color: #005bb1; font-style: normal; background-color: #eeeeee; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 12px; ">NSIntersectsRect</td>
            <td style="background-color: #eeeeee; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 12px; ">Do the rects at least partially overlap?</td>
        </tr>
        <tr>
            <td class="highlight" style="color: #005bb1; font-style: normal; background-color: #eeeeee; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 12px; ">NSPointInRect</td>
            <td style="background-color: #eeeeee; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 12px; ">Is the point inside the rect?</td>
        </tr>
        <tr>
            <td class="highlight" style="color: #005bb1; font-style: normal; background-color: #eeeeee; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 12px; ">NSMouseInRect</td>
            <td style="background-color: #eeeeee; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 12px; ">Is the mouse cursor in this rect?</td>
        </tr>
        <tr>
            <td class="highlight" style="color: #005bb1; font-style: normal; background-color: #eeeeee; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 12px; ">NSIsEmptyRect</td>
            <td style="background-color: #eeeeee; padding-top: 8px; padding-right: 12px; padding-bottom: 8px; padding-left: 12px; ">Is the rect empty (no area)?</td>
        </tr>
    </tbody>
</table>
<br />
<br />
These functions are listed in Foundation's NSGeometry.h file.</div>
<br />
</div>
<div class="contentsection codesection" style="margin-top: 60px; width: 650px; background-color: #f5f5f5; border-left-color: #bcbcbc; line-height: 21px; padding-top: 21px; padding-bottom: 0px; ">
<h3 style="font-family: 'Helvetica Neue', 'Lucida Grande', sans-serif; font-weight: bold; color: #005bb1; padding-left: 21px; float: none; margin-top: 0px; width: auto; font-size: 16px; ">Drawing</h3>
<div class="contentbody" style="margin-left: 0px; color: #636363; font-size: 12px; padding-right: 190px; padding-left: 21px; ">NSRects and NSPoints only describe geometry, they don't actually do drawing. Let's look at some primitive drawing functions in Cocoa's NSGraphics.h file.</div>
<div class="cl" style="clear: both; ">&nbsp;</div>
<div class="codeimagecombo" style="background-color: #ebebeb; padding-top: 21px; padding-right: 21px; padding-bottom: 0px; padding-left: 21px; margin-bottom: 42px; ">
<div class="codeblock" style="color: #424242; background-color: #ebebeb; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 12px; font-family: Monaco, monospace; white-space: pre; margin-bottom: 0px; line-height: 18px; margin-top: 0px; margin-right: 0px; margin-left: 0px; float: left; width: 298px; ">NSColor * gray  = [NSColor grayColor];
NSColor * white = [NSColor whiteColor];
<strong style="color: #787878; padding-top: 2px; padding-right: 8px; padding-bottom: 2px; padding-left: 2px; background-color: #f2f2f2; font-weight: normal; ">// fill background</strong>
[gray set];
<em style="color: #005bb1; font-style: normal; ">NSRectFill</em> ( [self bounds] );
<strong style="color: #787878; padding-top: 2px; padding-right: 8px; padding-bottom: 2px; padding-left: 2px; background-color: #f2f2f2; font-weight: normal; ">// fill target rect</strong>
NSRect rect1 = NSMakeRect ( 21,21,210,210 );
[white set];
<em style="color: #005bb1; font-style: normal; ">NSRectFill</em> ( rect1 );</div>
<img src="http://www.cocoadevcentral.com/images/articles/000090/quartz-draw-rect.png" style="float: right; "  alt="" />
<div class="cl" style="clear: both; ">&nbsp;</div>
</div>
<div class="contentbody" style="margin-left: 0px; color: #636363; font-size: 12px; padding-right: 190px; padding-left: 21px; ">The example above uses&nbsp;<cite style="color: #005bb1; font-style: normal; ">NSColor</cite>. When you call the&nbsp;<em style="font-style: normal; color: black; ">-set</em>&nbsp;method on a color object, Quartz uses it for all drawing until you set a new one.&nbsp;<br />
<br />
Here's how to draw a border around a rect:</div>
<div class="cl" style="clear: both; ">&nbsp;</div>
<div class="codeimagecombo" style="background-color: #ebebeb; padding-top: 21px; padding-right: 21px; padding-bottom: 0px; padding-left: 21px; margin-bottom: 42px; ">
<div class="codeblock" style="color: #424242; background-color: #ebebeb; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 12px; font-family: Monaco, monospace; white-space: pre; margin-bottom: 0px; line-height: 18px; margin-top: 0px; margin-right: 0px; margin-left: 0px; float: left; width: 298px; ">NSColor * gray  = [NSColor grayColor];
NSColor * white = [NSColor whiteColor];
<strong style="color: #787878; padding-top: 2px; padding-right: 8px; padding-bottom: 2px; padding-left: 2px; background-color: #f2f2f2; font-weight: normal; ">// fill background</strong>
[gray set];
NSRectFill ( [self bounds] );
<strong style="color: #787878; padding-top: 2px; padding-right: 8px; padding-bottom: 2px; padding-left: 2px; background-color: #f2f2f2; font-weight: normal; ">// draw a border around target rect</strong>
NSRect rect1 = NSMakeRect ( 21,21,210,210 );
[white set];
<em style="color: #005bb1; font-style: normal; ">NSFrameRectWithWidth</em> ( rect1, 1 );</div>
<img src="http://www.cocoadevcentral.com/images/articles/000090/quartz-frame-rect.png" style="float: right; "  alt="" />
<div class="cl" style="clear: both; ">&nbsp;</div>
</div>
<div class="contentbody" style="margin-left: 0px; color: #636363; font-size: 12px; padding-right: 190px; padding-left: 21px; ">You can also call&nbsp;<em style="font-style: normal; color: black; ">NSFrameRect</em>&nbsp;if you just want to use the default line width.</div>
<div class="cl" style="clear: both; ">&nbsp;</div>
</div>
<div class="contentsection codesection" style="margin-top: 60px; width: 650px; background-color: #f5f5f5; border-left-color: #bcbcbc; line-height: 21px; padding-top: 21px; padding-bottom: 0px; ">
<h3 style="font-family: 'Helvetica Neue', 'Lucida Grande', sans-serif; font-weight: bold; color: #005bb1; padding-left: 21px; float: none; margin-top: 0px; width: auto; font-size: 16px; ">Drawing Groups</h3>
<div class="contentbody" style="margin-left: 0px; color: #636363; font-size: 12px; padding-right: 190px; padding-left: 21px; ">It's often faster to draw an array of rects all at once instead of calling NSRectFill for each one individually.&nbsp;<br />
<br />
Below is a more involved example which builds C-style arrays of NSRects and NSColors, then passes both to&nbsp;<em style="font-style: normal; color: black; ">NSRectFillListWithColors</em>.</div>
<div class="cl" style="clear: both; ">&nbsp;</div>
<div class="codeimagecombo" style="background-color: #ebebeb; padding-top: 21px; padding-right: 21px; padding-bottom: 0px; padding-left: 21px; margin-bottom: 42px; ">
<div class="codeblock" style="color: #424242; background-color: #ebebeb; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 12px; font-family: Monaco, monospace; white-space: pre; margin-bottom: 0px; line-height: 18px; margin-top: 0px; margin-right: 0px; margin-left: 0px; float: left; width: 298px; ">
<strong style="color: #787878; padding-top: 2px; padding-right: 8px; padding-bottom: 2px; padding-left: 2px; background-color: #f2f2f2; font-weight: normal; ">// setup basics</strong>
[[NSColor grayColor] set];
NSRectFill ( [self bounds] );
int count = 12;
NSRect startingRect = NSMakeRect ( 21,21,50,50 );
<strong style="color: #787878; padding-top: 2px; padding-right: 8px; padding-bottom: 2px; padding-left: 2px; background-color: #f2f2f2; font-weight: normal; ">// create arrays of rects and colors</strong>
NSRect    rectArray [count];
NSColor * colorArray[count];
rectArray [0] = startingRect;
colorArray[0] = [NSColor redColor];
<strong style="color: #787878; padding-top: 2px; padding-right: 8px; padding-bottom: 2px; padding-left: 2px; background-color: #f2f2f2; font-weight: normal; ">// populate arrays</strong>
int i;
NSRect oneRect = rectArray[0];
for ( i = 1; i &lt; count; i++ )
{
<strong style="color: #787878; padding-top: 2px; padding-right: 8px; padding-bottom: 2px; padding-left: 2px; background-color: #f2f2f2; font-weight: normal; ">// move 100 pixels to the right</strong>
oneRect.origin.x += 100;
<strong style="color: #787878; padding-top: 2px; padding-right: 8px; padding-bottom: 2px; padding-left: 2px; background-color: #f2f2f2; font-weight: normal; ">// if the right edge doesn't fit, move down 100 pixels</strong>
if ( <em style="color: #005bb1; font-style: normal; ">NSMaxX</em> (oneRect) &gt; <em style="color: #005bb1; font-style: normal; ">NSMaxX</em> ([self bounds]) )
{
oneRect.origin.x = startingRect.origin.x;
oneRect.origin.y += 100;
}
rectArray [i] = oneRect;
<strong style="color: #787878; padding-top: 2px; padding-right: 8px; padding-bottom: 2px; padding-left: 2px; background-color: #f2f2f2; font-weight: normal; ">// increment color</strong>
colorArray[i] = [NSColor <em style="color: #005bb1; font-style: normal; ">colorWithCalibratedHue</em>: (i*0.04)
saturation: 1
brightness: 0.9
alpha: 1];
}
<strong style="color: #787878; padding-top: 2px; padding-right: 8px; padding-bottom: 2px; padding-left: 2px; background-color: #f2f2f2; font-weight: normal; ">// use rect and color arrays to fill</strong>
<em style="color: #005bb1; font-style: normal; ">NSRectFillListWithColors</em> ( rectArray, colorArray, count );
<strong style="color: #787878; padding-top: 2px; padding-right: 8px; padding-bottom: 2px; padding-left: 2px; background-color: #f2f2f2; font-weight: normal; ">// draw a 2 pixel border around each rect</strong>
[[NSColor whiteColor] set];
for ( i = 0; i &lt; count; i++) {
<em style="color: #005bb1; font-style: normal; ">NSFrameRectWithWidth</em> ( rectArray[i], 2 );
}</div>
<img src="http://www.cocoadevcentral.com/images/articles/000090/quartz-fill-rect-list.png" style="float: right; margin-top: 42px; "  alt="" />
<div class="cl" style="clear: both; ">&nbsp;</div>
</div>
<div class="contentbody" style="margin-left: 0px; color: #636363; font-size: 12px; padding-right: 190px; padding-left: 21px; ">This example also uses the function&nbsp;<em style="font-style: normal; color: black; ">NSMaxX</em>&nbsp;to get the maximum x-axis value of the rect (right edge). If it's outside of the view bounds, we move down to the next row.</div>
<div class="cl" style="clear: both; ">&nbsp;</div>
</div>
<div class="contentsection" style="margin-top: 60px; width: 650px; background-color: #f5f5f5; border-left-color: #bcbcbc; line-height: 21px; padding-top: 21px; padding-bottom: 0px; ">
<h3 style="font-family: 'Helvetica Neue', 'Lucida Grande', sans-serif; font-weight: bold; color: #005bb1; padding-left: 21px; float: left; margin-top: 0px; width: 190px; font-size: 16px; ">Wrap Up</h3>
<div class="contentbody" style="margin-left: 190px; color: #636363; font-size: 12px; padding-right: 20px; padding-left: 21px; ">We've covered basic Quartz concepts here. The next tutorial will dig into intermediate topics. You can download the final example here:&nbsp;<br />
<br />
<a href="http://www.cocoadevcentral.com/downloads/IntroToQuartz-20061102a.zip" style="color: #005bb1; text-decoration: none; ">IntroToQuartz</a>&nbsp;Xcode 2.4 Project (52k)&nbsp;<br />
<br />
Love it? Suggestions?&nbsp;<a href="http://www.cocoadevcentral.com/message.page" style="color: #005bb1; text-decoration: none; ">Send feedback</a>&nbsp;on this tutorial.&nbsp;<br />
<br />
For further reading, check out&nbsp;<a href="http://www.cocoadevcentral.com/d/intro_to_quartz_two/" style="color: #005bb1; text-decoration: none; ">Cocoa Graphics Part II</a></div>
<div class="cl" style="clear: both; ">&nbsp;</div>
<div class="credits" style="margin-left: 0px; color: #636363; font-size: 11px; padding-top: 10px; padding-right: 20px; padding-bottom: 20px; padding-left: 211px; ">Copyright &#169; 2004-2006 Scott Stevenson<br />
Made with&nbsp;<a href="http://macromates.com/" style="color: #005bb1; text-decoration: none; ">TextMate</a></div>
</div>
</div>
<div class="feedback" style="background-color: #e0e0e0; padding-top: 12px; padding-right: 12px; padding-bottom: 12px; padding-left: 12px; font-size: 12px; text-align: center; color: #878787; margin-top: 40px; line-height: 16px; "><a href="http://www.cocoadevcentral.com/message.page" style="color: #808080; text-decoration: none; ">Feedback Form</a><br />
</div>
<div class="notes" style="font-size: 11px; text-align: center; color: #878787; margin-top: 21px; line-height: 16px; ">Cocoa Dev Central is a servicemark of&nbsp;<a href="http://treehouseideas.com/" style="color: #005bb1; text-decoration: none; ">Tree House Ideas</a><br />
Site design &#169; 2004-2006 Scott Stevenson</div>
<div class="notes" style="font-size: 11px; text-align: center; color: #878787; margin-top: 21px; line-height: 16px; ">From:&nbsp;http://www.cocoadevcentral.com/d/intro_to_quartz/</div>
</div>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/161306.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2011-12-02 00:15 <a href="http://www.cppblog.com/biao/archive/2011/12/02/161306.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cocoa：NSArray</title><link>http://www.cppblog.com/biao/archive/2011/12/02/161305.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 01 Dec 2011 16:13:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2011/12/02/161305.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/161305.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2011/12/02/161305.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/161305.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/161305.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Adopted ProtocolsNSCoding        encodeWithCoder:            initWithCoder:    NSCoding例子- (id)initWithCoder:(NSCoder *)coder {&nbsp; &nbsp; if (self = [super init]) {&nbsp;&...&nbsp;&nbsp;<a href='http://www.cppblog.com/biao/archive/2011/12/02/161305.html'>阅读全文</a><img src ="http://www.cppblog.com/biao/aggbug/161305.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2011-12-02 00:13 <a href="http://www.cppblog.com/biao/archive/2011/12/02/161305.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cocoa：NSValue</title><link>http://www.cppblog.com/biao/archive/2011/12/02/161304.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 01 Dec 2011 16:11:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2011/12/02/161304.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/161304.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2011/12/02/161304.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/161304.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/161304.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: An&nbsp;NSValue&nbsp;object is a simple container for a single C or Objective-C data item. It can hold any of the scalar types such as int, float, and char, as well as pointers, structures, and object...&nbsp;&nbsp;<a href='http://www.cppblog.com/biao/archive/2011/12/02/161304.html'>阅读全文</a><img src ="http://www.cppblog.com/biao/aggbug/161304.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2011-12-02 00:11 <a href="http://www.cppblog.com/biao/archive/2011/12/02/161304.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cocoa：Core Data Overview</title><link>http://www.cppblog.com/biao/archive/2011/12/02/161303.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 01 Dec 2011 16:10:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2011/12/02/161303.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/161303.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2011/12/02/161303.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/161303.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/161303.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Cocoa Dev Central&nbsp;.&nbsp;Articles&nbsp;.&nbsp;Core Data Class Overview&nbsp;FeedbackThe Core Data framework provides a lot of new functionality to Cocoa developers, but manages to do so wit...&nbsp;&nbsp;<a href='http://www.cppblog.com/biao/archive/2011/12/02/161303.html'>阅读全文</a><img src ="http://www.cppblog.com/biao/aggbug/161303.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2011-12-02 00:10 <a href="http://www.cppblog.com/biao/archive/2011/12/02/161303.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cocoa：Saving Application Data</title><link>http://www.cppblog.com/biao/archive/2011/12/02/161302.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 01 Dec 2011 16:08:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2011/12/02/161302.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/161302.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2011/12/02/161302.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/161302.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/161302.html</trackback:ping><description><![CDATA[@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
<div style="font-family: 'Trebuchet MS', Trebuchet, Tahoma, sans-serif; font-size: 13px; background-color: #454545; color: white; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; line-height: normal; ">
<div class="docheader" style="width: 650px; padding-top: 15px; padding-right: 15px; padding-bottom: 15px; padding-left: 15px; margin-left: auto; margin-right: auto; background-color: #373737; font-family: 'Trebuchet MS', Trebuchet, Tahoma, sans-serif; font-size: 13px; text-align: left; "><strong><a href="http://www.cocoadevcentral.com/" style="color: white; text-decoration: none; ">CocoaDevCentral.com</a></strong>&nbsp;.&nbsp;<a href="http://cocoadevcentral.com/archives.php" style="color: white; text-decoration: none; ">Articles</a>&nbsp;.&nbsp;<em><a href="http://www.cocoadevcentral.com/articles/000084.php" style="color: white; text-decoration: none; ">Saving Cocoa Application Data</a></em><span style="float: right; "><a href="http://www.cocoadevcentral.com/message.page" style="color: white; text-decoration: none; "><img src="http://www.cocoadevcentral.com/images/global/mini-envelope-opaque.png" valign="middle" alt="Email" style="background-color: white; " />&nbsp;Feedback</a></span></div>
<div class="content" style="background-color: white; color: #4e4e4e; width: 580px; padding-top: 10px; padding-right: 50px; padding-bottom: 10px; padding-left: 50px; margin-top: 20px; margin-left: auto; margin-right: auto; line-height: 24px; "><img class="title" src="http://www.cocoadevcentral.com/images/articles/000084.png" alt="Saving User Data" style="margin-bottom: 68px; margin-top: 50px; " />&nbsp;<br />
<p><img src="http://www.cocoadevcentral.com/images/global/disk-arrow.gif" alt="Disk Icon" /><br />
In a nutshell:&nbsp;<em style="font-weight: bold; font-style: normal; color: #e24628; ">saving data created by the user</em></p>
<p>Most applications need a way to save data to disk and bring it back later. In many cases, the best built-in class to use for this is&nbsp;<var style="font-style: normal; color: #0a4aa7; font-weight: bold; ">NSKeyedArchiver</var>. It doesn't provide the scalability of a SQL database, but it is incredibly easy to use.</p>
<p>In this tutorial, you'll add saving support to the MailDemo application, which was originally used in the Cocoa Bindings tutorial. It's not necessary to read that tutorial first, but you do need to understand Objective-C and general Cocoa concepts.</p>
<p>Download the project so you can follow along and make the changes as they're described. Note that this is&nbsp;<em>not</em>&nbsp;the finished project, just the starting point. Mac OS X 10.3 (Panther) is required to use this project.</p>
<p>&gt;&nbsp;<a href="http://www.cocoadevcentral.com/downloads/MailDemo-000084-start.zip" style="color: #e24628; ">Download the MailDemo Xcode project</a>&nbsp;56k</p>
<p class="extranote" style="font-style: italic; font-size: 12px; margin-top: 25px; margin-left: 22px; ">Most Cocoa applications are typically either "regular" or multi-document. TextEdit, for example, is a multi-documentation application, whereas AddressBook is not. This tutorial does not discuss multi-document applications, though the concepts are very similar.</p>
</div>
<div><a name="1" style="width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "></a></div>
<div class="content" style="background-color: white; color: #4e4e4e; width: 580px; padding-top: 10px; padding-right: 50px; padding-bottom: 10px; padding-left: 50px; margin-top: 20px; margin-left: auto; margin-right: auto; line-height: 24px; ">
<p class="pagenumber" style="font-style: italic; text-align: right; font-size: 12px; "><a href="http://www.cocoadevcentral.com/articles/000084.php#1" style="color: #e24628; ">1</a>&nbsp;of 9</p>
<h3 style="font-weight: bold; color: #e24628; font-size: 1em; ">NSKeyedArchiver in a Nutshell</h3>
<p>The&nbsp;<var style="font-style: normal; color: #0a4aa7; font-weight: bold; ">NSKeyedArchiver</var>&nbsp;class accepts data objects from your application and stores them on disk. It stores the objects in a file as binary data.</p>
<center><img src="http://www.cocoadevcentral.com/images/articles/archiving-coding.gif" alt="Encoding Diagram" border="0" /></center>
<p>You can open a binary data file in a text editor, but it's quite hard to make any sense of the contents that way. Binary files are designed to be easily read by the computer, not a person. As a result, binary files take up less space on disk than plain text files and can be loaded by an application very quickly.</p>
<p>Interface Builder, for example, typically stores NIB files in a binary format.</p>
</div>
<div><a name="2" style="width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "></a></div>
<div class="content" style="background-color: white; color: #4e4e4e; width: 580px; padding-top: 10px; padding-right: 50px; padding-bottom: 10px; padding-left: 50px; margin-top: 20px; margin-left: auto; margin-right: auto; line-height: 24px; ">
<p class="pagenumber" style="font-style: italic; text-align: right; font-size: 12px; "><a href="http://www.cocoadevcentral.com/articles/000084.php#2" style="color: #e24628; ">2</a>&nbsp;of 9</p>
<h3 style="font-weight: bold; color: #e24628; font-size: 1em; ">Adding Saving Support to Mailbox</h3>
<p>The process of converting objects into binary data is called&nbsp;<var style="font-style: normal; color: #0a4aa7; font-weight: bold; ">encoding</var>. Your job is to tell Cocoa how your data should be encoded. Here's the code to add to&nbsp;<strong style="font-weight: normal; color: #161616; ">Mailbox.m</strong>&nbsp;to do that:</p>
<div class="filename" style="display: block; background-color: #e8e8e8; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; line-height: 12px; margin-left: 22px; border-left-color: #d3d3d3; font-family: 'Trebuchet MS', Trebuchet, Tahoma, sans-serif; font-size: 12px; color: #383838; ">Mailbox.m</div>
<div class="example" style="display: block; background-color: #f7f7f7; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; line-height: 16px; margin-left: 22px; border-left-color: #d3d3d3; font-family: Monaco, monospace; font-size: 11px; white-space: pre; ">
- (void) <var style="font-style: normal; color: #0a4aa7; font-weight: normal; ">encodeWithCoder</var>: (NSCoder *)coder
{
[coder <var style="font-style: normal; color: #0a4aa7; font-weight: normal; ">encodeObject</var>: properties <var style="font-style: normal; color: #0a4aa7; font-weight: normal; ">forKey</var>:<strong style="font-weight: normal; color: #161616; ">@"properties"</strong>];
[coder <var style="font-style: normal; color: #0a4aa7; font-weight: normal; ">encodeObject</var>: emails     <var style="font-style: normal; color: #0a4aa7; font-weight: normal; ">forKey</var>:<strong style="font-weight: normal; color: #161616; ">@"emails"</strong>];
}
</div>
<p>This method adds the Mailbox's data to the NSCoder object that's passed in, using the<nobr>-encodeObject:forKey:</nobr>&nbsp;message to store each item. You can choose whatever names you like for the keys, but they must mirror those used in -initWithCoder.</p>
<p>The -initWithCoder: method is called when data is being loaded back from disk. Here's the version of that method to put in&nbsp;<strong style="font-weight: normal; color: #161616; ">Mailbox.m</strong>:</p>
<div class="example" style="display: block; background-color: #f7f7f7; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; line-height: 16px; margin-left: 22px; border-left-color: #d3d3d3; font-family: Monaco, monospace; font-size: 11px; white-space: pre; ">
- (id) <var style="font-style: normal; color: #0a4aa7; font-weight: normal; ">initWithCoder</var>: (NSCoder *)coder
{
if (self = [super init])
{
[self setProperties: [coder <var style="font-style: normal; color: #0a4aa7; font-weight: normal; ">decodeObjectForKey</var>:<strong style="font-weight: normal; color: #161616; ">@"properties"</strong>]];
[self setEmails:     [coder <var style="font-style: normal; color: #0a4aa7; font-weight: normal; ">decodeObjectForKey</var>:<strong style="font-weight: normal; color: #161616; ">@"emails"</strong>]];
}
<strong style="font-weight: normal; color: #161616; ">return self</strong>;
}
</div>
<br />
<p>Finally, change&nbsp;<strong style="font-weight: normal; color: #161616; ">Mailbox.h</strong>&nbsp;to indicate that the Mailbox class now conforms to the&nbsp;<var style="font-style: normal; color: #0a4aa7; font-weight: bold; ">NSCoding</var>protocol:</p>
<div class="filename" style="display: block; background-color: #e8e8e8; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; line-height: 12px; margin-left: 22px; border-left-color: #d3d3d3; font-family: 'Trebuchet MS', Trebuchet, Tahoma, sans-serif; font-size: 12px; color: #383838; ">Mailbox.h</div>
<div class="example" style="display: block; background-color: #f7f7f7; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; line-height: 16px; margin-left: 22px; border-left-color: #d3d3d3; font-family: Monaco, monospace; font-size: 11px; white-space: pre; ">
@interface <strong style="font-weight: normal; color: #161616; ">Mailbox</strong> : NSObject &lt;<var style="font-style: normal; color: #0a4aa7; font-weight: normal; ">NSCoding</var>&gt;
</div>
<p>That's all we need to do in this file.</p>
</div>
<div><a name="3" style="width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "></a></div>
<div class="content" style="background-color: white; color: #4e4e4e; width: 580px; padding-top: 10px; padding-right: 50px; padding-bottom: 10px; padding-left: 50px; margin-top: 20px; margin-left: auto; margin-right: auto; line-height: 24px; ">
<p class="pagenumber" style="font-style: italic; text-align: right; font-size: 12px; "><a href="http://www.cocoadevcentral.com/articles/000084.php#3" style="color: #e24628; ">3</a>&nbsp;of 9</p>
<h3 style="font-weight: bold; color: #e24628; font-size: 1em; ">Adding Saving Support to Email</h3>
<p>Here's the code to add to&nbsp;<strong style="font-weight: normal; color: #161616; ">Email.m</strong>&nbsp;to enable saving for the Email class:</p>
<div class="filename" style="display: block; background-color: #e8e8e8; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; line-height: 12px; margin-left: 22px; border-left-color: #d3d3d3; font-family: 'Trebuchet MS', Trebuchet, Tahoma, sans-serif; font-size: 12px; color: #383838; ">Email.m</div>
<div class="example" style="display: block; background-color: #f7f7f7; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; line-height: 16px; margin-left: 22px; border-left-color: #d3d3d3; font-family: Monaco, monospace; font-size: 11px; white-space: pre; ">
- (void) <var style="font-style: normal; color: #0a4aa7; font-weight: normal; ">encodeWithCoder</var>: (NSCoder *)coder
{
[coder <var style="font-style: normal; color: #0a4aa7; font-weight: normal; ">encodeObject</var>: properties <var style="font-style: normal; color: #0a4aa7; font-weight: normal; ">forKey</var>:<strong style="font-weight: normal; color: #161616; ">@"properties"</strong>];
}
</div>
<p>Here's the code to enable loading from disk:</p>
<div class="example" style="display: block; background-color: #f7f7f7; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; line-height: 16px; margin-left: 22px; border-left-color: #d3d3d3; font-family: Monaco, monospace; font-size: 11px; white-space: pre; ">
- (id) <var style="font-style: normal; color: #0a4aa7; font-weight: normal; ">initWithCoder</var>: (NSCoder *)coder
{
if (self = [super init])
{
[self setProperties: [coder <var style="font-style: normal; color: #0a4aa7; font-weight: normal; ">decodeObjectForKey</var>:<strong style="font-weight: normal; color: #161616; ">@"properties"</strong>]];
}
<strong style="font-weight: normal; color: #161616; ">return self</strong>;
}
</div>
<p>This code is nearly identical to the methods added to Mailbox. The difference is that there's one less instance variable to store.</p>
<p>Just as before, add&nbsp;<var style="font-style: normal; color: #0a4aa7; font-weight: bold; ">NSCoding</var>&nbsp;to&nbsp;<strong style="font-weight: normal; color: #161616; ">Email.h</strong>:</p>
<div class="filename" style="display: block; background-color: #e8e8e8; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; line-height: 12px; margin-left: 22px; border-left-color: #d3d3d3; font-family: 'Trebuchet MS', Trebuchet, Tahoma, sans-serif; font-size: 12px; color: #383838; ">Email.h</div>
<div class="example" style="display: block; background-color: #f7f7f7; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; line-height: 16px; margin-left: 22px; border-left-color: #d3d3d3; font-family: Monaco, monospace; font-size: 11px; white-space: pre; ">
@interface <strong style="font-weight: normal; color: #161616; ">Email</strong> : NSObject &lt;<var style="font-style: normal; color: #0a4aa7; font-weight: normal; ">NSCoding</var>&gt;
</div>
<p>We're now done with this file.</p>
</div>
<div><a name="4" style="width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "></a></div>
<div class="content" style="background-color: white; color: #4e4e4e; width: 580px; padding-top: 10px; padding-right: 50px; padding-bottom: 10px; padding-left: 50px; margin-top: 20px; margin-left: auto; margin-right: auto; line-height: 24px; ">
<p class="pagenumber" style="font-style: italic; text-align: right; font-size: 12px; "><a href="http://www.cocoadevcentral.com/articles/000084.php#4" style="color: #e24628; ">4</a>&nbsp;of 9</p>
<h3 style="font-weight: bold; color: #e24628; font-size: 1em; ">Declare New Methods in MyController.h</h3>
<p>We'll create three new methods to MailDemo's MyController class to support saving. Add these declarations to the&nbsp;<strong style="font-weight: normal; color: #161616; ">MyController.h</strong>&nbsp;file:</p>
<div class="filename" style="display: block; background-color: #e8e8e8; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; line-height: 12px; margin-left: 22px; border-left-color: #d3d3d3; font-family: 'Trebuchet MS', Trebuchet, Tahoma, sans-serif; font-size: 12px; color: #383838; ">MyController.h</div>
<div class="example" style="display: block; background-color: #f7f7f7; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; line-height: 16px; margin-left: 22px; border-left-color: #d3d3d3; font-family: Monaco, monospace; font-size: 11px; white-space: pre; ">
- (NSString *) <strong style="font-weight: normal; color: #161616; ">pathForDataFile</strong>;
- (void) <strong style="font-weight: normal; color: #161616; ">saveDataToDisk</strong>;
- (void) <strong style="font-weight: normal; color: #161616; ">loadDataFromDisk</strong>;
</div>
<p>The first method returns the destination path of the data file. The other two methods are used to save and load the data, respectively.</p>
</div>
<div><a name="5" style="width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "></a></div>
<div class="content" style="background-color: white; color: #4e4e4e; width: 580px; padding-top: 10px; padding-right: 50px; padding-bottom: 10px; padding-left: 50px; margin-top: 20px; margin-left: auto; margin-right: auto; line-height: 24px; ">
<p class="pagenumber" style="font-style: italic; text-align: right; font-size: 12px; "><a href="http://www.cocoadevcentral.com/articles/000084.php#5" style="color: #e24628; ">5</a>&nbsp;of 9</p>
<h3 style="font-weight: bold; color: #e24628; font-size: 1em; ">Choosing the File Location</h3>
<p>Before an application can save, the developer must select a location for the data. Mac OS X applications generally store such things in&nbsp;<strong style="font-weight: normal; color: #161616; ">~/Library/Application Support/&lt;Application&gt;/</strong>. The application must create that directory if it doesn't already exist.</p>
<p>Here's the code to add to&nbsp;<strong style="font-weight: normal; color: #161616; ">MyController.m</strong>&nbsp;to do all of this:</p>
<div class="filename" style="display: block; background-color: #e8e8e8; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; line-height: 12px; margin-left: 22px; border-left-color: #d3d3d3; font-family: 'Trebuchet MS', Trebuchet, Tahoma, sans-serif; font-size: 12px; color: #383838; ">MyController.m</div>
<div class="example" style="display: block; background-color: #f7f7f7; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; line-height: 16px; margin-left: 22px; border-left-color: #d3d3d3; font-family: Monaco, monospace; font-size: 11px; white-space: pre; ">
- (NSString *) <em style="font-weight: normal; font-style: normal; color: #e24628; ">pathForDataFile</em>
{
NSFileManager *fileManager = [NSFileManager defaultManager];
<strong style="font-weight: normal; color: #161616; ">NSString *folder</strong> = <em style="font-weight: normal; font-style: normal; color: #e24628; ">@"~/Library/Application Support/MailDemo/"</em>;
folder = [folder <strong style="font-weight: normal; color: #161616; ">stringByExpandingTildeInPath</strong>];
if ([fileManager <strong style="font-weight: normal; color: #161616; ">fileExistsAtPath</strong>: folder] == <strong style="font-weight: normal; color: #161616; ">NO</strong>)
{
[fileManager <strong style="font-weight: normal; color: #161616; ">createDirectoryAtPath</strong>: folder attributes: nil];
}
NSString *fileName = <em style="font-weight: normal; font-style: normal; color: #e24628; ">@"MailDemo.cdcmaildemo"</em>;
return [folder stringByAppendingPathComponent: fileName];
}
</div>
<p>On the first line of this method, we set up an instance of NSFileManager. Many of the file-related tasks that you would typically do by hand with the Finder can be accomplished using NSFileManager.</p>
<p>Next, we create an NSString named&nbsp;<strong style="font-weight: normal; color: #161616; ">folder</strong>, and set it to the path of the destination folder.</p>
<p>The next line is a bit tricky in that it&nbsp;<em>re-assigns</em>&nbsp;the&nbsp;<strong style="font-weight: normal; color: #161616; ">folder</strong>&nbsp;variable to a different version of the same string. The -stringByExpandingTildeInPath method returns a version of the folder path with the tilde replaced by the user's home directory, such as "/Users/scott".</p>
<p>On the next line, we ask NSFileManager if the folder already exists. If it doesn't, we request that it is created by sending the -createDirectoryAtPath:attributes: message.</p>
<p>Finally, we append the file name to the folder path and return the complete path.</p>
</div>
<div><a name="6" style="width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "></a></div>
<div class="content" style="background-color: white; color: #4e4e4e; width: 580px; padding-top: 10px; padding-right: 50px; padding-bottom: 10px; padding-left: 50px; margin-top: 20px; margin-left: auto; margin-right: auto; line-height: 24px; ">
<p class="pagenumber" style="font-style: italic; text-align: right; font-size: 12px; "><a href="http://www.cocoadevcentral.com/articles/000084.php#6" style="color: #e24628; ">6</a>&nbsp;of 9</p>
<h3 style="font-weight: bold; color: #e24628; font-size: 1em; ">Archiving the Data</h3>
<p>Now that you have added the NSCoding methods to the data classes, you can ask&nbsp;<var style="font-style: normal; color: #0a4aa7; font-weight: bold; ">NSKeyedArchiver</var>to write your objects to disk. You only need to tell the archiver about your top-level objects. In this case, the term "top level" just means the objects which are listed as instance variables in MyController.h.</p>
<p>The only top-level object for the MyController class is the "mailboxes" array. Here's the code to add to&nbsp;<strong style="font-weight: normal; color: #161616; ">MyController.m</strong>&nbsp;to save the array (and everything it contains) to disk:</p>
<div class="filename" style="display: block; background-color: #e8e8e8; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; line-height: 12px; margin-left: 22px; border-left-color: #d3d3d3; font-family: 'Trebuchet MS', Trebuchet, Tahoma, sans-serif; font-size: 12px; color: #383838; ">MyController.m</div>
<div class="example" style="display: block; background-color: #f7f7f7; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; line-height: 16px; margin-left: 22px; border-left-color: #d3d3d3; font-family: Monaco, monospace; font-size: 11px; white-space: pre; ">
- (void) <em style="font-weight: normal; font-style: normal; color: #e24628; ">saveDataToDisk</em>
{
NSString * <em style="font-weight: normal; font-style: normal; color: #e24628; ">path</em> = [self <strong style="font-weight: normal; color: #161616; ">pathForDataFile</strong>];
NSMutableDictionary * rootObject;
<em style="font-weight: normal; font-style: normal; color: #e24628; ">rootObject</em> = [NSMutableDictionary dictionary];
[<em style="font-weight: normal; font-style: normal; color: #e24628; ">rootObject</em> setValue: [self <strong style="font-weight: normal; color: #161616; ">mailboxes</strong>] forKey:<strong style="font-weight: normal; color: #161616; ">@"mailboxes"</strong>];
[<var style="font-style: normal; color: #0a4aa7; font-weight: normal; ">NSKeyedArchiver</var> <strong style="font-weight: normal; color: #161616; ">archiveRootObject:</strong> <em style="font-weight: normal; font-style: normal; color: #e24628; ">rootObject</em> <strong style="font-weight: normal; color: #161616; ">toFile:</strong> <em style="font-weight: normal; font-style: normal; color: #e24628; ">path</em>];
}
</div>
<p>On the first line, we retrieve the destination file path. After that, we create an NSMutableDictionary and place the mailboxes array in it. If you have other top-level objects, add them to this dictionary.</p>
<p>This dictionary is considered the "root object" -- a master object which contains a tree of all other data objects. We pass this dictionary to&nbsp;<var style="font-style: normal; color: #0a4aa7; font-weight: bold; ">NSKeyedArchiver</var>&nbsp;in the +archiveRootObject:toFile: class method.</p>
</div>
<div><a name="7" style="width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "></a></div>
<div class="content" style="background-color: white; color: #4e4e4e; width: 580px; padding-top: 10px; padding-right: 50px; padding-bottom: 10px; padding-left: 50px; margin-top: 20px; margin-left: auto; margin-right: auto; line-height: 24px; ">
<p class="pagenumber" style="font-style: italic; text-align: right; font-size: 12px; "><a href="http://www.cocoadevcentral.com/articles/000084.php#7" style="color: #e24628; ">7</a>&nbsp;of 9</p>
<h3 style="font-weight: bold; color: #e24628; font-size: 1em; ">How Archiving Works</h3>
<p>Once&nbsp;<var style="font-style: normal; color: #0a4aa7; font-weight: bold; ">NSKeyedArchiver</var>&nbsp;receives the +archiveRootObject:toFile: message, it recursively calls<nobr>-encodeWithCoder</nobr>&nbsp;on the entire tree of objects inside the root object. Each instance of Mailbox and Email adds its own bit of data to the archiver.</p>
<center><img src="http://www.cocoadevcentral.com/images/articles/archiving-diagram.gif" alt="Archiving Diagram" border="0" /></center>
<p>After all of the objects have added their encoded data, the archiver automatically creates an NSData object and writes its contents to the file path we supplied.</p>
</div>
<div><a name="8" style="width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "></a></div>
<div class="content" style="background-color: white; color: #4e4e4e; width: 580px; padding-top: 10px; padding-right: 50px; padding-bottom: 10px; padding-left: 50px; margin-top: 20px; margin-left: auto; margin-right: auto; line-height: 24px; ">
<p class="pagenumber" style="font-style: italic; text-align: right; font-size: 12px; "><a href="http://www.cocoadevcentral.com/articles/000084.php#8" style="color: #e24628; ">8</a>&nbsp;of 9</p>
<h3 style="font-weight: bold; color: #e24628; font-size: 1em; ">Reloading Data at Startup</h3>
<p>Now we will implement a method in&nbsp;<strong style="font-weight: normal; color: #161616; ">MyController.m</strong>&nbsp;to load the data from disk:</p>
<div class="filename" style="display: block; background-color: #e8e8e8; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; line-height: 12px; margin-left: 22px; border-left-color: #d3d3d3; font-family: 'Trebuchet MS', Trebuchet, Tahoma, sans-serif; font-size: 12px; color: #383838; ">MyController.m</div>
<div class="example" style="display: block; background-color: #f7f7f7; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; line-height: 16px; margin-left: 22px; border-left-color: #d3d3d3; font-family: Monaco, monospace; font-size: 11px; white-space: pre; ">
- (void) <em style="font-weight: normal; font-style: normal; color: #e24628; ">loadDataFromDisk</em>
{
NSString     * <em style="font-weight: normal; font-style: normal; color: #e24628; ">path</em>        = [self pathForDataFile];
NSDictionary * <em style="font-weight: normal; font-style: normal; color: #e24628; ">rootObject</em>;
<em style="font-weight: normal; font-style: normal; color: #e24628; ">rootObject</em> = [<var style="font-style: normal; color: #0a4aa7; font-weight: normal; ">NSKeyedUnarchiver</var> unarchiveObjectWithFile:<em style="font-weight: normal; font-style: normal; color: #e24628; ">path</em>];
[self setMailboxes: [<em style="font-weight: normal; font-style: normal; color: #e24628; ">rootObject</em> valueForKey:@"mailboxes"]];
}
</div>
<p>This is essentially the reverse of the saving method. Once again, we retrieve the path to the file and create a dictionary which will serve as the root object.</p>
<p>This time, though, we're talking to the&nbsp;<var style="font-style: normal; color: #0a4aa7; font-weight: bold; ">NSKeyed<u>Un</u>archiver</var>&nbsp;class, which is similar to<var style="font-style: normal; color: #0a4aa7; font-weight: bold; ">NSKeyedArchiver</var>, but specializes in loading data from disk. We send it the<nobr>-unarchiveObjectWithFile</nobr>&nbsp;message, which will read the data file, un-encode the contents, and return the root object.</p>
<p>After that, it's just a matter of using -setMailboxes to repopulate the mailboxes array with the saved data.</p>
</div>
<div><a name="9" style="width: 20px; height: 20px; text-indent: 20px; background-image: url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=image&amp;file=anchor.gif); background-repeat: no-repeat no-repeat; "></a></div>
<div class="content" style="background-color: white; color: #4e4e4e; width: 580px; padding-top: 10px; padding-right: 50px; padding-bottom: 10px; padding-left: 50px; margin-top: 20px; margin-left: auto; margin-right: auto; line-height: 24px; ">
<p class="pagenumber" style="font-style: italic; text-align: right; font-size: 12px; "><a href="http://www.cocoadevcentral.com/articles/000084.php#9" style="color: #e24628; ">9</a>&nbsp;of 9</p>
<h3 style="font-weight: bold; color: #e24628; font-size: 1em; ">Automating Save and Reload</h3>
<p>MailDemo can now save and reload data, but the process should ideally be transparent to the user. We only need to add a few lines of code to&nbsp;<strong style="font-weight: normal; color: #161616; ">MyController.m</strong>&nbsp;to make that happen:</p>
<div class="filename" style="display: block; background-color: #e8e8e8; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; line-height: 12px; margin-left: 22px; border-left-color: #d3d3d3; font-family: 'Trebuchet MS', Trebuchet, Tahoma, sans-serif; font-size: 12px; color: #383838; ">MyController.m</div>
<div class="example" style="display: block; background-color: #f7f7f7; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; line-height: 16px; margin-left: 22px; border-left-color: #d3d3d3; font-family: Monaco, monospace; font-size: 11px; white-space: pre; ">
- (void) <strong style="font-weight: normal; color: #161616; ">awakeFromNib</strong>
{
[NSApp <em style="font-weight: normal; font-style: normal; color: #e24628; ">setDelegate</em>: self];
[self <em style="font-weight: normal; font-style: normal; color: #e24628; ">loadDataFromDisk</em>];
}
</div>
<p>The -setDelegate message registers MyController to be the application delegate. It's possible to do this in Interface Builder as well.</p>
<p>The next line of code simply calls -loadDataFromDisk. Since MailDemo uses Cocoa bindings, the table views are automatically updated after the data is loaded.</p>
<p>To complete the cycle, we need to make sure the data is saved when the application quits. To do this, add the -applicationWillTerminate delegate method show below:</p>
<div class="example" style="display: block; background-color: #f7f7f7; padding-top: 5px; padding-right: 10px; padding-bottom: 5px; padding-left: 10px; line-height: 16px; margin-left: 22px; border-left-color: #d3d3d3; font-family: Monaco, monospace; font-size: 11px; white-space: pre; ">
- (void) <strong style="font-weight: normal; color: #161616; ">applicationWillTerminate</strong>: (NSNotification *)note
{
[self <em style="font-weight: normal; font-style: normal; color: #e24628; ">saveDataToDisk</em>];
}
</div>
<p>We're all set! Build and run the project. You should see that your data stays intact between sessions. If you encountered any difficulties, go back and repeat the steps above.</p>
</div>
<div class="content" style="color: #4e4e4e; width: 580px; padding-top: 10px; padding-right: 50px; padding-bottom: 10px; padding-left: 50px; margin-top: 20px; margin-left: auto; margin-right: auto; line-height: 24px; background-color: white; "><br />
<h3 style="font-weight: bold; color: #e24628; font-size: 1em; ">Wrap Up</h3>
<p>We've added NSCoding support to the data classes and saving support to the application. Implementing encoding also sets your application up to support copy/paste and drag-and-drop. Both are typically accomplished by writing encoded objects to the pasteboard.</p>
<p>&gt;&nbsp;<a href="http://www.cocoadevcentral.com/downloads/MailDemo-000084-end.zip" style="color: #e24628; ">Download the&nbsp;<em>Finished</em>&nbsp;MailDemo Xcode project</a>&nbsp;57k</p>
<p>As always,&nbsp;<a href="http://www.cocoadevcentral.com/message.page" style="color: #e24628; ">let us know</a>&nbsp;what you think about the tutorial.</p>
<br />
<h3 style="font-weight: bold; color: #e24628; font-size: 1em; ">Further Reading</h3>
<div class="furtherreading" style="font-family: 'Lucida Grande', Helvetica, sans-serif; line-height: 15px; font-size: 11px; ">
<div class="body">
<table cellpadding="2" cellspacing="2" style="width: 580px; ">
    <tbody>
        <tr>
            <td class="icon" style="width: 15px; "><a href="http://www.cocoadevcentral.com/articles/000080.php" style="color: #4e4e4e; text-decoration: none; border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: gray; "><img src="http://www.cocoadevcentral.com/images/global/miniglobe-orange.png" alt="Local Page" valign="middle" border="0" /></a></td>
            <td><a href="http://www.cocoadevcentral.com/articles/000080.php" style="color: #4e4e4e; text-decoration: none; border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: gray; ">Intro to Cocoa Bindings</a></td>
            <td>the original tutorial which used MailDemo</td>
        </tr>
        <tr>
            <td class="icon" style="width: 15px; "><img src="http://www.cocoadevcentral.com/images/global/mini-apple.png" alt="Apple" /></td>
            <td><a href="http://developer.apple.com/documentation/Cocoa/Conceptual/Archiving/" style="color: #4e4e4e; text-decoration: none; border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: gray; ">Archives and Serialization</a></td>
            <td>Apple's official documentation for archiving</td>
        </tr>
        <tr>
            <td class="icon" style="width: 15px; "><a href="http://theocacao.com/" style="color: #4e4e4e; text-decoration: none; border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: gray; "><img src="http://www.cocoadevcentral.com/images/global/miniglobe-blue.png" alt="Remote Site" valign="middle" border="0" /></a></td>
            <td><a href="http://theocacao.com/" style="color: #4e4e4e; text-decoration: none; border-bottom-width: 1px; border-bottom-style: dotted; border-bottom-color: gray; ">Cocoa Dev Central blog</a></td>
            <td>author's personal site</td>
        </tr>
    </tbody>
</table>
</div>
</div>
<p class="footer" style="background-color: #f7f7f7; border-top-width: 1px; border-top-style: dashed; border-top-color: gray; margin-top: 65px; margin-bottom: 0px; ">Copyright &#169; 2005&nbsp;<a href="http://www.cocoadevcentral.com/message.page" style="color: #e24628; ">Scott Stevenson</a></p>
<p class="footer" style="background-color: #f7f7f7; border-top-width: 1px; border-top-style: dashed; border-top-color: gray; margin-top: 65px; margin-bottom: 0px; ">From:&nbsp;http://www.cocoadevcentral.com/articles/000084.php</p>
</div>
</div>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&amp;file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/161302.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2011-12-02 00:08 <a href="http://www.cppblog.com/biao/archive/2011/12/02/161302.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cocoa：NSArray and KVC</title><link>http://www.cppblog.com/biao/archive/2011/12/02/161301.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 01 Dec 2011 16:07:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2011/12/02/161301.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/161301.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2011/12/02/161301.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/161301.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/161301.html</trackback:ping><description><![CDATA[<div style="background-color: #e4e1d9; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">
<div id="mastercontainer" style="width: 845px; margin-left: auto; margin-right: auto; position: relative; ">
<div id="containerbgleft" style="width: 65px; height: 311px; position: absolute; top: 0px; left: 5px; "><img src="http://theocacao.com/images/document/container-bg-left.png"  alt="" /></div>
<div id="pagemarkleft" style="width: 166px; position: absolute; top: 320px; left: 34px; "><img src="http://theocacao.com/images/document/page-mark-left.png"  alt="" /></div>
<div id="pagemarkright" style="width: 34px; height: 542px; position: absolute; top: 334px; right: 41px; "><img src="http://theocacao.com/images/document/page-mark-right.png"  alt="" /></div>
<div id="contentcontainer" style="width: 731px; margin-left: auto; margin-right: auto; ">
<div id="masthead" style="width: 701px; height: 301px; background-image: url(http://theocacao.com/images/document/bg.jpg); position: relative; margin-left: 12px; margin-top: -22px; background-repeat: no-repeat no-repeat; ">
<div id="rightimage" style="width: 307px; height: 296px; position: absolute; bottom: -20px; right: 0px; border-style: initial; border-color: initial; "><a href="http://theocacao.com/document.page/165#"><img src="http://theocacao.com/images/document/feminine-spirit-and-lion.png" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "  alt="" /></a></div>
<div id="leftimage" style="width: 210px; height: 223px; position: absolute; bottom: 0px; left: -18px; border-style: initial; border-color: initial; "><a href="http://theocacao.com/document.page/165#"><img src="http://theocacao.com/images/shared/standing-globe.png" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "  alt="" /></a></div>
</div>
<div class="content" style="width: 731px; min-height: 525px; background-image: url(http://theocacao.com/images/document/page-bg.png); padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Georgia, serif; font-size: 14px; color: #383838; line-height: 1.6em; background-repeat: no-repeat repeat; ">&nbsp;
<div class="title" style="width: 731px; right: 18px; text-align: center; "><a href="http://theocacao.com/"><img src="http://theocacao.com/images/shared/Theocacao-Title.png" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; margin-left: -32px; "  alt="" /></a><br />
<div style="width: 526px; margin-left: auto; margin-right: auto; border-left-color: #bdbdbd; padding-top: 8px; padding-right: 8px; padding-bottom: 8px; padding-left: 8px; font-style: italic; margin-top: 20px; "><a href="http://cocoabook.com/" style="color: #802e00; ">Cocoa and Objective-C: Up and Running</a>&nbsp;(by me) is now available from&nbsp;<a href="http://oreilly.com/catalog/9780596804794/" style="color: #802e00; ">O'Reilly</a>.</div>
</div>
<div class="contenttext" style="font-family: Georgia, serif; font-size: 14px; line-height: 1.6em; width: 540px; text-align: justify; margin-left: 86px; margin-top: 25px; background-color: white; color: #383838; padding-bottom: 20px; ">
<h2 style="font-weight: normal; padding-left: 0px; padding-top: 0px; z-index: 10; ">NSArray and KVC</h2>
Even more on simplifying code with generic programming! NSArray's -valueForKey: has a feature that may not be immediately obvious. You can use it to return an array of values buried within a tree of objects. Here's a working example:<br />
<br />
<code style="background-color: #f3f3f3; display: block; margin-top: 0.8em; margin-left: 0em; margin-right: -22px; margin-bottom: -20px; padding-top: 1em; padding-right: 0.8em; padding-bottom: 1em; padding-left: 0.8em; font-size: 11.5px; color: black; line-height: 1.6em; font-family: Monaco, monospace; text-shadow: #f3f3f3 0px 0px 1px; text-align: left; "><br />
NSMutableArray * tree = [NSMutableArray array];<br />
<br />
NSDictionary&nbsp;&nbsp; * p = nil;&nbsp;&nbsp; // parent<br />
NSDictionary&nbsp;&nbsp; * c = nil;&nbsp;&nbsp; // child&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
NSNumber&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; * n = nil;&nbsp;&nbsp; // value<br />
<br />
int i;<br />
for ( i = 0; i &lt; 10; i++ )<br />
{<br />
&nbsp;&nbsp;n = [NSNumber numberWithInt: i];<br />
<br />
&nbsp;&nbsp;c = [NSDictionary dictionaryWithObject: n<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;forKey: @"someKey"];<br />
<br />
&nbsp;&nbsp;p = [NSDictionary dictionaryWithObject: c<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;forKey: @"storage"];<br />
<br />
&nbsp;&nbsp;[tree addObject: p];<br />
}<br />
<br />
<br />
NSLog (@"%@", tree);<br />
<br />
<br />
// here's the important part!<br />
<br />
NSArray * justValues;<br />
justValues = [tree valueForKeyPath: @"storage.someKey"];<br />
<br />
<br />
NSLog (@"%@", justValues);<br />
<br />
</code><br />
<br />
<br />
The first NSLog spits this out -- just a two-level property list:<br />
<br />
<code style="background-color: #f3f3f3; display: block; margin-top: 0.8em; margin-left: 0em; margin-right: -22px; margin-bottom: -20px; padding-top: 1em; padding-right: 0.8em; padding-bottom: 1em; padding-left: 0.8em; font-size: 11.5px; color: black; line-height: 1.6em; font-family: Monaco, monospace; text-shadow: #f3f3f3 0px 0px 1px; text-align: left; "><br />
NSLog (@"%@", tree);<br />
<br />
</code><br />
<code style="background-color: #f3f3f3; display: block; margin-top: 0.8em; margin-left: 0em; margin-right: -22px; margin-bottom: -20px; padding-top: 1em; padding-right: 0.8em; padding-bottom: 1em; padding-left: 0.8em; font-size: 11.5px; color: black; line-height: 1.6em; font-family: Monaco, monospace; text-shadow: #f3f3f3 0px 0px 1px; text-align: left; "><br />
(<br />
&nbsp;&nbsp;&nbsp;&nbsp;{storage = {someKey = 0; }; },&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;{storage = {someKey = 1; }; },&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;{storage = {someKey = 2; }; },&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;{storage = {someKey = 3; }; },&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;{storage = {someKey = 4; }; },&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;{storage = {someKey = 5; }; },&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;{storage = {someKey = 6; }; },&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;{storage = {someKey = 7; }; },&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;{storage = {someKey = 8; }; },&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;{storage = {someKey = 9; }; }<br />
)<br />
<br />
</code><br />
<br />
The second NSLog spits out an array of values collected by asking each contained object for the value at @"storage.someKey":<br />
<br />
<code style="background-color: #f3f3f3; display: block; margin-top: 0.8em; margin-left: 0em; margin-right: -22px; margin-bottom: -20px; padding-top: 1em; padding-right: 0.8em; padding-bottom: 1em; padding-left: 0.8em; font-size: 11.5px; color: black; line-height: 1.6em; font-family: Monaco, monospace; text-shadow: #f3f3f3 0px 0px 1px; text-align: left; "><br />
NSArray * justValues;<br />
justValues = [tree valueForKeyPath: @"storage.someKey"];<br />
<br />
NSLog (@"%@", justValues);<br />
<br />
</code><br />
<code style="background-color: #f3f3f3; display: block; margin-top: 0.8em; margin-left: 0em; margin-right: -22px; margin-bottom: -20px; padding-top: 1em; padding-right: 0.8em; padding-bottom: 1em; padding-left: 0.8em; font-size: 11.5px; color: black; line-height: 1.6em; font-family: Monaco, monospace; text-shadow: #f3f3f3 0px 0px 1px; text-align: left; "><br />
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)<br />
<br />
</code><br />
<br />
I find this really convenient in Core Data when working with a sorted array of&nbsp;&nbsp;managed objects -- you can pull out an array of values for just one attribute.
<div class="dividingimage" style="text-align: center; margin-top: 28px; margin-bottom: 0px; "><img src="http://theocacao.com/images/shared/Divider-Default.png" alt="Design Element" />
<div style="text-align: center; font-style: italic; margin-top: 10px; "><a href="http://theocacao.com/document.page/165" style="color: #585858; text-decoration: none; font-style: italic; ">NSArray and KVC</a><br />
Posted Oct 04, 2005 &#8212; 4 comments below</div>
</div>
</div>
</div>
<div id="pagecap" style="width: 731px; height: 29px; position: relative; margin-left: 0px; "><img src="http://theocacao.com/images/document/page-bottom.png"  alt="" /></div>
<br />
<br />
<br />
<br />
<div id="pagecap" style="width: 731px; height: 38px; position: relative; margin-left: 0px; "><img src="http://theocacao.com/images/document/page-top.png"  alt="" /></div>
<div class="content" style="width: 731px; min-height: 0px; background-image: url(http://theocacao.com/images/document/page-bg.png); padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Georgia, serif; font-size: 14px; color: #383838; line-height: 1.6em; background-repeat: no-repeat repeat; ">&nbsp;
<div class="contenttext" style="font-family: Georgia, serif; font-size: 14px; line-height: 1.6em; width: 540px; text-align: justify; margin-left: 86px; margin-top: 25px; background-color: white; color: #383838; padding-bottom: 20px; ">
<div class="comments">
<div>
<h3 style="font-weight: normal; padding-left: 0px; padding-top: 0px; font-style: italic; padding-bottom: 0px; margin-bottom: 8px; ">Abhi Beckert &#8212; Oct 12, 05&nbsp;<a href="http://theocacao.com/comment/431" style="color: #a4a4a4; text-decoration: none; font-style: italic; font-size: 11px; ">431</a></h3>
</div>
<div class="commentbody" style="font-size: 13px; margin-bottom: 30px; padding-left: 15px; padding-right: 15px; border-left-color: #dedede; ">*ching*<br />
<br />
Now I finally understand how the binding key for a table column works! Thanks ;)</div>
<div>
<h3 style="font-weight: normal; padding-left: 0px; padding-top: 0px; font-style: italic; padding-bottom: 0px; margin-bottom: 8px; ">Dirk &#8212; Mar 09, 06&nbsp;<a href="http://theocacao.com/comment/923" style="color: #a4a4a4; text-decoration: none; font-style: italic; font-size: 11px; ">923</a></h3>
</div>
<div class="commentbody" style="font-size: 13px; margin-bottom: 30px; padding-left: 15px; padding-right: 15px; border-left-color: #dedede; ">Unfortunately, I do not. I tried to show the results in "justvalues" in a TableView column using bindings. I use an ArrayController whose content outlet is connected to an instance of the class. The column of the TableView is bound to the ArrayController, controller key is arranged objects, model key path is tree.storage.someKey.<br />
This does not work unfortunately. If I switch to another application, the TableView will eventually show the (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) - but in just one row! :-(&nbsp;<br />
I am looking for an really easy example to this since - well, very long. I've gone through a lot of them on the web - but since I am very new to cocoa, I found them to complicated. Maybe you could give some hints? Thanks in advance and greetings ;-)</div>
<div>
<h3 style="font-weight: normal; padding-left: 0px; padding-top: 0px; font-style: italic; padding-bottom: 0px; margin-bottom: 8px; "><a href="http://homepage.mac.com/bagelturf/index.html" style="color: #802e00; text-decoration: none; font-style: italic; ">Bagelturf</a>&nbsp;&#8212; Mar 24, 07&nbsp;<a href="http://theocacao.com/comment/3807" style="color: #a4a4a4; text-decoration: none; font-style: italic; font-size: 11px; ">3807</a></h3>
</div>
<div class="commentbody" style="font-size: 13px; margin-bottom: 30px; padding-left: 15px; padding-right: 15px; border-left-color: #dedede; ">Dirk,<br />
<br />
Try my ten-part KVC tutorial at:<br />
<br />
http://www.bagelturf.com/cocoa/kvc1/kvc1.html<br />
<br />
It is *very* simple and includes downloadable projects for each step. I did it to convince myself that I understood KVC.<br />
<br />
(The links to the download page in the text are incorrect. Use the one from the menu on the right.)</div>
<div>
<h3 style="font-weight: normal; padding-left: 0px; padding-top: 0px; font-style: italic; padding-bottom: 0px; margin-bottom: 8px; ">Ben &#8212; May 20, 09&nbsp;<a href="http://theocacao.com/comment/6778" style="color: #a4a4a4; text-decoration: none; font-style: italic; font-size: 11px; ">6778</a></h3>
</div>
<div class="commentbody" style="font-size: 13px; margin-bottom: 30px; padding-left: 15px; padding-right: 15px; border-left-color: #dedede; ">Can i extend this to traverse a sequence of KVC items e.g. first.second.third.fourth<br />
<br />
Also can this mechanism just deal with to-one relationships or is there a way to make it link to an array and then follow into the items returned by the array items.</div>
</div>
<div class="dividingimage" style="text-align: center; margin-top: 28px; margin-bottom: 0px; "><img src="http://theocacao.com/images/shared/Divider-Default.png" alt="Design Element" /><br />
<br />
<a href="feed://theocacao.com/comments.rss" style="color: #802e00; text-decoration: none; font-style: italic; ">Comments Feed</a></div>
</div>
</div>
<div id="pagecap" style="width: 731px; height: 29px; position: relative; margin-left: 0px; "><img src="http://theocacao.com/images/document/page-bottom.png"  alt="" /></div>
<br />
<br />
<br />
<br />
<div id="pagecap" style="width: 731px; height: 38px; position: relative; margin-left: 0px; "><img src="http://theocacao.com/images/document/page-top.png"  alt="" /></div>
<div class="content" style="width: 731px; min-height: 0px; background-image: url(http://theocacao.com/images/document/page-bg.png); padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Georgia, serif; font-size: 14px; color: #383838; line-height: 1.6em; background-repeat: no-repeat repeat; ">&nbsp;
<div class="contenttext" style="font-family: Georgia, serif; font-size: 14px; line-height: 1.6em; width: 540px; text-align: justify; margin-left: 86px; margin-top: 25px; background-color: white; color: #383838; padding-bottom: 20px; ">
<h2 style="font-weight: normal; padding-left: 0px; padding-top: 0px; ">Comments Temporarily Disabled</h2>
<p>I had to temporarily disable comments due to spam. I'll re-enable them soon.</p>
</div>
</div>
<div id="pagecap" style="width: 731px; height: 29px; position: relative; margin-left: 0px; "><img src="http://theocacao.com/images/document/page-bottom.png"  alt="" /></div>
</div>
</div>
<br />
<br />
<br />
<p style="text-align: center; margin-top: 24px; font-size: 13px; "><a href="http://technorati.com/profile/scottstevenson" rel="me" style="text-decoration: none; font-size: 13px; font-style: italic; color: black; ">Technorati Profile</a><br />
Copyright &#169; Scott Stevenson 2004-2008</p>
<p style="text-align: center; margin-top: 24px; font-size: 13px; ">http://theocacao.com/document.page/165</p>
</div>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/161301.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2011-12-02 00:07 <a href="http://www.cppblog.com/biao/archive/2011/12/02/161301.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cocoa：Key-Value Coding (KVC) and Generic Programming</title><link>http://www.cppblog.com/biao/archive/2011/12/02/161300.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 01 Dec 2011 16:05:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2011/12/02/161300.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/161300.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2011/12/02/161300.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/161300.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/161300.html</trackback:ping><description><![CDATA[<div style="background-color: #e4e1d9; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">
<div id="mastercontainer" style="width: 845px; margin-left: auto; margin-right: auto; position: relative; ">
<div id="containerbgleft" style="width: 65px; height: 311px; position: absolute; top: 0px; left: 5px; "><img src="http://theocacao.com/images/document/container-bg-left.png"  alt="" /></div>
<div id="pagemarkleft" style="width: 166px; position: absolute; top: 320px; left: 34px; "><img src="http://theocacao.com/images/document/page-mark-left.png"  alt="" /></div>
<div id="pagemarkright" style="width: 34px; height: 542px; position: absolute; top: 334px; right: 41px; "><img src="http://theocacao.com/images/document/page-mark-right.png"  alt="" /></div>
<div id="contentcontainer" style="width: 731px; margin-left: auto; margin-right: auto; ">
<div id="masthead" style="width: 701px; height: 301px; background-image: url(http://theocacao.com/images/document/bg.jpg); position: relative; margin-left: 12px; margin-top: -22px; background-repeat: no-repeat no-repeat; ">
<div id="rightimage" style="width: 307px; height: 296px; position: absolute; bottom: -20px; right: 0px; border-style: initial; border-color: initial; "><a href="http://theocacao.com/document.page/161#"><img src="http://theocacao.com/images/document/feminine-spirit-and-lion.png" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "  alt="" /></a></div>
<div id="leftimage" style="width: 210px; height: 223px; position: absolute; bottom: 0px; left: -18px; border-style: initial; border-color: initial; "><a href="http://theocacao.com/document.page/161#"><img src="http://theocacao.com/images/shared/standing-globe.png" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "  alt="" /></a></div>
</div>
<div class="content" style="width: 731px; min-height: 525px; background-image: url(http://theocacao.com/images/document/page-bg.png); padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Georgia, serif; font-size: 14px; color: #383838; line-height: 1.6em; background-repeat: no-repeat repeat; ">&nbsp;
<div class="title" style="width: 731px; right: 18px; text-align: center; "><a href="http://theocacao.com/"><img src="http://theocacao.com/images/shared/Theocacao-Title.png" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; margin-left: -32px; "  alt="" /></a><br />
<div style="width: 526px; margin-left: auto; margin-right: auto; border-left-color: #bdbdbd; padding-top: 8px; padding-right: 8px; padding-bottom: 8px; padding-left: 8px; font-style: italic; margin-top: 20px; "><a href="http://cocoabook.com/" style="color: #802e00; ">Cocoa and Objective-C: Up and Running</a>&nbsp;(by me) is now available from&nbsp;<a href="http://oreilly.com/catalog/9780596804794/" style="color: #802e00; ">O'Reilly</a>.</div>
</div>
<div class="contenttext" style="font-family: Georgia, serif; font-size: 14px; line-height: 1.6em; width: 540px; text-align: justify; margin-left: 86px; margin-top: 25px; background-color: white; color: #383838; padding-bottom: 20px; ">
<h2 style="font-weight: normal; padding-left: 0px; padding-top: 0px; z-index: 10; ">Key-Value Coding (KVC) and Generic Programming</h2>
Key-Value Coding (KVC) is a Cocoa protocol for getting and setting values generically. In programming, the term "generically" describes a way of doing things that applies to many different situations.<br />
<br />
Generic code can reduce to total amount of code in a project (which is always good) and helps software to handle situations that the programmer didn't anticipate. Generic, reusable code is emphasized throughout Cocoa.<br />
<br />
For example, here's a non-generic way to set a first name and last name on an object:<br />
<br />
<code style="background-color: #f3f3f3; display: block; margin-top: 0.8em; margin-left: 0em; margin-right: -22px; margin-bottom: -20px; padding-top: 1em; padding-right: 0.8em; padding-bottom: 1em; padding-left: 0.8em; font-size: 11.5px; color: black; line-height: 1.6em; font-family: Monaco, monospace; text-shadow: #f3f3f3 0px 0px 1px; text-align: left; "><br />
[person setFirstName: @"Scott"];<br />
[person setLastName:&nbsp;&nbsp;@"Stevenson"];<br />
<br />
</code><br />
<br />
This works fine, but I can use KVC messages to write more generic code:<br />
<br />
<code style="background-color: #f3f3f3; display: block; margin-top: 0.8em; margin-left: 0em; margin-right: -22px; margin-bottom: -20px; padding-top: 1em; padding-right: 0.8em; padding-bottom: 1em; padding-left: 0.8em; font-size: 11.5px; color: black; line-height: 1.6em; font-family: Monaco, monospace; text-shadow: #f3f3f3 0px 0px 1px; text-align: left; "><br />
[person setValue:@"Scott" forKey:@"firstName"];<br />
[person setValue:@"Stevenson" forKey:@"lastName"];<br />
<br />
</code><br />
<br />
Beginners might wonder what the point is here. In fact, it actually seems like the KVC version requires&nbsp;<em>more</em>&nbsp;typing. Let's choose another scenario where KVC's value is more apparent.<br />
<br />
First, let's define the class:<br />
<br />
<code style="background-color: #f3f3f3; display: block; margin-top: 0.8em; margin-left: 0em; margin-right: -22px; margin-bottom: -20px; padding-top: 1em; padding-right: 0.8em; padding-bottom: 1em; padding-left: 0.8em; font-size: 11.5px; color: black; line-height: 1.6em; font-family: Monaco, monospace; text-shadow: #f3f3f3 0px 0px 1px; text-align: left; "><br />
@interface CDCPerson : NSObject<br />
{<br />
&nbsp;&nbsp;&nbsp;&nbsp;NSString * firstName;<br />
&nbsp;&nbsp;&nbsp;&nbsp;NSString * lastName;<br />
&nbsp;&nbsp;&nbsp;&nbsp;NSNumber * phoneNumber;<br />
&nbsp;&nbsp;&nbsp;&nbsp;NSString * emailAddress;<br />
}<br />
- (void) setFirstName:&nbsp;&nbsp;&nbsp;&nbsp;(NSString *)value;<br />
- (void) setLastName:&nbsp;&nbsp;&nbsp;&nbsp; (NSString *)value;<br />
- (void) setPhoneNumber:&nbsp;&nbsp;(NSNumber *)value;<br />
- (void) setEmailAddress: (NSString *)value;<br />
@end<br />
<br />
</code><br />
<br />
Now, some actual code:<br />
<br />
<code style="background-color: #f3f3f3; display: block; margin-top: 0.8em; margin-left: 0em; margin-right: -22px; margin-bottom: -20px; padding-top: 1em; padding-right: 0.8em; padding-bottom: 1em; padding-left: 0.8em; font-size: 11.5px; color: black; line-height: 1.6em; font-family: Monaco, monospace; text-shadow: #f3f3f3 0px 0px 1px; text-align: left; "><br />
// assume inputValues contains values we want to<br />
// set on the person<br />
<br />
NSDictionary * inputValues;<br />
CDCPerson&nbsp;&nbsp;&nbsp;&nbsp;* person = [[CDCPerson alloc] init];<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
NSEnumerator *e = [inputValues keyEnumerator];<br />
id dictKey, dictValue;<br />
<br />
while ( dictKey = [e nextObject] )<br />
{&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;dictValue = [inputValues valueForKey: dictKey];<br />
&nbsp;&nbsp;&nbsp;&nbsp;[person setValue: dictValue forKey: dictKey];<br />
}<br />
<br />
</code><br />
<br />
This snippet of code is generic, meaning that we don't need to change it everytime new instance variables are added to the Person class.<br />
<br />
But it gets better! Here's an even simpler version of the code above:<br />
<br />
<code style="background-color: #f3f3f3; display: block; margin-top: 0.8em; margin-left: 0em; margin-right: -22px; margin-bottom: -20px; padding-top: 1em; padding-right: 0.8em; padding-bottom: 1em; padding-left: 0.8em; font-size: 11.5px; color: black; line-height: 1.6em; font-family: Monaco, monospace; text-shadow: #f3f3f3 0px 0px 1px; text-align: left; "><br />
// assume inputValues contains values we want to<br />
// set on the person<br />
<br />
NSDictionary * inputValues;<br />
CDCPerson&nbsp;&nbsp;&nbsp;&nbsp;* person = [[CDCPerson alloc] init];<br />
<br />
[person setValuesForKeysWithDictionary: inputValues];<br />
<br />
</code><br />
<br />
Intrigued? Here's Apple explanation of what's happening in -setValuesForKeysWithDictionary:<br />
<br />
<blockquote style="font-style: italic; margin-bottom: -1em; "><br />
Sets properties of the receiver with values from keyedValues, using its keys to identify the properties. The default implementation invokes setValue:forKey: for each key-value pair, substituting nil for NSNull values in keyedValues.<br />
</blockquote><br />
<br />
In other words, essentially the same as the first example. But what is -setValue:forKey: actually doing? This is where the KVC magic comes in. It will actually find the -setFirstName:, -setLastName:, -setPhoneNumber: and -setEmailAddress: implementations and call those. If it can't find these, KVC will try quite a few different options before ultimately just setting a value on the instance variable itself.<br />
<br />
KVC can also be used to pull values out of an object:<br />
<br />
<code style="background-color: #f3f3f3; display: block; margin-top: 0.8em; margin-left: 0em; margin-right: -22px; margin-bottom: -20px; padding-top: 1em; padding-right: 0.8em; padding-bottom: 1em; padding-left: 0.8em; font-size: 11.5px; color: black; line-height: 1.6em; font-family: Monaco, monospace; text-shadow: #f3f3f3 0px 0px 1px; text-align: left; "><br />
// assume person already exists and is populated with values<br />
<br />
CDCPerson * person;<br />
<br />
NSMutableDictionary * outputValues;<br />
outputValues = [NSMutableDictionary dictionary];<br />
<br />
NSArray * keys;<br />
keys = [NSArray arrayWithObjects: @"firstName",<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@"lastName",<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@"phoneNumber",<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@"emailAddress",<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nil];<br />
<br />
NSEnumerator *e = [keys objectEnumator];<br />
id key, value;<br />
<br />
while ( key = [e nextObject] )<br />
{&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;value = [person valueForKey: key];<br />
&nbsp;&nbsp;&nbsp;&nbsp;[outputValues setValue: value forKey: key];<br />
}<br />
<br />
</code><br />
<br />
<br />
Or, the simpler version:<br />
<br />
<code style="background-color: #f3f3f3; display: block; margin-top: 0.8em; margin-left: 0em; margin-right: -22px; margin-bottom: -20px; padding-top: 1em; padding-right: 0.8em; padding-bottom: 1em; padding-left: 0.8em; font-size: 11.5px; color: black; line-height: 1.6em; font-family: Monaco, monospace; text-shadow: #f3f3f3 0px 0px 1px; text-align: left; "><br />
// assume person already exists and is populated with values<br />
<br />
CDCPerson&nbsp;&nbsp;&nbsp;&nbsp;* person;<br />
NSArray&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* keys;<br />
<br />
keys = [NSArray arrayWithObjects: @"firstName",<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@"lastName",<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@"phoneNumber",<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@"emailAddress",<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nil];<br />
<br />
NSDictionary * outputValues;<br />
outputValues = [person dictionaryWithValuesForKeys: keys];<br />
<br />
</code><br />
<br />
Just as with setting values, getting values with -valueForKey: will cause KVC to look for a method the same name as the key:<br />
<br />
<code style="background-color: #f3f3f3; display: block; margin-top: 0.8em; margin-left: 0em; margin-right: -22px; margin-bottom: -20px; padding-top: 1em; padding-right: 0.8em; padding-bottom: 1em; padding-left: 0.8em; font-size: 11.5px; color: black; line-height: 1.6em; font-family: Monaco, monospace; text-shadow: #f3f3f3 0px 0px 1px; text-align: left; "><br />
// this will cause KVC to look for a method called -firstName;<br />
<br />
NSString * name = [person valueForKey:@"firstName"];<br />
<br />
</code><br />
<br />
Key-value coding is key element in&nbsp;<a href="http://cocoadevcentral.com/articles/000080.php" style="color: #802e00; text-decoration: none; font-style: italic; ">Cocoa Bindings</a>&nbsp;and&nbsp;<a href="http://cocoadevcentral.com/articles/000085.php" style="color: #802e00; text-decoration: none; font-style: italic; ">Core Data</a>, so it really pays to understand the basic ideas. KVC can handle keypaths, such as:<br />
<br />
<code style="background-color: #f3f3f3; display: block; margin-top: 0.8em; margin-left: 0em; margin-right: -22px; margin-bottom: -20px; padding-top: 1em; padding-right: 0.8em; padding-bottom: 1em; padding-left: 0.8em; font-size: 11.5px; color: black; line-height: 1.6em; font-family: Monaco, monospace; text-shadow: #f3f3f3 0px 0px 1px; text-align: left; "><br />
// getting<br />
[obj valueForKeyPath: @"storage.firstName"];<br />
<br />
// setting<br />
[obj setValue: @"Scott" forKeyPath: @"storage.firstName"];<br />
<br />
</code><br />
<br />
This is similar to doing:<br />
<br />
<code style="background-color: #f3f3f3; display: block; margin-top: 0.8em; margin-left: 0em; margin-right: -22px; margin-bottom: -20px; padding-top: 1em; padding-right: 0.8em; padding-bottom: 1em; padding-left: 0.8em; font-size: 11.5px; color: black; line-height: 1.6em; font-family: Monaco, monospace; text-shadow: #f3f3f3 0px 0px 1px; text-align: left; "><br />
// getting<br />
[[obj storage] firstName];<br />
<br />
// setting<br />
[[obj storage] setFirstName:@"Scott"];<br />
<br />
</code><br />
<br />
For more details on Key-value coding, take a look at&nbsp;<a href="http://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/Concepts/BasicPrinciples.html" style="color: #802e00; text-decoration: none; font-style: italic; ">this page</a>&nbsp;on ADC.
<div class="dividingimage" style="text-align: center; margin-top: 28px; margin-bottom: 0px; "><img src="http://theocacao.com/images/shared/Divider-Default.png" alt="Design Element" />
<div style="text-align: center; font-style: italic; margin-top: 10px; "><a href="http://theocacao.com/document.page/161" style="color: #585858; text-decoration: none; font-style: italic; ">Key-Value Coding (KVC) and Generic Programming</a><br />
Posted Oct 03, 2005 &#8212; 11 comments below</div>
</div>
</div>
</div>
<div id="pagecap" style="width: 731px; height: 29px; position: relative; margin-left: 0px; "><img src="http://theocacao.com/images/document/page-bottom.png"  alt="" /></div>
<br />
<br />
<br />
<br />
<div id="pagecap" style="width: 731px; height: 38px; position: relative; margin-left: 0px; "><img src="http://theocacao.com/images/document/page-top.png"  alt="" /></div>
<div class="content" style="width: 731px; min-height: 0px; background-image: url(http://theocacao.com/images/document/page-bg.png); padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Georgia, serif; font-size: 14px; color: #383838; line-height: 1.6em; background-repeat: no-repeat repeat; ">&nbsp;
<div class="contenttext" style="font-family: Georgia, serif; font-size: 14px; line-height: 1.6em; width: 540px; text-align: justify; margin-left: 86px; margin-top: 25px; background-color: white; color: #383838; padding-bottom: 20px; ">
<div class="comments">
<div>
<h3 style="font-weight: normal; padding-left: 0px; padding-top: 0px; font-style: italic; padding-bottom: 0px; margin-bottom: 8px; ">SteveJ &#8212; Oct 03, 05&nbsp;<a href="http://theocacao.com/comment/403" style="color: #a4a4a4; text-decoration: none; font-style: italic; font-size: 11px; ">403</a></h3>
</div>
<div class="commentbody" style="font-size: 13px; margin-bottom: 30px; padding-left: 15px; padding-right: 15px; border-left-color: #dedede; ">In two places where you send the CDCPerson class the allocate message, you don't need the "]" after the class "CDCPerson" and before the message "allocate".</div>
<div>
<h3 style="font-weight: normal; padding-left: 0px; padding-top: 0px; font-style: italic; padding-bottom: 0px; margin-bottom: 8px; "><a href="http://theocacao.com/" style="color: #802e00; text-decoration: none; font-style: italic; ">Scott Stevenson</a>&nbsp;&#8212; Oct 03, 05&nbsp;<a href="http://theocacao.com/comment/404" style="color: #a4a4a4; text-decoration: none; font-style: italic; font-size: 11px; ">404</a>&nbsp;<img src="http://theocacao.com/images/leopard-avatar.png" alt="Scotty the Leopard" width="32" height="30" style="margin-bottom: -8px; margin-left: 8px; " /></h3>
</div>
<div class="commentbody" style="font-size: 13px; margin-bottom: 30px; padding-left: 15px; padding-right: 15px; border-left-color: #dedede; ">Fixed, thanks. I think this was partially due to a smart editing rule in the text editor.</div>
<div>
<h3 style="font-weight: normal; padding-left: 0px; padding-top: 0px; font-style: italic; padding-bottom: 0px; margin-bottom: 8px; "><a href="http://www.red-sweater.com/blog/" style="color: #802e00; text-decoration: none; font-style: italic; ">Daniel Jalkut</a>&nbsp;&#8212; Oct 03, 05&nbsp;<a href="http://theocacao.com/comment/405" style="color: #a4a4a4; text-decoration: none; font-style: italic; font-size: 11px; ">405</a></h3>
</div>
<div class="commentbody" style="font-size: 13px; margin-bottom: 30px; padding-left: 15px; padding-right: 15px; border-left-color: #dedede; ">Just a vote of confidence for this "experiment." I think you have a great writing style and the formatting of your articles is very accessible. The approach you're taking now could produce a sigfnicant "FAQ-like" resource for newbies and oldbies alike.</div>
<div>
<h3 style="font-weight: normal; padding-left: 0px; padding-top: 0px; font-style: italic; padding-bottom: 0px; margin-bottom: 8px; ">Samo Korosec &#8212; Oct 04, 05&nbsp;<a href="http://theocacao.com/comment/406" style="color: #a4a4a4; text-decoration: none; font-style: italic; font-size: 11px; ">406</a></h3>
</div>
<div class="commentbody" style="font-size: 13px; margin-bottom: 30px; padding-left: 15px; padding-right: 15px; border-left-color: #dedede; ">Aren't such articles what Cocoa Dev Central is about anyway? Will you repost them there, too?</div>
<div>
<h3 style="font-weight: normal; padding-left: 0px; padding-top: 0px; font-style: italic; padding-bottom: 0px; margin-bottom: 8px; "><a href="http://waffle.wootest.net/" style="color: #802e00; text-decoration: none; font-style: italic; ">Jesper</a>&nbsp;&#8212; Oct 04, 05&nbsp;<a href="http://theocacao.com/comment/407" style="color: #a4a4a4; text-decoration: none; font-style: italic; font-size: 11px; ">407</a></h3>
</div>
<div class="commentbody" style="font-size: 13px; margin-bottom: 30px; padding-left: 15px; padding-right: 15px; border-left-color: #dedede; ">Samo: Scott uses the CDC prefix for one-off classes in the article, so it seems like the goal is to get it there later on, but to publish it here now so that people can read it in the meantime.</div>
<div>
<h3 style="font-weight: normal; padding-left: 0px; padding-top: 0px; font-style: italic; padding-bottom: 0px; margin-bottom: 8px; "><a href="http://theocacao.com/" style="color: #802e00; text-decoration: none; font-style: italic; ">Scott Stevenson</a>&nbsp;&#8212; Oct 04, 05&nbsp;<a href="http://theocacao.com/comment/408" style="color: #a4a4a4; text-decoration: none; font-style: italic; font-size: 11px; ">408</a>&nbsp;<img src="http://theocacao.com/images/leopard-avatar.png" alt="Scotty the Leopard" width="32" height="30" style="margin-bottom: -8px; margin-left: 8px; " /></h3>
</div>
<div class="commentbody" style="font-size: 13px; margin-bottom: 30px; padding-left: 15px; padding-right: 15px; border-left-color: #dedede; ">Jesper's is basically on the right track. It's easier to experiment quickly here on the blog, and move stuff to CDC once it's cleaned up a bit.</div>
<div>
<h3 style="font-weight: normal; padding-left: 0px; padding-top: 0px; font-style: italic; padding-bottom: 0px; margin-bottom: 8px; ">Samo Korosec &#8212; Oct 04, 05&nbsp;<a href="http://theocacao.com/comment/415" style="color: #a4a4a4; text-decoration: none; font-style: italic; font-size: 11px; ">415</a></h3>
</div>
<div class="commentbody" style="font-size: 13px; margin-bottom: 30px; padding-left: 15px; padding-right: 15px; border-left-color: #dedede; ">Ah, okies. I've just read over the CDC* stuff not thinking about it. Great idea to post such Cocoa helplets, though.</div>
<div>
<h3 style="font-weight: normal; padding-left: 0px; padding-top: 0px; font-style: italic; padding-bottom: 0px; margin-bottom: 8px; ">Tito Ciuro &#8212; Mar 31, 06&nbsp;<a href="http://theocacao.com/comment/997" style="color: #a4a4a4; text-decoration: none; font-style: italic; font-size: 11px; ">997</a></h3>
</div>
<div class="commentbody" style="font-size: 13px; margin-bottom: 30px; padding-left: 15px; padding-right: 15px; border-left-color: #dedede; ">Thanks for the article! I'd like to see keypaths explained a little better. For example, if CDCPerson had an NSArray *siblings (with a few CDCPerson), how could we obtain the list the siblings?<br />
<br />
I'd think we would call it via [person valueForKeyPath: @"siblings.firstName"], but I'm not sure if I'd need extra accessors, especially if we start dealing with a fairly nested dictionary. How would that work?<br />
<br />
Thanks again!</div>
<div>
<h3 style="font-weight: normal; padding-left: 0px; padding-top: 0px; font-style: italic; padding-bottom: 0px; margin-bottom: 8px; "><a href="http://theocacao.com/" style="color: #802e00; text-decoration: none; font-style: italic; ">Scott Stevenson</a>&nbsp;&#8212; Apr 03, 06&nbsp;<a href="http://theocacao.com/comment/1000" style="color: #a4a4a4; text-decoration: none; font-style: italic; font-size: 11px; ">1000</a>&nbsp;<img src="http://theocacao.com/images/leopard-avatar.png" alt="Scotty the Leopard" width="32" height="30" style="margin-bottom: -8px; margin-left: 8px; " /></h3>
</div>
<div class="commentbody" style="font-size: 13px; margin-bottom: 30px; padding-left: 15px; padding-right: 15px; border-left-color: #dedede; "><em style="background-color: #f3f3f3; color: #585858; ">I'd think we would call it via [person valueForKeyPath: @"siblings.firstName"]</em><br />
<br />
That should work fine.</div>
<div>
<h3 style="font-weight: normal; padding-left: 0px; padding-top: 0px; font-style: italic; padding-bottom: 0px; margin-bottom: 8px; "><a href="http://theocacao.com/document.page/hishamkhalifa.com" style="color: #802e00; text-decoration: none; font-style: italic; ">hisham</a>&nbsp;&#8212; Dec 29, 08&nbsp;<a href="http://theocacao.com/comment/6577" style="color: #a4a4a4; text-decoration: none; font-style: italic; font-size: 11px; ">6577</a></h3>
</div>
<div class="commentbody" style="font-size: 13px; margin-bottom: 30px; padding-left: 15px; padding-right: 15px; border-left-color: #dedede; ">Great. I'm new to KVC and this really helped :)<br />
<br />
Thanks so much,<br />
Hisham</div>
<div>
<h3 style="font-weight: normal; padding-left: 0px; padding-top: 0px; font-style: italic; padding-bottom: 0px; margin-bottom: 8px; ">Daniel &#8212; Mar 20, 09&nbsp;<a href="http://theocacao.com/comment/6626" style="color: #a4a4a4; text-decoration: none; font-style: italic; font-size: 11px; ">6626</a></h3>
</div>
<div class="commentbody" style="font-size: 13px; margin-bottom: 30px; padding-left: 15px; padding-right: 15px; border-left-color: #dedede; ">"Beginners might wonder what the point is here. In fact, it actually seems like the KVC version requires more typing. Let's choose another scenario where KVC's value is more apparent."<br />
<br />
That's exactly why I was googling for more info. :) Thanks! Good info.</div>
</div>
<div class="dividingimage" style="text-align: center; margin-top: 28px; margin-bottom: 0px; "><img src="http://theocacao.com/images/shared/Divider-Default.png" alt="Design Element" /><br />
<br />
<a href="feed://theocacao.com/comments.rss" style="color: #802e00; text-decoration: none; font-style: italic; ">Comments Feed</a></div>
</div>
</div>
<div id="pagecap" style="width: 731px; height: 29px; position: relative; margin-left: 0px; "><img src="http://theocacao.com/images/document/page-bottom.png"  alt="" /></div>
<br />
<br />
<br />
<br />
<div id="pagecap" style="width: 731px; height: 38px; position: relative; margin-left: 0px; "><img src="http://theocacao.com/images/document/page-top.png"  alt="" /></div>
<div class="content" style="width: 731px; min-height: 0px; background-image: url(http://theocacao.com/images/document/page-bg.png); padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Georgia, serif; font-size: 14px; color: #383838; line-height: 1.6em; background-repeat: no-repeat repeat; ">&nbsp;
<div class="contenttext" style="font-family: Georgia, serif; font-size: 14px; line-height: 1.6em; width: 540px; text-align: justify; margin-left: 86px; margin-top: 25px; background-color: white; color: #383838; padding-bottom: 20px; ">
<h2 style="font-weight: normal; padding-left: 0px; padding-top: 0px; ">Comments Temporarily Disabled</h2>
<p>I had to temporarily disable comments due to spam. I'll re-enable them soon.</p>
</div>
</div>
<div id="pagecap" style="width: 731px; height: 29px; position: relative; margin-left: 0px; "><img src="http://theocacao.com/images/document/page-bottom.png"  alt="" /></div>
</div>
</div>
<br />
<br />
<br />
<p style="text-align: center; margin-top: 24px; font-size: 13px; "><a href="http://technorati.com/profile/scottstevenson" rel="me" style="text-decoration: none; font-size: 13px; font-style: italic; color: black; ">Technorati Profile</a><br />
Copyright &#169; Scott Stevenson 2004-2008</p>
<p style="text-align: center; margin-top: 24px; font-size: 13px; ">From:&nbsp;http://theocacao.com/document.page/161</p>
</div>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/161300.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2011-12-02 00:05 <a href="http://www.cppblog.com/biao/archive/2011/12/02/161300.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cocoa：NSArray Sorting</title><link>http://www.cppblog.com/biao/archive/2011/12/02/161299.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 01 Dec 2011 16:03:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2011/12/02/161299.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/161299.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2011/12/02/161299.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/161299.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/161299.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: Question:&nbsp;What I want to do seems pretty simple, but I can't find any answers on the web. I have an NSMutableArray of objects, let's say they are 'Person' objects. I want to sort the NSMutable ar...&nbsp;&nbsp;<a href='http://www.cppblog.com/biao/archive/2011/12/02/161299.html'>阅读全文</a><img src ="http://www.cppblog.com/biao/aggbug/161299.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2011-12-02 00:03 <a href="http://www.cppblog.com/biao/archive/2011/12/02/161299.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cocoa：Inline Arrays of NSSortDescriptors</title><link>http://www.cppblog.com/biao/archive/2011/12/02/161298.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 01 Dec 2011 16:01:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2011/12/02/161298.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/161298.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2011/12/02/161298.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/161298.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/161298.html</trackback:ping><description><![CDATA[<div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; background-color: #e4e1d9; ">
<div id="mastercontainer" style="width: 845px; margin-left: auto; margin-right: auto; position: relative; ">
<div id="containerbgleft" style="width: 65px; height: 311px; position: absolute; top: 0px; left: 5px; "><img src="http://theocacao.com/images/document/container-bg-left.png"  alt="" /></div>
<div id="pagemarkleft" style="width: 166px; position: absolute; top: 320px; left: 34px; "><img src="http://theocacao.com/images/document/page-mark-left.png"  alt="" /></div>
<div id="pagemarkright" style="width: 34px; height: 542px; position: absolute; top: 334px; right: 41px; "><img src="http://theocacao.com/images/document/page-mark-right.png"  alt="" /></div>
<div id="contentcontainer" style="width: 731px; margin-left: auto; margin-right: auto; ">
<div id="masthead" style="width: 701px; height: 301px; background-image: url(http://theocacao.com/images/document/bg.jpg); position: relative; margin-left: 12px; margin-top: -22px; background-repeat: no-repeat no-repeat; ">
<div id="rightimage" style="width: 307px; height: 296px; position: absolute; bottom: -20px; right: 0px; border-style: initial; border-color: initial; "><a href="http://theocacao.com/document.page/87#"><img src="http://theocacao.com/images/document/feminine-spirit-and-lion.png" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "  alt="" /></a></div>
<div id="leftimage" style="width: 210px; height: 223px; position: absolute; bottom: 0px; left: -18px; border-style: initial; border-color: initial; "><a href="http://theocacao.com/document.page/87#"><img src="http://theocacao.com/images/shared/standing-globe.png" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; "  alt="" /></a></div>
</div>
<div class="content" style="width: 731px; min-height: 525px; background-image: url(http://theocacao.com/images/document/page-bg.png); padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Georgia, serif; font-size: 14px; color: #383838; line-height: 1.6em; background-repeat: no-repeat repeat; ">&nbsp;
<div class="title" style="width: 731px; right: 18px; text-align: center; "><a href="http://theocacao.com/"><img src="http://theocacao.com/images/shared/Theocacao-Title.png" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; margin-left: -32px; "  alt="" /></a><br />
<div style="width: 526px; margin-left: auto; margin-right: auto; border-left-color: #bdbdbd; padding-top: 8px; padding-right: 8px; padding-bottom: 8px; padding-left: 8px; font-style: italic; margin-top: 20px; "><a href="http://cocoabook.com/" style="color: #802e00; ">Cocoa and Objective-C: Up and Running</a>&nbsp;(by me) is now available from&nbsp;<a href="http://oreilly.com/catalog/9780596804794/" style="color: #802e00; ">O'Reilly</a>.</div>
</div>
<div class="contenttext" style="font-family: Georgia, serif; font-size: 14px; line-height: 1.6em; width: 540px; text-align: justify; margin-left: 86px; margin-top: 25px; background-color: white; color: #383838; padding-bottom: 20px; ">
<h2 style="font-weight: normal; padding-left: 0px; padding-top: 0px; z-index: 10; ">Inline Arrays of NSSortDescriptors</h2>
NSSortDescriptor is Cocoa's standard class for sorting an NSTableView (or NSArray) by multiple criteria. Sort descriptors are created automatically when the user clicks on a column, but there are a number of situations where you need to create them in code. The API is really simple but the process of getting these things into the table is fairly verbose. Here's a clean and simple way to automate the process, using a category with varargs as input:<br />
<br />
<code style="background-color: #f3f3f3; display: block; margin-top: 0.8em; margin-left: 0em; margin-right: -22px; margin-bottom: -20px; padding-top: 1em; padding-right: 0.8em; padding-bottom: 1em; padding-left: 0.8em; font-size: 11.5px; color: black; line-height: 1.6em; font-family: Monaco, monospace; text-shadow: #f3f3f3 0px 0px 1px; text-align: left; "><br />
@interface NSSortDescriptor (CDCExtensions)<br />
<br />
+ (NSArray *) ascendingDescriptorsForKeys: (NSString *)firstKey,...;<br />
<br />
@end<br />
</code><br />
You can use it like this:<br />
<code style="background-color: #f3f3f3; display: block; margin-top: 0.8em; margin-left: 0em; margin-right: -22px; margin-bottom: -20px; padding-top: 1em; padding-right: 0.8em; padding-bottom: 1em; padding-left: 0.8em; font-size: 11.5px; color: black; line-height: 1.6em; font-family: Monaco, monospace; text-shadow: #f3f3f3 0px 0px 1px; text-align: left; "><br />
NSArray *descriptors = [NSSortDescriptor<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ascendingDescriptorsForKeys: @"subject", @"date", nil];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
[tableView setSortDescriptors: descriptors];<br />
</code><br />
Or this:<br />
<code style="background-color: #f3f3f3; display: block; margin-top: 0.8em; margin-left: 0em; margin-right: -22px; margin-bottom: -20px; padding-top: 1em; padding-right: 0.8em; padding-bottom: 1em; padding-left: 0.8em; font-size: 11.5px; color: black; line-height: 1.6em; font-family: Monaco, monospace; text-shadow: #f3f3f3 0px 0px 1px; text-align: left; "><br />
NSArray *descriptors = [NSSortDescriptor<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ascendingDescriptorsForKeys: @"subject", @"date", nil];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
NSArray *sorted = [someArray sortedArrayUsingDescriptors: descriptors];<br />
</code><br />
<br />
<a href="http://theocacao.com/downloads/InlineSortDescriptors.zip" style="color: #802e00; text-decoration: none; font-style: italic; ">Grab zip file</a>&nbsp;with the the .h and .m files. Here's the implementation (formatting adjusted for site):<br />
<code style="background-color: #f3f3f3; display: block; margin-top: 0.8em; margin-left: 0em; margin-right: -22px; margin-bottom: -20px; padding-top: 1em; padding-right: 0.8em; padding-bottom: 1em; padding-left: 0.8em; font-size: 11.5px; color: black; line-height: 1.6em; font-family: Monaco, monospace; text-shadow: #f3f3f3 0px 0px 1px; text-align: left; "><br />
@implementation&nbsp;&nbsp;NSSortDescriptor (CDCAdditions)<br />
<br />
+ (NSArray *) ascendingDescriptorsForKeys: (NSString *)firstKey,...<br />
{&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;id returnArray&nbsp;&nbsp; = [[NSMutableArray arrayWithCapacity: 5] retain];<br />
&nbsp;&nbsp;va_list&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; keyList;<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;NSString&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;* oneKey;<br />
&nbsp;&nbsp;NSSortDescriptor&nbsp;&nbsp;* oneDescriptor;<br />
&nbsp;&nbsp;<br />
&nbsp;&nbsp;if (firstKey)<br />
&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;oneDescriptor = [[NSSortDescriptor alloc] initWithKey: firstKey<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ascending: YES];<br />
&nbsp;&nbsp;&nbsp;&nbsp;[returnArray addObject: oneDescriptor];<br />
&nbsp;&nbsp;&nbsp;&nbsp;[oneDescriptor release];<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;va_start (keyList, firstKey);<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;while (oneKey = va_arg(keyList, NSString *))<br />
&nbsp;&nbsp;&nbsp;&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;oneDescriptor = [[NSSortDescriptor alloc] initWithKey: oneKey<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ascending: YES];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[returnArray addObject: oneDescriptor];<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[oneDescriptor release];<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;va_end (keyList);<br />
&nbsp;&nbsp;}<br />
&nbsp;&nbsp;<br />
&nbsp;&nbsp;return [returnArray autorelease];<br />
&nbsp;&nbsp;<br />
}<br />
<br />
@end<br />
</code>
<div class="dividingimage" style="text-align: center; margin-top: 28px; margin-bottom: 0px; "><img src="http://theocacao.com/images/shared/Divider-Default.png" alt="Design Element" />
<div style="text-align: center; font-style: italic; margin-top: 10px; "><a href="http://theocacao.com/document.page/87" style="color: #585858; text-decoration: none; font-style: italic; ">Inline Arrays of NSSortDescriptors</a><br />
Posted Mar 15, 2005 &#8212; 3 comments below</div>
</div>
</div>
</div>
<div id="pagecap" style="width: 731px; height: 29px; position: relative; margin-left: 0px; "><img src="http://theocacao.com/images/document/page-bottom.png"  alt="" /></div>
<br />
<br />
<br />
<br />
<div id="pagecap" style="width: 731px; height: 38px; position: relative; margin-left: 0px; "><img src="http://theocacao.com/images/document/page-top.png"  alt="" /></div>
<div class="content" style="width: 731px; min-height: 0px; background-image: url(http://theocacao.com/images/document/page-bg.png); padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Georgia, serif; font-size: 14px; color: #383838; line-height: 1.6em; background-repeat: no-repeat repeat; ">&nbsp;
<div class="contenttext" style="font-family: Georgia, serif; font-size: 14px; line-height: 1.6em; width: 540px; text-align: justify; margin-left: 86px; margin-top: 25px; background-color: white; color: #383838; padding-bottom: 20px; ">
<div class="comments">
<div>
<h3 style="font-weight: normal; padding-left: 0px; padding-top: 0px; font-style: italic; padding-bottom: 0px; margin-bottom: 8px; ">Hayssam &#8212; Mar 15, 05&nbsp;<a href="http://theocacao.com/comment/114" style="color: #a4a4a4; text-decoration: none; font-style: italic; font-size: 11px; ">114</a></h3>
</div>
<div class="commentbody" style="font-size: 13px; margin-bottom: 30px; padding-left: 15px; padding-right: 15px; border-left-color: #dedede; ">Hi!<br />
<br />
I think there might be a typo in your&nbsp;<br />
&gt; &nbsp;&nbsp;NSSortDescriptor&nbsp;&nbsp;* descriptor;<br />
as "descriptor" is never used while "OneDescriptor" is used but not declared.<br />
<br />
Thanks for sharing this snippet !</div>
<div>
<h3 style="font-weight: normal; padding-left: 0px; padding-top: 0px; font-style: italic; padding-bottom: 0px; margin-bottom: 8px; "><a href="http://theocacao.com/" style="color: #802e00; text-decoration: none; font-style: italic; ">Scott Stevenson</a>&nbsp;&#8212; Mar 16, 05&nbsp;<a href="http://theocacao.com/comment/115" style="color: #a4a4a4; text-decoration: none; font-style: italic; font-size: 11px; ">115</a>&nbsp;<img src="http://theocacao.com/images/leopard-avatar.png" alt="Scotty the Leopard" width="32" height="30" style="margin-bottom: -8px; margin-left: 8px; " /></h3>
</div>
<div class="commentbody" style="font-size: 13px; margin-bottom: 30px; padding-left: 15px; padding-right: 15px; border-left-color: #dedede; ">Fixed, thanks. I had tweaked a few things to fit the code on the page, and this was an artifact of that. The code in the zip file was fine, though.</div>
<div>
<h3 style="font-weight: normal; padding-left: 0px; padding-top: 0px; font-style: italic; padding-bottom: 0px; margin-bottom: 8px; "><a href="http://theocacao.com/" style="color: #802e00; text-decoration: none; font-style: italic; ">Scott Stevenson</a>&nbsp;&#8212; Mar 16, 05&nbsp;<a href="http://theocacao.com/comment/116" style="color: #a4a4a4; text-decoration: none; font-style: italic; font-size: 11px; ">116</a>&nbsp;<img src="http://theocacao.com/images/leopard-avatar.png" alt="Scotty the Leopard" width="32" height="30" style="margin-bottom: -8px; margin-left: 8px; " /></h3>
</div>
<div class="commentbody" style="font-size: 13px; margin-bottom: 30px; padding-left: 15px; padding-right: 15px; border-left-color: #dedede; "><strong>Update:</strong>&nbsp;I added some discussion about sorting NSArrays.</div>
</div>
<div class="dividingimage" style="text-align: center; margin-top: 28px; margin-bottom: 0px; "><img src="http://theocacao.com/images/shared/Divider-Default.png" alt="Design Element" /><br />
<br />
<a href="feed://theocacao.com/comments.rss" style="color: #802e00; text-decoration: none; font-style: italic; ">Comments Feed</a></div>
</div>
</div>
<div id="pagecap" style="width: 731px; height: 29px; position: relative; margin-left: 0px; "><img src="http://theocacao.com/images/document/page-bottom.png"  alt="" /></div>
<br />
<br />
<br />
<br />
<div id="pagecap" style="width: 731px; height: 38px; position: relative; margin-left: 0px; "><img src="http://theocacao.com/images/document/page-top.png"  alt="" /></div>
<div class="content" style="width: 731px; min-height: 0px; background-image: url(http://theocacao.com/images/document/page-bg.png); padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Georgia, serif; font-size: 14px; color: #383838; line-height: 1.6em; background-repeat: no-repeat repeat; ">&nbsp;
<div class="contenttext" style="font-family: Georgia, serif; font-size: 14px; line-height: 1.6em; width: 540px; text-align: justify; margin-left: 86px; margin-top: 25px; background-color: white; color: #383838; padding-bottom: 20px; ">
<h2 style="font-weight: normal; padding-left: 0px; padding-top: 0px; ">Comments Temporarily Disabled</h2>
<p>I had to temporarily disable comments due to spam. I'll re-enable them soon.</p>
</div>
</div>
<div id="pagecap" style="width: 731px; height: 29px; position: relative; margin-left: 0px; "><img src="http://theocacao.com/images/document/page-bottom.png"  alt="" /></div>
</div>
</div>
<br />
<br />
<br />
<p style="text-align: center; margin-top: 24px; font-size: 13px; "><a href="http://technorati.com/profile/scottstevenson" rel="me" style="text-decoration: none; font-size: 13px; font-style: italic; color: black; ">Technorati Profile</a><br />
Copyright &#169; Scott Stevenson 2004-2008</p>
<p style="text-align: center; margin-top: 24px; font-size: 13px; ">From:&nbsp;http://theocacao.com/document.page/87</p>
</div>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/161298.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2011-12-02 00:01 <a href="http://www.cppblog.com/biao/archive/2011/12/02/161298.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cocoa：variable argument lists</title><link>http://www.cppblog.com/biao/archive/2011/12/02/161297.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Thu, 01 Dec 2011 16:00:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2011/12/02/161297.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/161297.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2011/12/02/161297.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/161297.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/161297.html</trackback:ping><description><![CDATA[<span  style="border-collapse: collapse; color: #545454; line-height: 22px; font-family: ff-tisa-web-pro-1, ff-tisa-web-pro-2, 'Lucida Grande', 'Hiragino Sans GB', 'Hiragino Sans GB W3'; ">
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">STDARG(3)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;BSD Library Functions Manual&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;STDARG(3)</p>
<p class="p2" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; min-height: 14px; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; "><br />
</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">NAME</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;stdarg -- variable argument lists</p>
<p class="p2" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; min-height: 14px; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; "><br />
</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">SYNOPSIS</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;#include &lt;stdarg.h&gt;</p>
<p class="p2" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; min-height: 14px; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; "><br />
</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;void va_start(va_list ap, last);</p>
<p class="p2" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; min-height: 14px; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; "><br />
</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;type va_arg(va_list ap, type);</p>
<p class="p2" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; min-height: 14px; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; "><br />
</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;void va_copy(va_list dest, va_list src);</p>
<p class="p2" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; min-height: 14px; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; "><br />
</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;void va_end(va_list ap);</p>
<p class="p2" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; min-height: 14px; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; "><br />
</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">DESCRIPTION</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;A function may be called with a varying number of arguments of varying types.&nbsp;&nbsp;The include file</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;&lt;stdarg.h&gt; declares a type (va_list) and defines three macros for stepping through a list of arguments</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;whose number and types are not known to the called function.</p>
<p class="p2" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; min-height: 14px; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; "><br />
</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;The called function must declare an object of type va_list which is used by the macros va_start(),</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;va_arg(), va_copy(), and va_end().</p>
<p class="p2" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; min-height: 14px; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; "><br />
</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;The va_start() macro must be called first, and it initializes ap, which can be passed to va_arg() for</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;each argument to be processed.&nbsp;&nbsp;Calling va_end() signals that there are no further arguments, and</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;causes ap to be invalidated.&nbsp;&nbsp;Note that each call to va_start() must be matched by a call to va_end(),</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;from within the same function.</p>
<p class="p2" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; min-height: 14px; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; "><br />
</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;The parameter last is the name of the last parameter before the variable argument list, i.e., the last</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;parameter of which the calling function knows the type.</p>
<p class="p2" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; min-height: 14px; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; "><br />
</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;Because the address of this parameter is used in the va_start() macro, it should not be declared as a</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;register variable, or as a function or an array type.</p>
<p class="p2" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; min-height: 14px; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; "><br />
</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;The va_arg() macro expands to an expression that has the type and value of the next argument in the</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;call.&nbsp;&nbsp;The parameter ap is the va_list ap initialized by va_start().&nbsp;&nbsp;Each call to va_arg() modifies ap</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;so that the next call returns the next argument.&nbsp;&nbsp;The parameter type is a type name specified so that</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;the type of a pointer to an object that has the specified type can be obtained simply by adding a * to</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;type.</p>
<p class="p2" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; min-height: 14px; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; "><br />
</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;If there is no next argument, or if type is not compatible with the type of the actual next argument</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;(as promoted according to the default argument promotions), random errors will occur.</p>
<p class="p2" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; min-height: 14px; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; "><br />
</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;The first use of the va_arg() macro after that of the va_start() macro returns the argument after last.</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;Successive invocations return the values of the remaining arguments.</p>
<p class="p2" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; min-height: 14px; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; "><br />
</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;The va_copy() macro copies the state of the variable argument list, src, previously initialized by</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;va_start(), to the variable argument list, dest, which must not have been previously initialized by</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;va_start(), without an intervening call to va_end().&nbsp;&nbsp;The state preserved in dest is equivalent to</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;calling va_start() and va_arg() on dest in the same way as was used on src.&nbsp;&nbsp;The copied variable argu-</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;ment list can subsequently be passed to va_arg(), and must finally be passed to va_end() when through</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;with it.</p>
<p class="p2" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; min-height: 14px; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; "><br />
</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;After a variable argument list is invalidated by va_end(), it can be reinitialized with va_start() or</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;made a copy of another variable argument list with va_copy().</p>
<p class="p2" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; min-height: 14px; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; "><br />
</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">EXAMPLES</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;The function foo takes a string of format characters and prints out the argument associated with each</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;format character based on the type.</p>
<p class="p2" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; min-height: 14px; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; "><br />
</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;void foo(char *fmt, ...)</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;{</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;va_list ap, ap2;</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;int d;</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;char c, *s;</p>
<p class="p2" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; min-height: 14px; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; "><br />
</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;va_start(ap, fmt);</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;va_copy(ap2, ap);</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;while (*fmt)</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;switch(*fmt++) {</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;case 's':&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;/* string */</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;s = va_arg(ap, char *);</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;printf("string %s\n", s);</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;break;</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;case 'd':&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;/* int */</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;d = va_arg(ap, int);</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;printf("int %d\n", d);</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;break;</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;case 'c':&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;/* char */</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;/* Note: char is promoted to int. */</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;c = va_arg(ap, int);</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;printf("char %c\n", c);</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;break;</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;va_end(ap);</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;...</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;/* use ap2 to iterate over the arguments again */</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;...</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;va_end(ap2);</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;}</p>
<p class="p2" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; min-height: 14px; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; "><br />
</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">COMPATIBILITY</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;These macros are not compatible with the historic macros they replace.&nbsp;&nbsp;A backward compatible version</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;can be found in the include file &lt;varargs.h&gt;.</p>
<p class="p2" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; min-height: 14px; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; "><br />
</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">STANDARDS</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;The va_start(), va_arg(), va_copy(), and va_end() macros conform to ISO/IEC 9899:1999 (``ISO C99'').</p>
<p class="p2" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; min-height: 14px; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; "><br />
</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">BUGS</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;Unlike the varargs macros, the stdarg macros do not permit programmers to code a function with no fixed</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;arguments.&nbsp;&nbsp;This problem generates work mainly when converting varargs code to stdarg code, but it also</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;creates difficulties for variadic functions that wish to pass all of their arguments on to a function</p>
<p class="p1" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font: normal normal normal 12px/normal Helvetica; font-family: 'Hiragino Sans GB'; line-height: normal; font-size: medium; ">&nbsp;&nbsp; &nbsp;&nbsp;that takes a va_list argument, such as vfprintf(3).</p>
</span>
@import url(http://www.cppblog.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);<img src ="http://www.cppblog.com/biao/aggbug/161297.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2011-12-02 00:00 <a href="http://www.cppblog.com/biao/archive/2011/12/02/161297.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Cocoa：Xcode安装离线文档</title><link>http://www.cppblog.com/biao/archive/2011/10/31/159396.html</link><dc:creator>逛奔的蜗牛</dc:creator><author>逛奔的蜗牛</author><pubDate>Mon, 31 Oct 2011 09:19:00 GMT</pubDate><guid>http://www.cppblog.com/biao/archive/2011/10/31/159396.html</guid><wfw:comment>http://www.cppblog.com/biao/comments/159396.html</wfw:comment><comments>http://www.cppblog.com/biao/archive/2011/10/31/159396.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cppblog.com/biao/comments/commentRss/159396.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/biao/services/trackbacks/159396.html</trackback:ping><description><![CDATA[<br />
<span style="color: #545454; font-size: 12px; line-height: 19px; font-family: ff-tisa-web-pro-1, ff-tisa-web-pro-2, 'Lucida Grande', 'Hiragino Sans GB', 'Hiragino Sans GB W3'; ">
<table cellspacing="0" cellpadding="0" id="blogContentTable" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-collapse: collapse; table-layout: fixed; width: 878px; position: relative; ">
    <tbody style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">
        <tr style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">
            <td valign="top" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; ">
            <div id="blogContainer" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; position: relative; overflow-x: hidden; overflow-y: hidden; height: 591px; ">
            <div id="paperTitleArea" align="center" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span id="paperTitle" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; display: block; word-break: break-all; font-weight: bolder; "></span></div>
            <div id="blogDetailDiv" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 14px; ">
            <div class="blog_details_20110920" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><font face="Tahoma" size="3" style="line-height: 1.6; ">10.6:&nbsp;http://developer.apple.com/rss/com.apple.adc.documentation.AppleSnowLeopard.atom</font>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.6; "></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.6; "><font face="Tahoma" size="3">10.7:&nbsp;<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: normal; ">http://developer.apple.com/rss/com.apple.adc.documentation.AppleLion.atom&nbsp;</span></font></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.6; "><font face="Tahoma" size="3"><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: normal; ">iOS 5:&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: normal; ">http://developer.apple.com/rss/com.apple.adc.documentation.AppleiPhone5_0.atom</span></font></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.6; font-size: 14px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: normal; "><font face="Tahoma" size="3"><br />
            </font></span></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.6; font-size: 14px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: normal; "><font face="Tahoma" size="3">legacy: 遗产，祖遗留下来之物</font></span></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 14px; "><font face="Tahoma" size="3"><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: normal; "><br />
            </span></font></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-size: 14px; line-height: 1.6; "><font face="Tahoma" size="3"><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: normal; "><font color="#c00000">Apple publish the Xcode docset through RSS feeds, so Xcode can check and download them easily. If you are interested in retrieving the docsets directly, here is the catch:</font><br />
            </span></font></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.6; font-size: 14px; "><font face="Tahoma" size="3"><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: normal; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Arial, sans-serif; font-size: 13px; "><br />
            </span></span></font></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.6; font-size: 14px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: normal; "><font face="Tahoma" size="3">Xcode 4 的帮助文档都是在线的，如果网速慢，查看起来会非常不方便，我们可以使用下载工具把文档下载到本地硬盘，这样搜索查看时就方便多了</font></span></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.6; font-size: 14px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: normal; "><font face="Tahoma" size="3"><br />
            </font></span></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.6; font-size: 14px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: normal; "><font face="Tahoma" size="3" color="#c00000">安装下载到的atom:</font></span></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.6; font-size: 14px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Tahoma; line-height: normal; font-size: medium; ">0. 找到帮助文档的 Feed 地址</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: normal; "><font face="Tahoma" size="3" color="#c00000"><br />
            </font></span></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.6; font-size: 14px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Tahoma; line-height: normal; font-size: medium; ">在Xcode中 Cmd + , 显示设置窗口，然后定位到 Downloads-&gt;Documentation</span></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.6; font-size: 14px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Tahoma; line-height: normal; font-size: medium; ">点击要使用的文档，如 Mac OS X 10.7 Core Library，再点击左下角的小箭头图标，在显示出来的详细信息中有 Feed 地址</span></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.6; font-size: 14px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Tahoma; line-height: normal; font-size: medium; "><br />
            </span></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.6; font-size: 14px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: normal; "><font face="Tahoma" size="3">1. 解压下载到的 xar 得到 docset</font></span></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.6; font-size: 14px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #0000ff; font-size: 13px; letter-spacing: 2px; line-height: normal; "><font face="Tahoma">sudo xar -xf 文档文件.xar</font></span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: normal; "><font face="Tahoma" size="3"><br />
            </font></span></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.6; font-size: 14px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #0000ff; font-family: Georgia; font-size: 13px; letter-spacing: 2px; line-height: normal; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Tahoma; font-size: medium; color: #000000; letter-spacing: normal; "><br />
            </span></span></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.6; font-size: 14px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #0000ff; font-family: Georgia; font-size: 13px; letter-spacing: 2px; line-height: normal; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Tahoma; font-size: medium; color: #000000; letter-spacing: normal; ">2. 设置 docset 文件的所有者为 devdocs</span><br />
            </span></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.6; font-size: 14px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #0000ff; font-size: 13px; letter-spacing: 2px; line-height: normal; "><font face="Tahoma">sudo chown -R -P devdocs 文档文件.docset</font></span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #0000ff; font-family: Georgia; font-size: 13px; letter-spacing: 2px; line-height: normal; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Tahoma; font-size: medium; color: #000000; letter-spacing: normal; "><br />
            </span></span></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.6; font-size: 14px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #0000ff; font-family: Georgia; font-size: 13px; letter-spacing: 2px; line-height: normal; "><br />
            </span></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.6; font-size: 14px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #0000ff; font-family: Georgia; font-size: 13px; letter-spacing: 2px; line-height: normal; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Tahoma; font-size: medium; color: #000000; letter-spacing: normal; ">3. 复制 devdocs 到</span><br />
            </span></div>
            <div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; line-height: 1.6; font-size: 14px; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; color: #0000ff; font-family: Georgia; font-size: 13px; letter-spacing: 2px; line-height: normal; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Tahoma; font-size: medium; color: #000000; letter-spacing: normal; ">/Developer/Documentation/DocSets/</span></span></div>
            </div>
            </div>
            </div>
            </td>
        </tr>
    </tbody>
</table>
</span><span style="font-family: Georgia; font-size: 12px; letter-spacing: 2px; line-height: 20px; "><font color="#ff0000"><strong>
</strong></font></span><img src ="http://www.cppblog.com/biao/aggbug/159396.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/biao/" target="_blank">逛奔的蜗牛</a> 2011-10-31 17:19 <a href="http://www.cppblog.com/biao/archive/2011/10/31/159396.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>