iOS 4 - 使用块作为类的成员

发布于 2024-10-05 14:28:45 字数 865 浏览 2 评论 0原文

我希望有人可以帮助我理解块用作类成员时的语法。我有一些代码实际上工作得很好:

@class Structure;
typedef void (^StructureDeleteCallback)(Structure *);

@interface StructureListDelegate : NRFCTableDelegate
{
    StructureDeleteCallback _structureDeleteCallback;
}

@property (nonatomic, copy) StructureDeleteCallback structureDeleteCallback;

@end

这可以工作,但我想了解 typedef 语句的语法;以及是否确实需要使用 typedef。

我读到的内容表明,在这种情况下建议使用 typedef,因为它使代码更加清晰;但当我尝试在没有 typedef 的情况下执行此操作时,我根本无法编译它。我对 typedef 的理解是,语法基本上是:

typedef [actual type] [new name for type];

如:

typedef double CLLocationDegrees;

但我的 typedef 语句的语法与此不匹配。所以我的问题是:

  • 我的 typedef 的语法如何 声明与其他声明如此不同 typedef 语句/我使用的语法对编译器实际上意味着什么?
  • 是否可以 有一个块作为类的成员 不使用 typedef?

I was hoping someone could help me understand the syntax of blocks when used as members of a class. I have some code that's actually working just fine:

@class Structure;
typedef void (^StructureDeleteCallback)(Structure *);

@interface StructureListDelegate : NRFCTableDelegate
{
    StructureDeleteCallback _structureDeleteCallback;
}

@property (nonatomic, copy) StructureDeleteCallback structureDeleteCallback;

@end

This works, but I would like to understand the syntax of the typedef statement; and whether or not it's actually required to use typedef.

What I've read says that using typedef in this situation is recommended because it makes the code a lot clearer; but I've been unable to get it to compile at all when trying to do this without typedef. My understanding of typedef was that the syntax was basically:

typedef [actual type] [new name for type];

Such as:

typedef double CLLocationDegrees;

But the syntax of my typedef statement doesn't match this. So my questions are:

  • How can the syntax of my typedef
    statement be so different from other
    typedef statements / what does the syntax I'm using actually mean to the compiler?
  • Is it possible to
    have a block as a member of a class
    without using typedef?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

高跟鞋的旋律 2024-10-12 14:28:45

我自己在这里问了一个与您类似的问题: 阻止引用为Objective-C 中的实例变量

查看我的回答这里这里

I myself have asked a question along the lines of yours here: Block references as instance vars in Objective-C

See my answers here and here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文