属性也声明为实例变量吗? [英] Properties declared as instance variables too?

查看:80
本文介绍了属性也声明为实例变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码有效:

    @interface StringStuff : NSObject {

    }
    @property (nonatomic, retain) NSString *String1;
    @property (nonatomic, retain) NSString *String2;

    - (NSString *) doSomethingWithStrings;

    @end

但是我经常看到:

        @interface StringStuff : NSObject {
             NSString *String1;
             NSSTring *String2;    
        }
        @property (nonatomic, retain) NSString *String1;
        @property (nonatomic, retain) NSString *String2;

        - (NSString *) doSomethingWithStrings;

        @end

是否也经常将属性声明为实例变量?它只是被认为是好的形式吗?

Is there a reason that properties are often declared as an instance variable as well? Is it just considered good form?

推荐答案

旧版;过去(现在仍然在32位Mac OS X目标代码上)需要ivar声明.在iOS,模拟器和64位OS X上不再如此.

Legacy; it used to be (and still is on 32 bit Mac OS X targeted code) that the ivar declarations were required. That is no longer true on iOS, the simulator and 64 bit OS X.

请注意,在确实需要self.iVar的情况下,@synthesize iVar = iVar_;可以防止意外的直接访问.

Note that it is common to @synthesize iVar = iVar_; to prevent accidental direct access where self.iVar is really required.

这篇关于属性也声明为实例变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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