一、各种alloc方法的比较
1、alloc:
2、allocWithZone:
(1)alloc与 allocWithZone的区别:
When one object creates another, it’s sometimes a good idea to make sure they’re both allocated from the same region of memory. The zone method (declared in the NSObject protocol) can be used for this purpose; it returns the zone where the receiver is located.
3、NSAllocateObject:
(1) NSAllocateObject与allocWithZone的区别:
被分配的类可能是nsobject,也可能是root class(例如: NSProxy ), 所以直接使用NSAllocateObject。 iOS下的root class就是NSObject, NSObject 就是root class。
这里有详细的解释: http://stackoverflow.com/questions/6203467/default-base-class-for-objective-c-classes 。
Q: Do all objective c classes share a common default base class when a base class is not explicity defined in the class definition?
A:No, if you do not explicitly define a super class in the class definition you are creating a root class.
From Cocoa Core Competencies:
A root class inherits from no other class and defines an interface and behavior common to all objects in the hierarchy below it. All objects in that hierarchy ultimately inherit from the root class. A root class is sometimes referred to as a base class.
The root class of all Objective-C classes is NSObject
, which is part of the Foundation framework. All objects in a Cocoa or Cocoa Touch application ultimately inherit from NSObject. This class is the primary access point whereby other classes interact with the Objective-C runtime. It also declares the fundamental object interface and implements basic object behavior, including introspection, memory management, and method invocation. Cocoa and Cocoa Touch objects derive the ability to behave as objects in large part from the root class.
The Foundation framework defines another root class, NSProxy
, but this class is rarely used in Cocoa applications and never in Cocoa Touch applications.
[[NSObject alloc] init]
seems a bit long for me. After all, why call two methods when you could just call one:
[NSObject new]
. But a bit further into the matter reveals that “In some classes,
new
methods manage a set of instances…” In other words, Apple added a layer of confusion by extending the
new
method to do things the
new
method really shouldn’t do.
The skinny is, use [[NSObject alloc] init]
, not [NSObject new]
because it’s an Apple coding convention and because [NSObject new]
might not do what you would expect it to do.
[http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/occ/clm/NSObject/new reading]
References
* [http://macresearch.org/difference-between-alloc-init-and-new difference between alloc init and new]
* [http://stackoverflow.com/questions/719877/use-of-alloc-init-instead-of-new-objective-c use of alloc init instead of new objective c]
[NSObject new]
that’s doing strange things but
[NSObject newMyClassWithTag]
.
The going theory supporting [[Foo alloc] init]
usage is that there are generally more init...
methods than new...
methods. To create a new class in some places you could simply use [Bar new]
, but in other places you must use[[Foo alloc] initWithParam:param]
because you need initWithParam
where no newWithParam
exists. To keep consistency within your own code you might as well always use [[Foo alloc] init]
. Thanks for the update [http://thom.org.uk/ Thom].
div.edit
{
margin:2px;
padding:4px;
border:1px solid gray;
height:auto;
width:auto;
float:left;
}
三、[[Class alloc] init] vs [[[self class] alloc] init] vs [[self alloc] init]
(本人英文不好,所以将英文原文贴在下面)
一般情况下,在类的方法里,你可以直接使用 [[self alloc] init]。例如,为类写一个方法的推荐方式是
+ (id)fooWithBar:(Bar *)aBar
{
return [[[self alloc] initWithBar:aBar] autorelease];
}
(注意 在类的方法里,self指向class object。)
然而,你也可以使用 [[Foo alloc] init](Foo是某个类的确切的名字),如果你想确切的得到Foo类的实例(而不是subclass)。
我解释一下类的方法:
Objective-C中的方法有两种:类方法(class method)和实例方法(instance method)。类方法被限定在类范围内,不能被类的实例调用(即脱离实例运行)。alloc就是一种类方法。实例方法限定在对象实例的范围内(即实例化之前不能运行)。init就是一种实例方法,被alloc方法返回的对象实例调用。
instance method 以减号 "-" 开头
class method 以加号 “+” 开头,相当于static方法
英文原文:
Generally, within a class method, you do use [[self alloc] init]. For example, the canonical way to write a convenience method for a class is:
+ (id)fooWithBar:(Bar *)aBar
{
return [[[self alloc] initWithBar:aBar] autorelease];
}
(Note that in a class method, self refers to the class object.)
However, you would use [[Foo alloc] init] (that is, an explicit class name) if you actually want an instance of the Foo class (and not a subclass).
原文地址:
http://stackoverflow.com/questions/3559763/why-use-classname-alloc-instead-of-self-class-alloc