首页 > 代码库 > 【OC语法快览】三、创建实例对象
【OC语法快览】三、创建实例对象
Creating Objects
创建对象
There are two main ways to create an object. The first is the one you saw before:
创建对象主要有两种方法。第一种如下:
NSString* myString = [NSString string];
This is the more convenient automatic style. In this case, you are creating an autoreleased object, which we‘ll look at in more detail later. In many cases, though, you need to create an object using themanual style:
上面这种是比较方便自动的方式。这种情况下,创建了一个自动释放的对象,接下来我们会探究其更多细节。在更多的情况下,你需要使用人工方式来创建对象:
NSString* myString = [[NSStringalloc]init];
This is a nested method call. The first is the alloc method called on NSString itself. This is a relatively low-level call which reserves memory and instantiates an object.
这是一种嵌套方法调用。首先是NSString调用alloc方法。这是一种相对底层的方法调用,用来获得内存和初始实例化对象。
The second piece is a call to init on the new object. The init implementation usually does basic setup, such as creating instance variables. The details of that are unknown to you as a client of the class.
第二个调用的是新实例对象的init方法。Init方法通常实现一些基本的启动动作,比如创建实例变量。Init方法的细节对调用者是透明的。
In some cases, you may use a different version of init which takes input:
在某些情况下,你可能使用新版本的init方法——带有输入参数。
NSNumber* value = http://www.mamicode.com/[[NSNumber alloc] initWithFloat:1.0];
原文:learn_objective_C part 3
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。