码迷,mamicode.com
首页 > 其他好文 > 详细

【OC语法快览】三、创建实例对象

时间:2014-07-06 00:53:56      阅读:211      评论:0      收藏:0      [点我收藏+]

标签:oc   tutorial   对象   cocoa   

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 = [[NSNumber alloc] initWithFloat:1.0];
原文:learn_objective_C part 3

【OC语法快览】三、创建实例对象,布布扣,bubuko.com

【OC语法快览】三、创建实例对象

标签:oc   tutorial   对象   cocoa   

原文地址:http://blog.csdn.net/gogler/article/details/36870015

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!