Objective-C:typedef为一个块,在方法声明中使用它.我该如何实现?

bap*_*ire 9 block objective-c

只是想试试块.我明白了.它们就像函数指针,但它们实际上是对象; 你可以声明一个块变量并为它赋值一个块值; 把它称为功能; 由于缺少一个术语,当它们被执行时,它们会"及时冻结",等等.我创建了几个块并以几种不同的格式成功运行它们,但是当它在方法中使用它们时 - - 使用typedef或不使用 - 我遇到了很多麻烦.例如,这是我创建的对象接口,只是为了获得语法句柄.我几乎不知道如何实现它.

// AnObject.h

#import <Foundation/Foundation.h>

// The idea with the block and the method below is for the block to take
// an int, multiply it by 3, and return a "tripled" int.  The method
// will then repeat: this process however many times the user wants via
// the howManyTimes parameter and return that value in the form of an int.

typedef int (^triple)(int);

@interface AnObject : NSObject
{
    int num;
}

-(int)repeat:(int)howManyTimes withBlock:(triple)someBlock;

@end
Run Code Online (Sandbox Code Playgroud)

到目前为止,这是我实现的内容:

#import "AnObject.h"

@implementation AnObject

@synthesize num;

-(int)repeat:(int)howManyTimes withBlock:(triple)someBlock {
    for (int i = 0; i <= howManyTimes; i++) {
        // What the heck am I supposed to put here?  I'm baffled by the
        // syntax over and over again.
    }
}

@end
Run Code Online (Sandbox Code Playgroud)

我知道我还没有解决实例变量.再次,这是一个粗略的草案,只是试图了解块如何工作.我甚至宣布这种方法对吗?我正在阅读Big Nerd Ranch的Objective-C编程,Mike Clark关于Pragmatic Studio中的块的文章,以及几个SO线程.找不到任何相关的东西.谢谢.

编辑:XCode 4.3.2,如果重要的话.

进一步编辑:好的.使用BJ(略微修改)的例子,我想我已经提出了一种将5乘以3的非常复杂的方法.:)

// BJ's implementation:

-(int)repeat:(int)howManyTimes withBlock:(Triple)someBlock {

    int blockReturnValue;

    for (int i = 0; i <= howManyTimes; i++) {
        blockReturnValue = someBlock(i);
    }
    return blockReturnValue;
}
Run Code Online (Sandbox Code Playgroud)

主要:

...
    @autoreleasepool
    {
        AnObject *obj = [[AnObject alloc] init];

        NSLog(@"%d", [obj repeat: 5 withBlock: ^ (int number) {
            return number * 3;
        }]);

    }
    return 0;
...
Run Code Online (Sandbox Code Playgroud)

输出是:

15
Run Code Online (Sandbox Code Playgroud)

现在,它正在回击15,因为我定义为参数的块只运行一次,对吧?它将"数字"(在这种情况下为5)乘以3并冻结该答案,对吧?我确定我刚刚创建了一个完全没用的方法,我还不知道如何利用块的优点/功能.我对么?

/ ********************* UPDATE ********************* /

更新:我明白你在说什么,CRD.但是,对于任何可能正在阅读本文的新程序员来说,只需要进行修正,获得不同的输出并继续"Que?" 你的for循环应该是:

for (int i = 0; i < howManyTimes; i++)
            value = someBlock(value);
Run Code Online (Sandbox Code Playgroud)

...要么...

(i = 1; i <= howManyTimes; i++)
Run Code Online (Sandbox Code Playgroud)

......得到答案243.

而且,是的,这正是我最初尝试使用此代码的原因.至少那是我认为应该发生的事情.原来,作者的意图不是将数字加倍,存储该值,存储值的三倍,存储......等等,而只是为数字1-5(3,6,9,打印x*3) 12,15).

这是成品.我只是输入一个带有int的块并返回一个名为Tripler的int.我还将参数的名称从"someBlock"更改为"triple"以更清楚地指示块的预期用途.我认为这些是对代码的唯一更改.

/********************  interface  ********************/


#import <Foundation/Foundation.h>

typedef int (^Tripler)(int);

@interface AnObject : NSObject

-(void)iterateFromOneTo:(int)number withBlock:(Tripler)triple;

@end

/********************  implementation  ********************/


#import "AnObject.h"

@implementation AnObject

-(void)iterateFromOneTo:(int)number withBlock:(Tripler)triple {
    for (int i = 1; i <= number; i++) {
        NSLog(@"%d", triple(i));
    }
}

@end

/********************  main.m  ********************/


#import "AnObject.h"
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{
    @autoreleasepool
    {
        AnObject *obj = [[AnObject alloc] init];

        [obj iterateFromOneTo:5 withBlock:^(int number) {
            return number * 3;
        }];
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

您可以想象,结果输出是:

2012-05-05 17:10:13.418 Untitled 2[71735:707] 3
2012-05-05 17:10:13.445 Untitled 2[71735:707] 6
2012-05-05 17:10:13.446 Untitled 2[71735:707] 9
2012-05-05 17:10:13.446 Untitled 2[71735:707] 12
2012-05-05 17:10:13.447 Untitled 2[71735:707] 15
Run Code Online (Sandbox Code Playgroud)

我让它变得比它需要的复杂得多.很抱歉在OP中解释得太差了.谢谢你的帮助!/线?:)

CRD*_*CRD 8

从阅读你的问题,我理解,或者可能被误解,你的意图是产生你的块的n次结果; 例如,如果您应用两次三倍函数,则会得到原始值乘以9.

万一它有帮助,这里是代码:

@interface AnObject

typedef int (^monadic)(int); // an function which takes an int and return an int

- (int) repeat:(int)howManyTimes for:(int)value withBlock:(monadic)someBlock;

@end

@implementation AnObject

- (int) repeat:(int)howManyTimes for:(int)value withBlock:(monadic)someBlock
{
   for (int i = 0; i < howManyTimes; i++)
      value = someBlock(value);

   return value;
}

@end
Run Code Online (Sandbox Code Playgroud)

现在用以下方法调用:

AnObject *myObject = [AnObject new];

int z = [myObject repeat:5  
                     for:1 
               withBlock: ^(int number)
                          {
                             return number * 3;
                          }
        ];
Run Code Online (Sandbox Code Playgroud)

并将z有价值243.


BJ *_*mer 7

只需像普通的C函数一样调用块.

-(int)repeat:(int)howManyTimes withBlock:(triple)someBlock {
    for (int i = 0; i <= howManyTimes; i++) {
        int blockReturnValue = someBlock(i);
        // do something with blockReturnValue
    }
}
Run Code Online (Sandbox Code Playgroud)

"进一步编辑"后更新

不,您作为参数传入的块运行五次,每次都通过for循环.

  • 第一次,它调用块1作为参数,然后返回3.它将其存储在blockReturnValue,然后继续循环的下一次迭代.
  • 第二次,它调用块2作为参数,然后返回6.它存储blockReturnValue,完全覆盖我们在上一次传递中存储的值.
  • 第三次,它以3作为参数的方式禁止该块,然后返回9.同样,它会覆盖中的值blockReturnValue.
  • 第四次,我们存储12blockReturnValue.
  • 第五次,我们存储15blockReturnValue.

然后我们退出for循环,并返回15.所以是的,你是正确的,你已经做了一个无意义的方法乘以3.但你这样做也会做一堆无用的计算.