How Do I Declare A Block in Objective-C?

How Do I Declare A Block in Objective-C?

As a local variable:

returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...};

As a property:

@property (nonatomic, copy, nullability) returnType (^blockName)(parameterTypes);

As a method parameter:

- (void)someMethodThatTakesABlock:(returnType (^nullability)(parameterTypes))blockName;

As an argument to a method call:

[someObject someMethodThatTakesABlock:^returnType (parameters) {...}];

As a typedef:

typedef returnType (^TypeName)(parameterTypes);
TypeName blockName = ^returnType(parameters) {...};

This site is not intended to be an exhaustive list of all possible uses of blocks.
If you find yourself needing syntax not listed here, it is likely that a typedef would make your code more readable.

Unable to access this site due to the profanity in the URL? http://goshdarnblocksyntax.com is a more work-friendly mirror.

By Mike Lazer-Walker, who has a very bad memory for this sort of thing.

时间: 2024-10-29 19:07:47

How Do I Declare A Block in Objective-C?的相关文章

Objective C block背后的黑魔法

前言 block在Objective C开发中应用非常广泛,我们知道block会捕获外部对象,也知道使用block要防止循环引用. "知其然而不知其所以然"是一件很痛苦的事情,那么block这套机制在OC中是如何实现的呢?本文通过从C/C++到汇编层面分析block的实现原理. Clang clang是XCode的编译器前端,编译器前端负责语法分析,语义分析,生成中间代码(intermediate representation ). 比如当你在XCode中进行build一个.m文件的时

Block 简介

1.Blocks简介 Block字面意思就是代码块 iOS4.0.Mac OS X 10.6开始Apple引入的特性 Block是Objective C语言中的对象 但是与NSObject有所区别 Block是特殊的Objective C对象 Block 对象提供了一个使用 C 语言和 C 派生语言(如 Objective-C 和 C++)来创建表达式作为一个特别的函数.在其他语言和环境中,一个block对象有时候被称为“闭包(closure)”.在这里,它们通常被口语化为”块(blocks)”

深入理解ObjetiveC的Block

0. 问题所在 下面给出一段代码: - (NSArray*) getBlockArray { int num = 916; return [[NSArray alloc] initWithObjects: ^{ NSLog(@"this is block 0:%i", num); }, ^{ NSLog(@"this is block 1:%i", num); }, ^{ NSLog(@"this is block 2:%i", num); },

堆block和栈block的区分

0. 问题所在 下面给出一段代码: - (NSArray*) getBlockArray { int num = 916; return [[NSArray alloc] initWithObjects: ^{ NSLog(@"this is block 0:%i", num); }, ^{ NSLog(@"this is block 1:%i", num); }, ^{ NSLog(@"this is block 2:%i", num); },

Block存储域探析

接<Block截获自动变量实现与__block修饰符内部实现>我们继续探讨Block 留下的问题 1,__Block_byref_i_0 *__forwarding;这个指向自身的指针是什么鬼,有什么作用,什么时候用? 2,Desc_0结构体中多出来的void (*copy) void (*dispose)这两个方法有什么作用,什么时候用? Block 和 __block变量的实质 名称 实质 Block 栈上Block的结构体实例 __block变量 栈上__block变量的结构体实例 Bl

(译)IOS block编程指南 2 block开始

Getting Started with Blocks(开始block) The following sections help you to get started with blocks using practical examples. 接下来这一节有实用的例子帮你开始blocks. Declaring and Using a Block (定义和使用block) You use the ^ operator to declare a block variable and to indic

IOS Block详解

这是我原先写的OC中关于协议和代理的文章,建议大家阅读此篇文章的时候先阅读此文章,便于大家理解:  IOS Protocol与Delegate详解(一)  IOS Protocol与Delegate详解(二) 官方中对于Block的用途为:  You can use blocks to compose function expressions that can be passed to API, optionally stored, and usedby multiple threads. Bl

IOS开发之---初识Block

正文 Block简介 我们可以把Block当做Objective-C的匿名函数.Block允许开发者在两个对象之间将任意的语句当做数据进行传递,往往这要比引用定义在别处的函数直观.另外,block的实现具有封闭性(closure),而又能够很容易获取上下文的相关状态信息. Block的创建 实际上,block使用了与函数相同的机制:可以像声明函数一样,来声明一个bock变量:可以利用定义一个函数的方法来定义一个block:也可以将block当做一个函数来调用. 1 // main.m 2 #im

IOS block编程指南 2 block开始

Getting Started with Blocks(开始block) The following sections help you to get started with blocks using practical examples. 接下来这一节有实用的例子帮你开始blocks. Declaring and Using a Block (定义和使用block) You use the ^ operator to declare a block variable and to indic