私人和公共成员应如何实现目标? [英] How should private and public members be implemented in objective-c?

查看:108
本文介绍了私人和公共成员应如何实现目标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些讨论涉及到在工作中使用属性和实例变量,因此我想找到一个wiki答案。现在,我知道在objective-c中没有真正的私有成员类型,一切都是公开的。然而,我有点担心我们应该如何设计我们的类,并遵守OOP原则。我想听听这三种设计方法的意见:



A。根据各种职位,甚至到一个新的斯坦福大学iPhone开发课程,你应该总是使用属性无处不在你可以。然而,IMHO,这种方法制定OOP设计原则,因为在这种情况下,所有成员公开。为什么我需要将所有内部/本地实例变量发布到外部?此外,如果你通过属性使用合成的setters,而不是直接使用本地ivar,有一些很少(但仍然)开销。下面是一个示例:

  // ====头文件===== // 
@interface MyClass :NSObject

@property(nonatomic,retain)NSString * publicMemberWithProperty;
@property(nonatomic,retain)NSString * propertyForPrivateMember;

@end

B。另一种方法是在私有成员的头文件(不声明相对属性)中声明ivars,并在同一头文件中声明公共成员的纯属性(不声明相对ivar)。在这种情况下,ivars将直接在类中使用。这种方法是有意义的,但不使用所有的属性的好处,因为我们手动释放旧的值,然后再设置新的值。下面是一个示例:

  // ====头文件===== // 
@interface MyClass :NSObject {
NSString * _privateMember;
}

@property(nonatomic,retain)NSString * publicMemberWithProperty;

@end

C。在头文件中声明公共成员的纯属性(不声明相对ivars),并在实现文件中的私有接口声明私有成员的纯属性(不声明相对ivars)。这种方法IMHO比第一个更清楚,但同样的问题仍然是:为什么我们必须有内部/本地成员的属性?下面是一个示例:

  // ====头文件===== // 
@interface MyClass :NSObject

@property(nonatomic,retain)NSString * publicMemberWithProperty;

@end

// ====实现文件===== //
@interface MyClass()

@property(nonatomic,retain)NSString * propertyForPrivateMember;

@end

这个决定的自由会让我有点恼火,喜欢从各个来源找到一个确认如何应该做的事情。但是,我不能在苹果文档中找到这样严格的声明,所以请发布一个链接苹果文档(如果存在),或任何其他理论清除。

解决方案

通过使用类扩展可以拥有私有属性。



类扩展语法很简单:



在.m文件中有类,创建一个未命名的类别:



.h

  @interface OverlayViewController:UIViewController< VSClickWheelViewDelegate> 
- (IBAction)moreButtonClicked:(id)sender;
- (IBAction)cancelButtonClicked:(id)sender;
@end




$ b #importOverlayViewController.h

@interface OverlayViewController()
@property(nonatomic)NSInteger amount;
@property(retain,nonatomic)NSArray * colors;
@end

@implementation OverlayViewController
@synthesize amount = amount_;
@synthesize colors = colors_;

// ...

@end

现在你拥有私人会员的所有方面的财产,而不暴露给公众。应该没有合成属性到写入的getter / setter的开销,因为编译器将在编译时创建更多或更少的相同。



请注意,此代码使用合成的ivar。在标题中不需要ivar声明。



有一个不错的 cocoawithlove关于此方法的文章



您还要问为什么要使用属性作为私有ivars。有几个好的原因:





编辑

由于LLVM 3,

  @interface OverlayViewController(){
NSInteger amount;
NSArray * colors;
}
@end

甚至在实现块

  @implementation OverlayViewController {
NSInteger amount;
NSArray * colors;
}
// ...
@end

请参阅WWDC2011 :Session 322 - Objective-C Advancements in Depth(〜03:00)


I had some discussion related to the use of properties and instance variables at work, therefore I would like to find a wiki answer for that. Now, I know there's no real private member type in objective-c, everything is pretty much public. However, I'm a little bit concerned about the way we should design our classes and also to comply to OOP principles. I would like to hear opinions of these three design approaches:

A. According to various post and even to a new Stanford university iPhone development courses, you should always use properties everywhere you can. However IMHO, this approach brakes OOP design principles because in this case, all members become public. Why do I need to publish all my internal/local instance variables to outside? Also, there's some very little (but still) overhead if you use synthesized setters via properties, instead using local ivar directly. Here's a sample:

//==== header file =====//
@interface MyClass : NSObject

@property (nonatomic, retain) NSString *publicMemberWithProperty;
@property (nonatomic, retain) NSString *propertyForPrivateMember;

@end

B. Another approach is to declare ivars in header file (without declaring relative properties) for private members, and in the same header file, to declare pure properties (without declaring relative ivars) for public members. In such case, ivars would be used directly in the class. This approach makes sense but not uses all benefits from properties because we have manually to release old values before setting the new ones. Here's a sample:

//==== header file =====//
@interface MyClass : NSObject{
  NSString *_privateMember;
}

@property (nonatomic, retain) NSString *publicMemberWithProperty;

@end

C. To declare pure properties (without declaring relative ivars) for public members in header file, and to declare pure properties (without declaring relative ivars) for private members in private interface in implementation file. This approach IMHO is more clear than the first one, but the same question remains: why do we have to have properties for internal/local members? Here's a sample:

//==== header file =====//
@interface MyClass : NSObject

@property (nonatomic, retain) NSString *publicMemberWithProperty;

@end

//==== implementation file =====//
@interface MyClass()

@property (nonatomic, retain) NSString *propertyForPrivateMember;

@end

This decision freedom annoys me a little bit and I would like to find a confirmation from respective sources about how things should be done. However, I was unable to find such strict statements in Apple docs on that, so please post a link to apple docs if any exists, or to any other theory that clears that.

解决方案

By using class extensions you can have private properties.

A class extension syntax is simple:

Inside the .m-file, that has the class, create a unnamed category:

.h

@interface OverlayViewController : UIViewController <VSClickWheelViewDelegate>
- (IBAction)moreButtonClicked:(id)sender;
- (IBAction)cancelButtonClicked:(id)sender;
@end

.m

#import "OverlayViewController.h"

@interface OverlayViewController ()
@property(nonatomic) NSInteger amount;
@property(retain,nonatomic)NSArray *colors;
@end

@implementation OverlayViewController
@synthesize amount = amount_;
@synthesize colors = colors_;

//…

@end

Now you got all the aspects of properties for private members, without exposing them to public. There should be no overhead to synthesized properties to written getter/setters, as the compiler will create more or less the same at compile time.

Note that this code uses synthesized ivars. No ivar declaration in the header is needed.

There is a nice cocoawithlove article, about this approach.

You also ask why to use properties for private ivars. There are several good reasons:

edit
Since LLVM 3 it is also possible, to declare ivars in class extensions

@interface OverlayViewController (){
    NSInteger amount;
    NSArray *colors;
}
@end

or even at the implementation block

@implementation OverlayViewController{
    NSInteger amount;
    NSArray *colors;
}
//…
@end

see "WWDC2011: Session 322 - Objective-C Advancements in Depth" (~03:00)

这篇关于私人和公共成员应如何实现目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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