我应该使用什么限定符将一个块声明为一个ivar? [英] What qualifier should I use to declare a block as an ivar?

查看:93
本文介绍了我应该使用什么限定符将一个块声明为一个ivar?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:


typedef void(^responseBlock)(NSDictionary*, NSError *);
@interface MyClass : NSObject
{
    [??] responseBlock responseHandler;
}

我应该在[??]括号中放置什么限定词?

What qualifier should I put in the [??] brackets?

我已经读过,应该使用复制限定符设置作为属性的块...但是在这种情况下,我不需要将块作为属性公开.我只是希望它保持不变,但是如何指定副本呢?而且,在未指定任何内容的情况下,使用的默认限定词是什么?和其他所有东西一样__strong吗?

I've read that blocks as properties should be setup with the copy qualifier...but in this case I don't need the block exposed as a property. I simply want it to remain an ivar but how can I specify copy? And also, without specifying anything what is the default qualifier used? Is it __strong as in the case with everything else?

我在ios5上使用ARC.

I'm using ARC on ios5.

推荐答案

是的,块是ObjC中的对象,因此__strong是合适的限定符.由于这是默认设置,因此您实际上可以将其保留.

Yes, Blocks are objects in ObjC, so __strong is the appropriate qualifier. Since that's the default, you can in fact leave it off.

您无法指定在分配时复制不带属性的块,这是您的责任(responseHandler = [someBlock copy];).您可以通过在.m文件中放置类扩展名,来声明仅对该类本身可见的属性(其他代码不可用):

There's no way for you to specify that the Block be copied on assignment without a property -- that will be your responsibility (responseHandler = [someBlock copy];). You could declare a property that's only visible to this class itself (not available to other code) by putting a class extension in your .m file:

@interface MyClass ()
@property (copy) responseBlock responseHandler;
@end

此方法(在合成后)将为您提供常用的访问器方法,当您使用它们时,这些方法将为您提供保护.

This (upon being synthesized) will give you the usual accessor methods, which will take care of the copy for you when you use them.

还请注意,这是有可能的(并且

Also be aware that it's possible (and now the recommended procedure) to declare instance variables in the @implementation block. It sounds like you want this to be a private attribute (no property access), and the ivars declared there can't be seen by any other code. (Of course you don't need to do this if you're using a property; @synthesize will create the ivar for you.)

@implementation MyClass
{
    responseBlock responseHandler;
}

// Continue with implementation as usual

这篇关于我应该使用什么限定符将一个块声明为一个ivar?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆