首页 > 代码库 > 【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