Objective c 目标C:typedef';d一个块,在方法声明中使用它。我如何实现这一点?

Objective c 目标C:typedef';d一个块,在方法声明中使用它。我如何实现这一点?,objective-c,block,Objective C,Block,只是想控制住街区。我明白这个概念。它们就像函数指针,但实际上是对象;可以声明块变量并为其指定块值;像函数一样调用它;它们“在时间上被冻结”,因为缺少一个术语,当它们被执行时,等等。我已经创建了一些块,并以几种不同的格式成功地运行了它们,但是当涉及到在方法中使用它们时——无论是使用typedef还是不使用typedef——我遇到了很多麻烦。例如,我创建了一个对象接口,只是为了获得语法句柄。我几乎不知道如何实施它 // AnObject.h #import <Foundation/Found

只是想控制住街区。我明白这个概念。它们就像函数指针,但实际上是对象;可以声明块变量并为其指定块值;像函数一样调用它;它们“在时间上被冻结”,因为缺少一个术语,当它们被执行时,等等。我已经创建了一些块,并以几种不同的格式成功地运行了它们,但是当涉及到在方法中使用它们时——无论是使用typedef还是不使用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
输出为:

15
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
现在,它是回退15,因为我定义为参数的块只运行一次,对吗?它将“数字”(在本例中为5)乘以3并冻结该答案,对吗?我确信我刚刚创建了一个完全无用的方法,我还不知道如何利用块的优点/特性。我说得对吗

/********************更新***********************/

更新:我明白你的意思,CRD。不过,这只是一个更正,对于任何可能正在阅读本文的新程序员来说,获得不同的输出并发出“Que?”的for循环应该是:

for (int i = 0; i < howManyTimes; i++)
            value = someBlock(value);

我把事情弄得比需要的复杂得多。很抱歉在OP中解释得这么糟糕。谢谢你的帮助/线程?:)

只需像调用常规C函数一样调用块

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

因为(int i=0;i通过阅读您的问题,我理解,或者可能误解,您的意图是产生应用块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

z
的值将为
243

好的。也许比我想象的要简单。现在就开始处理。好的。请参阅“进一步编辑:”谢谢你的帮助,BJ。不幸的是,我认为我的编程智商很低。:P我了解了发生的事情的要点。应该更清楚,但我想这只是语法让我适应了。这正是阻止我了解真正发生的事情的原因。我会尽可能多地了解块,直到我最终看到它们是如何工作的k以及何时正确使用它们。非常感谢您的帮助和耐心。感谢您的回复,CRD。感谢您抽出时间。请参阅上面的“更新”。@baptzmoffire-正确,oops:-(已修复拼写错误,现在它生成243!请阅读此区块解释。。。
@import Foundation;

typedef int(^MyBlockType)(int);

@implementation NSObject(extra)
+ (int)invokeBlock:(MyBlockType)block withArgument:(int)arg
{
    return block(arg);
}
@end;

int main() {
    @autoreleasepool {
        NSLog(@"executeBlock(3) returns %d",
              [NSObject invokeBlock:^(int param) { return param * param; } withArgument:3]);
    } return 0;
}
/********************  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;
}
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
-(int)repeat:(int)howManyTimes withBlock:(triple)someBlock {
    for (int i = 0; i <= howManyTimes; i++) {
        int blockReturnValue = someBlock(i);
        // do something with blockReturnValue
    }
}
@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
AnObject *myObject = [AnObject new];

int z = [myObject repeat:5  
                     for:1 
               withBlock: ^(int number)
                          {
                             return number * 3;
                          }
        ];
@import Foundation;

typedef int(^MyBlockType)(int);

@implementation NSObject(extra)
+ (int)invokeBlock:(MyBlockType)block withArgument:(int)arg
{
    return block(arg);
}
@end;

int main() {
    @autoreleasepool {
        NSLog(@"executeBlock(3) returns %d",
              [NSObject invokeBlock:^(int param) { return param * param; } withArgument:3]);
    } return 0;
}