<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>DFdou&#039;s Blog &#187; ObjC</title>
	<atom:link href="http://nwhy.org/tag/objc/feed" rel="self" type="application/rss+xml" />
	<link>http://nwhy.org</link>
	<description>Life is short,Be yourself.</description>
	<lastBuildDate>Thu, 29 Jul 2010 02:10:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Objc &#8211; Memory Management基本内容管理</title>
		<link>http://nwhy.org/objc-memory-management.html</link>
		<comments>http://nwhy.org/objc-memory-management.html#comments</comments>
		<pubDate>Thu, 30 Jul 2009 18:11:08 +0000</pubDate>
		<dc:creator>DFdou</dc:creator>
				<category><![CDATA[Others]]></category>
		<category><![CDATA[alloc]]></category>
		<category><![CDATA[ObjC]]></category>

		<guid isPermaLink="false">http://nwhy.org/?p=4677</guid>
		<description><![CDATA[基于C的东西似乎都要这样,要手动去管理内存,Objc也是如此,本人新手,弄一段别人写的指南. Learned from http://cocoadevcentral.com/d/learn_objectivec/: Objective-C's memory management system is called reference counting. All y... ]]></description>
			<content:encoded><![CDATA[<p>基于C的东西似乎都要这样,要手动去管理内存,Objc也是如此,本人新手,弄一段别人写的指南.<br />
Learned from <a  href="http://cocoadevcentral.com/d/learn_objectivec/">http://cocoadevcentral.com/d/learn_objectivec/</a>:<br />
Objective-C's memory management system is called reference counting. All you have to do is keep track of your references, and the runtime does the actual freeing of memory.<br />
In simplest terms, you alloc an object, maybe retain it at some point, then send one release for each alloc/retain you sent. So if you used alloc once and then retain once, you need to release twice.<br />
<img src="http://cocoadevcentral.com/images/articles/000094/learnobjectivec-referencecounting.png" alt="learnobjectivec-referencecounting" /><br />
That's the theory of reference counting. But in practice, there are usually only two reasons to create an object:<br />
一般只有2个原因才会去建立一个Object<br />
1. To keep it as an instance variable //作为一个实例变量<br />
2. To use temporarily for single use inside a function //在一个函数里临时使用</p>
<p>In most cases, the setter for an instance variable should just autorelease the old object, and retain the new one. You then just make sure to release it in dealloc as well.<br />
大部分情况下，set一个实例变量的时候我们需要autorelease原来的Object，并且retain新的值。<br />
然后你只需要在dealloc函数里release掉就可以了。</p>
<p>So the only real work is managing local references inside a function. And there's only one rule: if you create an object with alloc or copy, send it a release or autorelease message at the end of the function. If you create an object any other way, do nothing.<br />
所以呢，实际上要做的只是管理函数中的引用。只有一个规则，如果你用alloc或者copy方法建立了一个Object，在函数结尾使用release或者autorelease就可以了。<br />
如果你是用的别的方法建立的，那更简单，什么都不用做～Yes！Just do nothing。<br />
哇靠，真累，不翻译了……反正翻译的也不准，，～<br />
Here's the first case, managing an instance variable: </p>
<pre class="brush: c">
- (void) setTotalAmount: (NSNumber*)input {
[totalAmount autorelease];
totalAmount = [input retain];
}
- (void) dealloc {
[totalAmount release];
[super dealloc];
}
</pre>
<p>Here's the other case, local references. We only need to release the object created with alloc: </p>
<pre class="brush: c">
NSNumber* value1 = [[NSNumber alloc] initWithFloat:8.75];
NSNumber* value2 = [NSNumber numberWithFloat:14.78];
// only release value1, not value2 [value1 release];
</pre>
<p>And here's a combo: using a local reference to set an object as an instance variable: </p>
<pre class="brush: c">
NSNumber* value1 = [[NSNumber alloc] initWithFloat:8.75];
[self setTotal:value1];
NSNumber* value2 = [NSNumber numberWithFloat:14.78];
[self setTotal:value2];
[value1 release];
</pre>
<p>Notice how the rules for managing local references are exactly the same, regardless of whether you're setting them as instance variables or not. You don't need to think about how the setters are implemented.</p>
<p>If you understand this, you understand 90% of what you will ever need to know about Objective-C memory management. </p>
<h4  class="related_post_title">Some Related Posts</h4><ul class="related_post"><li>2009/07/30 -- <a  href="http://nwhy.org/objc-special-type-in-objc.html" title="Objc-Special Type In Objc">Objc-Special Type In Objc</a> (0)</li><li>2009/07/02 -- <a  href="http://nwhy.org/afk-some-days.html" title="AFK Some Days&#8211;">AFK Some Days&#8211;</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://nwhy.org/objc-memory-management.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Objc-Special Type In Objc</title>
		<link>http://nwhy.org/objc-special-type-in-objc.html</link>
		<comments>http://nwhy.org/objc-special-type-in-objc.html#comments</comments>
		<pubDate>Thu, 30 Jul 2009 02:10:45 +0000</pubDate>
		<dc:creator>DFdou</dc:creator>
				<category><![CDATA[Others]]></category>
		<category><![CDATA[ObjC]]></category>
		<category><![CDATA[Type]]></category>

		<guid isPermaLink="false">http://nwhy.org/?p=4665</guid>
		<description><![CDATA[唉，完蛋了，最近忙的晕头转向，都没时间去想找什么好玩的东西放Blog上了……OK,OK,就转载个Objc的教材嘛。 原文作者为Greg Miller，文章地址在：http://unixjunkie.blogspot.com/2006/02/nil-and-nil.html 一下... ]]></description>
			<content:encoded><![CDATA[<p>唉，完蛋了，最近忙的晕头转向，都没时间去想找什么好玩的东西放Blog上了……OK,OK,就转载个Objc的教材嘛。<br />
原文作者为Greg Miller，文章地址在：<a  href="http://unixjunkie.blogspot.com/2006/02/nil-and-nil.html">http://unixjunkie.blogspot.com/2006/02/nil-and-nil.html</a><br />
一下内容来自：<a  href="http://cocoachina.com/newbie/basic/2009/0611/170.html">http://cocoachina.com/newbie/basic/2009/0611/170.html</a><br />
Objective-C中有一些很有趣的数据类型经常会被错误地理解。他们中的大多数都可以在/usr/include/objc/objc.h或者这个目录中的其他头文件中找到。下面是从objc.h中摘录的一段，定义了一些数据类型：</p>
<pre class="brush: c">
// objc.h
typedef struct objc_class *Class;
typedef struct objc_object {
        Class isa;
} *id;
typedef struct objc_selector  *SEL;
typedef id      (*IMP)(id, SEL, …);
typedef signed char   BOOL;
#define YES             (BOOL)1
#define NO              (BOOL)0
#ifndef Nil
#define Nil 0   /* id of Nil class */
#endif
#ifndef nil
#define nil 0   /* id of Nil instance */
#endif
</pre>
<p><span id="more-4665"></span></p>
<h3>id</h3>
<p>id 和void *并非完全一样。在上面的代码中，id是指向struct objc_object的一个指针，这个意思基本上是说，id是一个指向任何一个继承了Object（或者NSObject）类的对象。需要注意的是id 是一个指针，所以你在使用id的时候不需要加星号。比如id foo=nil定义了一个nil指针，这个指针指向NSObject的一个任意子类。而id *foo=nil则定义了一个指针，这个指针指向另一个指针，被指向的这个指针指向NSObject的一个子类。</p>
<h3>nil</h3>
<p>nil和C语言的NULL相同，在objc/objc.h中定义。nil表示一个Objctive-C对象，这个对象的指针指向空（没有东西就是空）。</p>
<h3>Nil</h3>
<p>首字母大写的Nil和nil有一点不一样，Nil定义一个指向空的类（是Class，而不是对象）。</p>
<h3>SEL</h3>
<p>这个很有趣。SEL是“selector”的一个类型，表示一个方法的名字。比如以下方法：<br />
-[Foo count] 和 -[Bar count] 使用同一个selector，它们的selector叫做count。<br />
在上面的头文件里我们看到，SEL是指向 struct objc_selector的指针，但是objc_selector是什么呢？那么实际上，你使用GNU Objective-C的运行时间库和NeXT Objective-C的运行运行时间库（Mac OS X使用NeXT的运行时间库）时,它们的定义是不一样的。实际上Mac OSX仅仅将SEL映射为C字符串。比如，我们定义一个Foo的类，这个类带有一个- (int) blah方法，那么以下代码：</p>
<pre class="brush: c">
NSLog (@&quot;SEL=%s&quot;, @selector(blah));
</pre>
<p>会输出为 SEL=blah。<br />
说白了SEL就是返回方法名。</p>
<h4  class="related_post_title">Some Related Posts</h4><ul class="related_post"><li>2009/07/31 -- <a  href="http://nwhy.org/objc-memory-management.html" title="Objc &#8211; Memory Management基本内容管理">Objc &#8211; Memory Management基本内容管理</a> (0)</li><li>2009/07/02 -- <a  href="http://nwhy.org/afk-some-days.html" title="AFK Some Days&#8211;">AFK Some Days&#8211;</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://nwhy.org/objc-special-type-in-objc.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AFK Some Days&#8211;</title>
		<link>http://nwhy.org/afk-some-days.html</link>
		<comments>http://nwhy.org/afk-some-days.html#comments</comments>
		<pubDate>Thu, 02 Jul 2009 12:13:56 +0000</pubDate>
		<dc:creator>DFdou</dc:creator>
				<category><![CDATA[Others]]></category>
		<category><![CDATA[AFK]]></category>
		<category><![CDATA[ObjC]]></category>
		<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://nwhy.org/?p=4542</guid>
		<description><![CDATA[Beacuse those days,my work was changed to Objective-C,I has to spend more time to study,,sooooo.... AFK for some days,I will come back when it's not so busy yet. Some Related Posts2009/08/14 -- NSMutableArray,NSArray (0)2009/07/31 -- Objc &#8211; Memory ... ]]></description>
			<content:encoded><![CDATA[<p><img src="http://lh6.ggpht.com/_YE8sr7aRsWc/SkyjgcH81BI/AAAAAAAAB24/DP9sCOxShlg/s640/iphoneappicon.png" alt="iphoneappicon" /><br />
Beacuse those days,my work was changed to Objective-C,I has to spend more time to study,,sooooo....<br />
AFK for some days,I will come back when it's not so busy yet.<br />
 <img src='http://nwhy.org/wp-includes/images/smilies/21.GIF' alt=':wuwu' class='wp-smiley' /> </p>
<h4  class="related_post_title">Some Related Posts</h4><ul class="related_post"><li>2009/08/14 -- <a  href="http://nwhy.org/objc-nsmutablearray-nsarray.html" title="NSMutableArray,NSArray">NSMutableArray,NSArray</a> (0)</li><li>2009/07/31 -- <a  href="http://nwhy.org/objc-memory-management.html" title="Objc &#8211; Memory Management基本内容管理">Objc &#8211; Memory Management基本内容管理</a> (0)</li><li>2009/07/30 -- <a  href="http://nwhy.org/objc-special-type-in-objc.html" title="Objc-Special Type In Objc">Objc-Special Type In Objc</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://nwhy.org/afk-some-days.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
