分享

object-c 学习第五天(对象块)

 quasiceo 2014-12-01
分类: object-c 2012-08-16 13:51 3511人阅读 评论(0) 收藏 举报

对象块

Blocks

:Apple有一個叫做GCD(Grand Central Dispach)的新功能,用在同步處理(concurrency)的環境下有更好的效率。Block語法產生的動機就是來自於GCD,用Block包好一個工作量交給GCDGCD有一個宏觀的視野可以來分配CPUGPUMemory的來下最好的決定。

Block 簡介

Block其實行為和Function很像,最大的差別是在可以存取同一個Scope的變數值

Block 實體會長成這樣

 

块是一个封装了一个单元工作的对象——,就是说是一段代码——可在任何时间执行。它们本质上是很方便的并且也是匿名的,我们可以以一个对象的参数访问它或者它也可以通过方法来返回。块本身有一个类型的参数列表并且可能有一个可推测的?或者一个声明的返回类型。你也可以分配一个块给一个变量然后就像一个函数一样调用它。

 

Blocks are objects that encapsulate a unit of work—that is, a segment of code—that can be executed at any time. They are essentially portable and anonymous functions that one can pass in as parameters of methods and functions or that can be returned from methods and functions. Blocks themselves have a typed parameter list and may have an inferred or a declared return type. You can also assign a block to a variable and then call it just as you would a function.

 

一个^符号被用来作为块的语法标志。还有给参数的一些我们熟悉的语法,返回值,和块的body。下面就有一个例子来说明这个语法,特别是当我们要分配一个块变量时。

 

 

A caret (^) is used as a syntactic marker for blocks. There are other, familiar syntactic conventions for parameters, return values, and body of the block (that is, the executed code). The following figure explicates the syntax, specifically when assigning a block to a variable.

 

你也可以调用这个块变量就像你调用方法一样

You can then call the block variable as if it were a function:

int result = myBlock(4); // result is 28

 

 

一个块在一个当地的“lexical scope”内分享数据。块的这个特点是十分有用的因为如果你实现一个方法的时候并且这个方法定义了一个块,这个块就有访问本地变量和方法参数(包括栈里面的变量)还有方法和全局变量的权力,也包括实例变量。这个访问是只读的,但是如果一个变量被声明为_block这样的修饰符,它的值就可以在块里变化。即使当一个方法已经封闭块并且已经返回了而且它的“local scope”也已经被摧毁的时候,只要有块的引用,本地变量作为块的对象会一直存在

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 has been destroyed, the local variables persist as part of the block object as long as there is a reference to the block.

 

 

作为一个方法的参数,块也可以充当一个回调函数。当被回调函数被调用的时候,方法或函数执行一些工作,在适当的时候调用回调代码-----通过块-----请求额外的信息或者获取程序-----的一种特定的行为。(和c++中的回调函数功能一样)。块可以让调用者在需要的时候提供回调代码。而不是把所用需要的数据包装在一个“context”的结构中,块从一个相同的“lexical scope”捕捉数据作为主方法或者主函数,因为块代码不需要实施在一个单独的方法或函数,你的实现代码将会变得简单并且很容易被理解。

 

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的框架已经很多方法和块参数。例如,在Foundation框架中NSNotificationCenter类声明了如下的方法,其中就有一个块参数

- (id)addObserverForName:(NSString *)name object:(id)obj queue:(NSOperationQueue *)queue usingBlock:(void (^)(NSNotification

Objective-C frameworks have many methods with block parameters. For example, theNSNotificationCenter class of the Foundation framework declares the following method, which has a block parameter:

 

这个方法在通讯中心加了一个“观察者”(也就是触发点吧)。当一个指定名字的通告被发送的时候。块就被调用来处理这个通告。

 

    opQ = [[NSOperationQueue alloc] init];

    [[NSNotificationCenter defaultCenter] addObserverForName:@"CustomOperationCompleted"

             object:nil queue:opQ

        usingBlock:^(NSNotification *notif) {

        //处理通告

    }];

 

This method adds an observer to the notification center (notifications are discussed inStreamline Your App with Design Patterns). When a notification of the specified name is posted, the block is invoked to handle the notification.

 

其他资料推荐 http://www.cnblogs.com/east520/archive/2011/08/23/2150288.html

 

主题推荐
对象 object global variables design patterns 全局变量
猜你在找
使用UIAppearance 协议自定义视图
在Objective-C中 NSString并不受引用计数器机制管理
Flash/Flex 移动端开发 之 DPI
4-数据类型和表达式
MVC面试问题与答案
ProtoBuf开发者指南
IOS开发基础之──MVC模式
android平滑过渡的动画效果
xcode objcet c 函数定义和使用
Object-C学习之四:字段与函数

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多