OC 构造函数 alloc init

来源:互联网 发布:哈希姆家族 知乎 编辑:程序博客网 时间:2024/06/05 15:42


////  Person.h//  构造方法////  Created by LiuWei on 15/4/15.//  Copyright (c) 2015年 LiuWei. All rights reserved.//#import <Foundation/Foundation.h>@interface Person : NSObject{    int _age;}@property int age;@end

////  Person.m//  构造方法////  Created by LiuWei on 15/4/15.//  Copyright (c) 2015年 LiuWei. All rights reserved.//#import "Person.h"@implementation Person// 重写init方法- (id)init{    // 一定要先调用父类的init方法, 以完成从父类继承的成员变量初始化    self = [super init]; // 返回当前对象 self        if (self != nil)    {        // self不为nil则说明分配空间成功, 所以可以继续进行成员变量的初始化操作                // 初始化成员变量_age        _age = 10;            }        // 返回self    return self;}@end

////  main.m//  构造方法////  Created by LiuWei on 15/4/15.//  Copyright (c) 2015年 LiuWei. All rights reserved.//#import <Foundation/Foundation.h>#import "Person.h"int main(int argc, const char * argv[]) {         Person *per = [Person new];    // new方法 调用了alloc类方法和init对象方法来申请空间并初始化对象    // + alloc 分配空间, 初始化isa指向数据结构, 其它成员变量初始化为0    // - init 初始化对象成员变量    // Person *per = [Person alloc];    // per = [per init];    // 等同 Person *per = [[Person alloc] init];        Person * p = [[Person alloc] init];        NSLog(@"per.age = %i", per.age); // _age在构造方法中初始化为10, 所以这里输出结果为10        NSLog(@"p.age = %i", p.age); // _age在构造方法中初始化为10, 所以这里输出结果为10        return 0;}





+ alloc

Returns a new instance of the receiving class.  // 返回接收类类型的一个新实例

Declaration // 方法声明

OBJECTIVE-C

+ (instancetype)alloc

Return Value

A new instance of the receiver.  // 返回接收类类型的一个新实例


Discussion // 说明

The isa instance variable of the new instance is initialized to a data structure that describes the class; memory for all other instance variables is set to0.

You must use an init... method to complete the initialization process. For example:

// 新实例的实例变量isa初始化为指向类描述信息的结构体, 所有其它实例变量都设置为0. 你必须使用init方法来完成初始化过程 , 示例如下

  • TheClass *newObject = [[TheClassalloc] init];


Do not override alloc to include initialization code. Instead, implement class-specific versions of init... methods.

// 不可重写alloc方法来包含初始化代码, 取而代之,应当实现类特有的版本方法 init

For historical reasons, alloc invokes allocWithZone:.

// 由于历史原因, alloc 调用了allocWIthZone:






- init

Implemented by subclasses to initialize a new object (the receiver) immediately after memory for it has been allocated.

//  通过子类实现的, 在内存分配成功后,立即调用以

Declaration // 声明 

OBJECTIVE-C

- (instancetype)init

Return Value // 返回值

An initialized object, or nil if an object could not be created for some reason that would not result in an exception.

// 返回值为一个初始化过的对象, 或者由于某些原因不能创建一个对象则返回nil

Discussion

An init message is coupled with an alloc (orallocWithZone:) message in the same line of code:

// 在一行代码中, init 通常与alloc成对出现

TheClass *newObject = [[TheClass alloc] init];

An object isn’t ready to be used until it has been initialized. The init method defined in theNSObject class does no initialization; it simply returns self.

// 一个对象在没有初始化后,是不能被使用的. 在NSObject 类中定义的init方法并没有进行初始化操作, 它只是简单的返回self

In a custom implementation of this method, you must invoke super’s designated initializer then initialize and return the new object. If the new object can’t be initialized, the method should return nil. For example, a hypotheticalBuilt In Camera class might returnnil from its init method if run on a device that has no camera.

// 在这个方法正确的实现中, 你必须调用 父类指定的初始化器来初始化并返回这个新对象. 如果这个新对象不能被初始化. init方法应当返回nil.  例如, 假设构建了一个摄像头类, 而这个类代码运行在一个没有摄像头的设备中时, 那么这个类的init方法就应当返回nil.

- (id)init {    self = [super init];    if (self) {        // Initialize self.    }    return self;}


In some cases, an init method might return a substitute object. You must therefore always use the object returned by init, and not the one returned by alloc or allocWithZone:, in subsequent code.


// 在某些情况下, init方法可能返回一个替代对象. 因此 在后续的代码中, 你必须使用通过init返回的对象, 而不是alloc或allocWithZone返回的那一个.


0 0
原创粉丝点击