【问题标题】:Understanding ios and objective-c @property [duplicate]了解ios和objective-c @property [重复]
【发布时间】:2014-08-31 10:03:24
【问题描述】:

有什么区别:

@interface PhotoAppViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
    UIImageView * imageView;
    UIButton * choosePhotoBtn;
    UIButton * takePhotoBtn;
}

@property (nonatomic, retain) IBOutlet UIImageView * imageView;
@property (nonatomic, retain) IBOutlet UIButton * choosePhotoBtn;
@property (nonatomic, retain) IBOutlet UIButton * takePhotoBtn;

还有这个:

@interface PhotoAppViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (nonatomic, retain) IBOutlet UIImageView * imageView;
@property (nonatomic, retain) IBOutlet UIButton * choosePhotoBtn;
@property (nonatomic, retain) IBOutlet UIButton * takePhotoBtn;

委托后的大括号是什么意思?

【问题讨论】:

  • 重复的答案很有帮助

标签: ios objective-c ios7 properties delegation


【解决方案1】:

来自http://rypress.com/tutorials/objective-c/properties.html

一个对象的属性让其他对象检查或改变它的状态。 但是,在一个设计良好的面向对象程序中,不可能 直接访问对象的内部状态。相反,访问器 方法(getter 和 setter)被用作抽象 与对象的底层数据交互。

Stackoverflow Answer

【讨论】:

    【解决方案2】:

    “花括号”是定义instance variables(也称为ivars)的范围。属性是可以公开访问的变量(即其他类),而实例变量是私有的,只能在类实现本身的范围内访问。

    阅读 thisthis 以直观地了解属性和 ivars 之间的区别。

    我强烈建议阅读 Apple 关于 Objective-C 的文档,或者一本关于同一主题的好书,如果前者对你的口味来说有点过于技术性的话。

    【讨论】:

    • instance variables are private and can be only accessed in the scope of the class's implementation itself 这实际上是错误的。事实上,在 ObjC 中没有什么是私有的
    • 接口文件中声明的实例变量可以从任何类访问。它完全暴露了。
    • 不,@Anil,ivars 默认情况下是 protected —— 只有类本身或子类可以访问它们。 ivar 必须在接口中显式声明 @public 才能被其他类访问。合成的 ivars 或在实现块中声明的 ivars 只能由类本身使用。
    • @JoshCaswell 这是我的错觉。是的,ivars 默认受到保护,我检查了它。感谢您澄清这一点:)
    猜你喜欢
    • 2016-05-19
    • 2015-08-25
    • 1970-01-01
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 2021-02-17
    • 2012-07-26
    • 2016-03-17
    相关资源
    最近更新 更多