关于alloc

一、各种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.

enter image description here

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] vs[ NSObject new]
转播: http://developerconceptions.wordpress.com/2011/04/26/nsobject-alloc-init-vs-nsobject-new/ ;

[[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]

”’Edit”’: According to [ http://thom.org.uk/ Thom Lawrence] in the comments, that may not actually be the case. It’s not a  [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 initWithParamwhere 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





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值