在object-c基础教程这本书里老是可以看见类似下面这种代码,我这小菜鸟就纳闷了,啥区别啊,看上去都一样么。网上找了找,找到一些说法,这里先摘出来。
someClass* object = [someClass new];
or
someClass* object = [[someClass alloc] init];
其实是一样的,某位老兄是这样说的,new在内部调用的alloc和init,
Actually "new" is not a keyword in Objective-C, but NSObject implements a class method "new" which simply calls "alloc" and "init".
既然一样干嘛有两个,吃饱了那个啥来着。。
The new method actually comes from NeXT days. Back then, there was no two phase initialization, it was just a single new method. They soon realized that a two phase approach could have advantages, and introduced alloc. new was kinda deprecated, but kept in for backwards compatibility. It is exactly the same as alloc-init. Use 'new' if it suits. One shortcoming is that it only works with the basic 'init' initializer, and will not work with other initializers (eg initWithString:).
背景说明,new是较为老式的写法,后来发现只有一个new不好使,才引入了alloc和init这种写法,保留new一是向后兼容,二是很多时候是一种更简单的写法。至于alloc这种写法可以变出这样的花来,
Frequently, you are going to need to pass arguments to init and so you will be using a different method, such as [[SomeObject alloc] initWithString: @"Foo"]. If you're used to writing this, you get in the habit of doing it this way and so [[SomeObject alloc] init] may come more naturally that [SomeObject new].
恩,确实如果不需要用其他的init函数,比如initWithString, 用new的方法,毫无疑问是更加方便的。
再来个据说是来源比较靠谱的说法:
There was a very long thread on this same subject (alloc/init vs. new) on the cocoa-dev mailing list this week (search for "[Foo new] vs [[Foo alloc] init]"). Unfortunately the documentation is not crystal clear on this, but Bill Bumgarner (an Apple Engineer) confirmed that new is implemented as allocWithZone/alloc followed by init back at least to the beginning of OS X. So the answer is, use whichever you prefer. The current vogue in Cocoa programming is to use alloc/init because it makes the intended behavior explicit.
好吧,似乎只有显示调用更加容易理解这一个区别了,总之一句话,爱谁谁。
这位兄弟来了个总结性发言,姑且咱先信了他,
* new doesn't support custom initializers (like initWithString)
* alloc-init is more explicit than new
General opinion seems to be that you should use whatever you're comfortable with.
以上的说法都来自http://macresearch.org/difference-between-alloc-init-and-new , 有兴趣的看看把。
分享到:
相关推荐
pcf8563_i2c1_r8_ruoge_ov2640通过给RTC驱动增加设备节点读取秒钟成功+直接读取I2C1获取秒钟值20160626_2201.7z http://blog.csdn.net/21cnbao/article/details/7919055 在Android源码树中添加userspace I2C读写...
c++智能指针源码分析:文件memory之类-Ref-count-resource-alloc
用于Grand Central Dispatch的Objective-C包装器,具有用于每个单个调度调用的方法以及一些有用的补充。 欢迎以请求请求,功能请求或问题的形式进行贡献! 特别建议使用改进的调度源,调度IO和调度数据API。 加法 ...
IOS 中 new 和 alloc init 的对比 1.在实际开发中很少会用到new,一般创建对象咱们看到的全...id newObject = (*_alloc)((Class)self, 0); Class metaClass = self->isa; if (class_getVersion(metaClass) > 1) return
g++ -c -pipe -fPIC -fpermissive -finline-functions -Wno-long-long -O2 -std=gnu++1y -fvisibility=hidden -fvisibility-inlines-hidden -Wall -W -D_REENTRANT -fPIC -DQTCSSH_LIBRARY -D'RELATIVE_PLUGIN_PATH=...
MN10300 Dynamic DMA mapping support.
7天的API包装器死亡来自Alloc的fixs模式的Web API的简单包装。要求运行Alloc修复程序的7天死服务器该模块所需的端口与动态映射页面相同。 有关更多信息,请参见mod本身的文档用法示例使用npm npm i 7daystodie-api-...
#include "sceneGraph/sceneObject.h" #include "collision/convex.h" #include "collision/gjk.h" //---------------------------------------------------------------------------- static F32 rel_error = 1E...
Happy DNS for Objective-C用途调用系统底层Dns解析库,可以使用114 等第三方dns解析,也可以集成dnspod等httpdns。另外也有丰富的hosts 域名配置。安装通过CocoaPodspod "HappyDNS"运行环境使用方法* 返回IP列表 ...
_ ###易于使用 // Get startedSCLAlertView *alert = [[SCLAlertView alloc ] init ];[alert showSuccess: self title: @" Hello World " subTitle: @" This is a more descriptive text. " closeButtonTitle: @...
consistent DMA memory allocation
KO2Abc* b = [[KO2Abc alloc] init]; KO2Abc* c = [KO2Abc allocWithZone:nil]; KO2Abc* d = [KO2Abc new]; KO2Abc* e = [[KO2Abc alloc] copy]; KO2Abc* f = [[KO2Abc alloc] mutableCopy]; XCTAssertEqualObjects...
部分代码UIImageView *bgImage=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; bgImage.image=[UIImage imageNamed:@"bg.png"]; bgImage....
钩住所有对象中的alloc和dealloc,以查看底层系统的工作方式。 特别是用于查找由苹果引起的错误。 只需调用beginAllocAndDeallocMonitor即可开始: [ NSObject beginAllocAndDeallocMonitor ]; 然后观看控制台: ...
Object C 内存管理课件,关于一些内存管理机制 还有手动内存 引用计数操作的一些 小例子
在GameViewController.m中,它的-viewDidLoad,迷宫的高度和宽度由[[MazeGenerator alloc] initMazeWithWidth:mazeWidth height:mazeHeight]设置; 在应用程序启动时,默认情况下会随机生成一个 10x10 的迷宫。 它...
#import "TextfieldValidation.h" #import "PaySlipCalculator.h" TextfieldValidation *TextfieldValidator = [[TextfieldValidation alloc] init]; 如果您的项目需要工资单和税收计算,请将 PaySlipCalculator.h ...
[map setObject:@"c" forKey:@"content"]; 或者 NSMutableArray *list = [[NSMutableArray alloc]init]; NSMutableDictionary *map1 = [[NSMutableDictionary alloc]init]; [map1 setObject:@"a1" forKey:@...
薛定inger的猫Schrodinger'sCat是Shellcode防病毒规避框架技术原理请访问我的博客如何去美国 └─$ python3 Schrodingerscat.py -f 64.bin -t tpl_nttestalert -a alloc_calloc -p x64 ____ _ _ _ _ ____ _ / ___| _...