Cocoa Core Competencies 的Object creation 和IOS面试题示例:写一个NSString类的实现
2013-05-17 12:11
561 查看
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 alloc
or 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 isa pointer) 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
init method 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 alloc or 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;
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 alloc
or 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 isa pointer) 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
init method 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 alloc or 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类的实现
- 01-IOSCore - NSString、NSFileManager、NSBundle、StringAndObjectConvert
- iOS 实现一个类似电商购物车界面示例
- iOS 面试题(八):实现一个嵌套数组的迭代器
- IOS中通过给NSString分类实现计算一个字符串中的阿拉伯数字出现的个数
- 【续】iOS 面试题(八):实现一个嵌套数组的迭代器
- 面试题---实现一个函数clone,可以对JavaScript中的5种主要的数据类型(包括Number、String、Object、Array、Boolean)进行值复制。
- iOS 实现一个类似电商购物车界面示例
- IOS上实现的自定义仪表盘示例
- iOS实现导航栏透明示例代码
- 通过实现一个 TableView 来理解 iOS UI 编程
- iOS实现pop效果(模态一个气泡出来)
- Angular.js实现多个checkbox只能选择一个的方法示例
- iOS 实现多个按钮,点选一个其它都取消选中状态的最佳方法
- 利用vue组件自定义v-model实现一个Tab组件方法示例
- [转]Remoting的一个代码示例(借助Remoting实现发送信息功能)
- iOS开发网络篇—实现一个视频播放客户端小应用
- iOS开发:一个高仿美团的团购ipad客户端的设计和实现(功能:根据拼音进行检索并展示数据,离线缓存团购数据,浏览记录与收藏记录的批量删除等)