加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 05:58:42 所属栏目:百科 来源:网络整理
导读:只是试图得到一个句柄.我得到这个概念.它们就像函数指针,但它们实际上是对象;您可以声明一个块变量并为其分配一个块值;称之为功能;他们得到“冻结时间”,因为缺少一个术语,执行时等等.我已经创建了几个块,并以几种不同的格式成功运行它们,但是当涉及到在方法
只是试图得到一个句柄.我得到这个概念.它们就像函数指针,但它们实际上是对象;您可以声明一个块变量并为其分配一个块值;称之为功能;他们得到“冻结时间”,因为缺少一个术语,执行时等等.我已经创建了几个块,并以几种不同的格式成功运行它们,但是当涉及到在方法中使用它们时,– 没有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

到目前为止,这是一个实现方法:

#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

我知道我还没有处理实例变量.再次,这是一个粗略的草案,只是想弄清块如何工作.我甚至宣布这种方法吗?我正在阅读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;
}

主要:

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

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

    }
    return 0;
...

输出为:

15

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

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

更新:我明白你在说什么,CRD.尽管如此,对于任何可能正在阅读的程序员来说,获得不同的输出并进行“Que?”你的循环应该是:

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

…要么…

(i = 1; i <= howManyTimes; i++)

…得到答案243.

而且,是的,这正是我最初尝试用这个代码.至少这就是我以为应该发生的事情.结果是作者的意图不是三倍数字,存储该值,存储值的三倍,存储…等等,而只是打印x * 3的数字1-5(3,6,9,12,15).

这是成品.我只是typedef的一个块,它接受一个int并返回一个int,称为Tripler.我还将参数的名称从“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;
}

你可以想象,产生的结果是:

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

我使它比它需要的复杂得多.对不起,在OP中解释得如此糟糕.谢谢你的帮助! /线?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读