动态特性
1. 动态输入
在运行时才决定一个对象的类,通过使用id数据类型。与动态输入响应的是静态输入,编译时就指定对象的类。动态类型id表示一个指针,而声明静态类型的
需要在对象名前加*表示指针。对象的引用必须为指针。
2. 动态绑定
在运行时才决定需要调用的方法,消息被发送时才和响应的方法绑定
3. 动态载入
在运行时添加代码块和其他资源。
类和对象
1. 对象是类在运行时的实例,包含类声明的实例变量和指向方法的指针,拥有自己的内存空间。
2. 类对象在在运行时创建的class类型对象,
3. nil与NULL都是定义为0的常量
方法和消息
属性
1.
Block

int result = myBlock(4); // result is 281. block是封装了一段代码的对象,可以作为方法的参数传递,或者作为方法的返回值。block可以定义自己的参数类型和返回值,block用^标记
2. block可以访问局部变量,实例变量,方法的参数,但这个访问是只读的。如果用
A block shares data in the local lexical scope. This characteristic of blocks is useful because if you implement a method and that method defines a block, the block has access to the local variables and parameters of the method
(including stack variables) as well as to functions and global variables, including instance variables. This access is read-only, but if a variable is declared with the __block modifier, its value can be changed within the block. Even after the method or function
enclosing a block has returned and its local scope is destroyed, the local variables persist as part of the block object as long as there is a reference to the block.
As method or function parameters, blocks can serve as a callback. When invoked, the method or function performs some work and, at the appropriate moments, calls back to the invoking code—via the block—to request additional information or to obtain program-specific
behavior from it. Blocks enable the caller to provide the callback code at the point of invocation. Instead of packaging the required data in a “context” structure, blocks capture data from the same lexical scope as the host method or function. Because the
block code does not have to be implemented in a separate method or function, your implementation code can be simpler and easier to understand.
Objective-C frameworks have many methods with block parameters. For example, the UIKit framework declares the following class method, which has two parameters that take blocks:

本文介绍了Objective-C中的动态特性,包括动态输入、动态绑定和动态加载的概念。此外还探讨了类和对象的关系,以及方法和消息的工作原理。特别强调了Block作为一种封装代码片段的对象,在方法中作为参数或返回值的应用。
87

被折叠的 条评论
为什么被折叠?



