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

查看:74
本文介绍了如果您仅在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天全站免登陆