在方括号中声明.h文件中的变量



当变量在.h文件的公共接口内声明时,括号内发生了什么?什么情况下你会做这样的事情?在我看来,您应该将这些变量支持的属性公开,并从.h文件中删除这些变量。

@interface GSFullscreenAdViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
    /**
     * This view controller's view.
     */
    GSFullscreenAdView * m_adView;
    /**
     * The ad that is currently fetched or displaying. Resets to nil on ad dismissal.
     */
    GSFullscreenAd * m_currentAd;
    /**
     * The view controller that this GSFullscreenAdViewController is being
     * displayed from.
     */
    UIViewController * m_parentViewController;
    /**
     * Whether the ad is currently displaying
     */
    BOOL m_isDisplaying;
    /**
     * Whether the ad is currently animating onto the screen
     */
    BOOL m_isPresenting;
    /**
     * Whether the ad is in the process of being dismissed
     */
    BOOL m_isDismissing;
    UIImagePickerController * m_imagePickerController;
}
@property (readonly) GSFullscreenAdView * adView;
@property (readonly) BOOL isDisplaying;
@property (nonatomic) BOOL allowOrientationChange;

@implementation部分中声明实例变量的功能实际上是Objective-C的一个相对较新的功能。在Xcode 4.2(2011/10/12发布)之前,您只能在@interface部分声明实例变量。也许您正在查看的代码是在Xcode 4.2(或更高版本)被广泛使用之前编写的。

你自己在Objective-C功能可用性索引中检查一下。

如今,在大多数情况下,只声明一个属性就可以了。对于objective-c的旧版本,必须手动声明相应的iVar。为此,必须在声明属性之前在大括号内声明iVar。

即使在今天,您也可能希望将一个属性与给定的iVar关联起来(通过在@synthezise语句中命名iVar)。但是,为此,在@interface语句之后的花括号内声明iVar就足够了。在这种情况下,您可以在.m文件中的第二个@interface语句之后的括号内声明它们。

嗯,它仍然有效。一些奥德教程仍然使用这种模式。您或您的团队/公司可能仍然使用旧的代码来执行此操作。

最后,有时很难克服一个古老的奇特现象:-)

最新更新