Cocoa Core Competencies 的Object creation 和IOS面试题示例:写一个NSString类的实现
2017-06-12 18:22
1221 查看
object creation:
An object comes into runtime existence through a two-step process that allocates memory for the object and sets its state to reasonable initial values. To allocate an Objective-C object,
send an
instance of the class. It also sets a pointer (known as the
1.
After you allocate an object, you must initialize it. Initialization sets the instance variables of an object to reasonable initial values. It can also allocate and prepare other global resources needed by the object. You initialize an object by invoking an
or some other method whose name begins with
methods succeed in initializing an object, they return it; otherwise, they return
invokes the initializer of the nearest ancestor instead.
意思是:建立对象需要两个步骤1分配内存 2初始化
1分配内存,要sent
to the object’s class. 也就是常见的[Class alloc].或是不常见的[Class allocWithZone]
2初始化.要调用init的方法进行初始化,各种各样的要或不要参数的init方法都算.
以上建立对象的方式,返回值要送进 对象自动管理池
iOS面试题示例:写一个NSString类的实现
+ (id)initWithCString:(const char *)nullTerminatedCString encoding:(NSStringEncoding)encoding;
+ (id) stringWithCString: (const char*)nullTerminatedCString
encoding: (NSStringEncoding)encoding
{
NSString *obj;
obj = [self allocWithZone: NSDefaultMallocZone()];
obj = [obj initWithCString: nullTerminatedCString encoding: encoding];
return AUTORELEASE(obj);
}
The Form of an Object-Creation Expression
A convention in Cocoa programming is to nest the allocation call inside the initialization call.
An object comes into runtime existence through a two-step process that allocates memory for the object and sets its state to reasonable initial values. To allocate an Objective-C object,
send an
allocor
allocWithZone:message to the object’s class. The runtime allocates memory for the object and returns a “raw” (uninitialized)
instance of the class. It also sets a pointer (known as the
isapointer) to the object’s class, zeros out all instance variables to appropriately typed values, and sets the object’s retain count to
1.
After you allocate an object, you must initialize it. Initialization sets the instance variables of an object to reasonable initial values. It can also allocate and prepare other global resources needed by the object. You initialize an object by invoking an
initmethod
or some other method whose name begins with
init. These initializer methods often have one or more parameters that enable you to specify beginning values of an object’s instance variables. If these
methods succeed in initializing an object, they return it; otherwise, they return
nil. If an object’s class does not implement an initializer, the objective-c runtime
invokes the initializer of the nearest ancestor instead.
意思是:建立对象需要两个步骤1分配内存 2初始化
1分配内存,要sent
allocor
allocWithZone:message
to the object’s class. 也就是常见的[Class alloc].或是不常见的[Class allocWithZone]
2初始化.要调用init的方法进行初始化,各种各样的要或不要参数的init方法都算.
以上建立对象的方式,返回值要送进 对象自动管理池
iOS面试题示例:写一个NSString类的实现
+ (id)initWithCString:(const char *)nullTerminatedCString encoding:(NSStringEncoding)encoding;
+ (id) stringWithCString: (const char*)nullTerminatedCString
encoding: (NSStringEncoding)encoding
{
NSString *obj;
obj = [self allocWithZone: NSDefaultMallocZone()];
obj = [obj initWithCString: nullTerminatedCString encoding: encoding];
return AUTORELEASE(obj);
}
The Form of an Object-Creation Expression
A convention in Cocoa programming is to nest the allocation call inside the initialization call.
MyCustomClass *myObject = [[MyCustomClass alloc] init]; |
便利函数:就是工厂方法,不需使用者管理.
+ (id)dataWithContentsOfURL:(NSURL *)url; |
相关文章推荐
- Cocoa Core Competencies 的Object creation 和IOS面试题示例:写一个NSString类的实现
- Cocoa Core Competencies 的Object creation 和IOS面试题示例:写一个NSString类的实现
- Cocoa Core Competencies 的Object creation 和IOS面试题示例:写一个NSString类的实现
- iOS 面试题(八):实现一个嵌套数组的迭代器
- iOS 实现一个类似电商购物车界面示例
- iOS 实现一个类似电商购物车界面示例
- 面试题---实现一个函数clone,可以对JavaScript中的5种主要的数据类型(包括Number、String、Object、Array、Boolean)进行值复制。
- 【续】iOS 面试题(八):实现一个嵌套数组的迭代器
- IOS中通过给NSString分类实现计算一个字符串中的阿拉伯数字出现的个数
- 01-IOSCore - NSString、NSFileManager、NSBundle、StringAndObjectConvert
- 一个友好的.改善的 Object.prototype.toString的实现
- 自己编写的一个Json工具类,实现了反射将整个Object转换为Json对象的功能,支持Hibernate的延迟加载对象
- 基于Ext-core-3.0实现的tabs示例JS代码解析
- 分形技术的一个简单示例——雪花图案(AS3实现)
- Skinned Mesh 原理解析和一个最简单的实现示例
- Skinned Mesh 原理解析和一个最简单的实现示例
- [置顶] Skinned Mesh原理解析和一个最简单的实现示例
- 分布式编程->Remoting的一个代码示例(借助Remoting实现发送信息功能)
- 一个Spring的beanFactory的简单实现示例(传智播客)
- 一个简单的struts实现文件上传示例