DFdou's Blog Life is short,Be yourself.

3107/090

Objc – Memory Management基本内容管理

基于C的东西似乎都要这样,要手动去管理内存,Objc也是如此,本人新手,弄一段别人写的指南.
Learned from http://cocoadevcentral.com/d/learn_objectivec/:
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.
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.
learnobjectivec-referencecounting
That's the theory of reference counting. But in practice, there are usually only two reasons to create an object:
一般只有2个原因才会去建立一个Object
1. To keep it as an instance variable //作为一个实例变量
2. To use temporarily for single use inside a function //在一个函数里临时使用

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.
大部分情况下,set一个实例变量的时候我们需要autorelease原来的Object,并且retain新的值。
然后你只需要在dealloc函数里release掉就可以了。

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.
所以呢,实际上要做的只是管理函数中的引用。只有一个规则,如果你用alloc或者copy方法建立了一个Object,在函数结尾使用release或者autorelease就可以了。
如果你是用的别的方法建立的,那更简单,什么都不用做~Yes!Just do nothing。
哇靠,真累,不翻译了……反正翻译的也不准,,~
Here's the first case, managing an instance variable:

- (void) setTotalAmount: (NSNumber*)input {
[totalAmount autorelease];
totalAmount = [input retain];
}
- (void) dealloc {
[totalAmount release];
[super dealloc];
}

Here's the other case, local references. We only need to release the object created with alloc:

NSNumber* value1 = [[NSNumber alloc] initWithFloat:8.75];
NSNumber* value2 = [NSNumber numberWithFloat:14.78];
// only release value1, not value2 [value1 release];

And here's a combo: using a local reference to set an object as an instance variable:

NSNumber* value1 = [[NSNumber alloc] initWithFloat:8.75];
[self setTotal:value1];
NSNumber* value2 = [NSNumber numberWithFloat:14.78];
[self setTotal:value2];
[value1 release];

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.

If you understand this, you understand 90% of what you will ever need to know about Objective-C memory management.

Tagged as: , No Comments
3007/090

Objc-Special Type In Objc

唉,完蛋了,最近忙的晕头转向,都没时间去想找什么好玩的东西放Blog上了……OK,OK,就转载个Objc的教材嘛。
原文作者为Greg Miller,文章地址在:http://unixjunkie.blogspot.com/2006/02/nil-and-nil.html
一下内容来自:http://cocoachina.com/newbie/basic/2009/0611/170.html
Objective-C中有一些很有趣的数据类型经常会被错误地理解。他们中的大多数都可以在/usr/include/objc/objc.h或者这个目录中的其他头文件中找到。下面是从objc.h中摘录的一段,定义了一些数据类型:

// 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
Tagged as: , Continue reading
207/090

AFK Some Days–

iphoneappicon
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.
:wuwu