《Programming with Objective-C》第五章 Customizing Existing Classes
2015-10-26 20:21
363 查看
1.分类里面只新增函数,不要新增变量;虽然新增是语法合法的,但是编译器并不会为你的property合成相应的成员变量、setter和getter
Categories can be used to declare either instance methods or class methods but are not usually suitable for declaring additional properties. It’s valid syntax to include a property declaration in a category interface, but it’s not possible to declare an additional instance variable in a category. This means the compiler won’t synthesize any instance variable, nor will it synthesize any property accessor methods. You can write your own accessor methods in the category implementation, but you won’t be able to keep track of a value for that property unless it’s already stored by the original class.
2.扩展的方法在.m文件中的@implementation中实现
The methods declared by a class extension are implemented in the
3.扩展可以看做是匿名的分类,但是扩展中可以新增变量
class extensions are often referred to as anonymous categories.
Unlike regular categories, a class extension can add its own properties and instance variables to a class.
4.在.h中将属性声明为readonly,但是在.m中的扩展中声明为readwrite,这样内部可读写,外部只能读
It’s common, for example, to define a property as
5.Objective-C并不像C++那样,一编译一链接就变成机器码,它执行的时候还需要一个Runtime系统来支持
Objective-C is more than just a language that is compiled down to machine code. Instead, it requires a runtime system in place to execute that code.
Categories can be used to declare either instance methods or class methods but are not usually suitable for declaring additional properties. It’s valid syntax to include a property declaration in a category interface, but it’s not possible to declare an additional instance variable in a category. This means the compiler won’t synthesize any instance variable, nor will it synthesize any property accessor methods. You can write your own accessor methods in the category implementation, but you won’t be able to keep track of a value for that property unless it’s already stored by the original class.
2.扩展的方法在.m文件中的@implementation中实现
The methods declared by a class extension are implemented in the
@implementationblock for the original class so you can’t, for example, declare a class extension on a framework class, such as a Cocoa or Cocoa Touch class like
NSString.
3.扩展可以看做是匿名的分类,但是扩展中可以新增变量
class extensions are often referred to as anonymous categories.
Unlike regular categories, a class extension can add its own properties and instance variables to a class.
4.在.h中将属性声明为readonly,但是在.m中的扩展中声明为readwrite,这样内部可读写,外部只能读
It’s common, for example, to define a property as
readonlyin the interface, but as
readwritein a class extension declared above the implementation, in order that the internal methods of the class can change the property value directly.
5.Objective-C并不像C++那样,一编译一链接就变成机器码,它执行的时候还需要一个Runtime系统来支持
Objective-C is more than just a language that is compiled down to machine code. Instead, it requires a runtime system in place to execute that code.
相关文章推荐
- Objective-C之NSArray(数组)默认排序与自定义排序
- 【JAVA核心技术卷一】Object Oriented面向对象
- public Object queryOne(String sql, Object... objs)
- JSONObject accumulate
- 轻松学习之一--Objective-C消息转发
- VidPN Objects and Interfaces
- OC中解档读取内容为空
- 输出数组内中文字符
- XML,Object,Json分析转换Xstream采用
- Objective-C学习笔记之for( int )机制
- Sea.js出现Uncaught TypeError: object is not a function的解决方法
- 苹果开发 所要知道的那些类库含义 Cocoa,Foundation,UIKit,Objective-C,Xcode,Interface Builder,
- Objective-C MRC多个对象相互引用的内存管理
- Effective Objective-C 2.0 第10条:在既有类中使用关联对象存放自定义数据
- Effective Objective-C 2.0 第9条:以“类族模式”隐藏实现细节
- Java记录 -44- Object数组陷阱
- Objective-C 获取当前执行函数的名称
- Effective Objective-C 2.0 第8条:理解“对象等同性”概念
- Objective-C description的用法
- Objective-C 成员变量的访问修饰即成员变量可见性解析