如果您只在 Objective-C 中使用属性,是否有任何理由声明 ivars? [英] Is there any reason to declare ivars if you're using properties exclusively in Objective-C?

查看:16
本文介绍了如果您只在 Objective-C 中使用属性,是否有任何理由声明 ivars?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我倾向于只在我的类中使用属​​性,特别是现在您可以在类扩展中声明属性,这要归功于现代的 Objective-C 2.0 运行时——我使用这个特性来创建私有"属性.

I tend to use properties exclusively in my classes, especially now that you can declare properties in a class extension thanks to the modern Objective-C 2.0 runtime—I use this feature to create "private" properties.

我的问题是,是否有充分的理由再在类接口中声明 ivars.我更喜欢我的面向公众的界面尽可能简洁和干净,只显示我的类的相关方面.

My question is if there is any good reason to ever declare ivars in a class interface anymore. I prefer my public-facing interfaces to be as minimal and clean as possible, only revealing aspects of my class that are pertinent.

例如,我倾向于执行以下操作:

For example, I would tend to do the following:

MyClass.h:

@interface MyClass : NSObject

@property (nonatomic, copy) NSString * publicString;
@property (nonatomic, copy, readonly) NSString * readOnlyString;

@end

MyClass.m:

@interface MyClass ()

@property (nonatomic, copy, readwrite) NSString * readOnlyString;
@property (nonatomic, copy) NSString * privateString;

@end

@implementation MyClass

@synthesize publicString = publicString_;
@synthesize readOnlyString = readOnlyString_;
@synthesize privateString = privateString_;

- (void)init
{
    self = [super init];

    if (self != nil)
    {
        self.publicString = @"Public String";
        self.readOnlyString = @"Read-Only String";
        self.privateString = @"Private String";
    }

    return self;
}

- (void)dealloc
{
    [publicString_ release];
    [readOnlyString_ release];
    [privateString_ release];

    [super dealloc];
}

@end

撇开代码风格偏好不谈,完全像这样避免 ivars 有什么问题吗?

Code style preferences aside, are there any issues with avoiding ivars entirely like this?

推荐答案

我可能找到了一个足够适合我用 ivars 明确支持我的属性的答案.调试器似乎不会列出任何自动合成的 ivars,因此在调试期间没有办法只钻取 self 并检查各种值,除了手动调用属性访问器,这是乏味的.除非他们改变这一点,否则这可能足以让我回到明确声明 ivars.

I may have found an answer that's suitable enough for me to explicitly back my properties with ivars. It doesn't appear as if the debugger will list any automatically synthesized ivars, so there's no way to just drill through self during debugging and check various values other than manually calling the property accessors, which is tedious. Unless they change this, this is probably more than enough reason for me to just go back to declaring ivars explicitly.

这篇关于如果您只在 Objective-C 中使用属性,是否有任何理由声明 ivars?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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