objective-c – IBOutlets,实例变量和属性:最佳实践

前端之家收集整理的这篇文章主要介绍了objective-c – IBOutlets,实例变量和属性:最佳实践前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
关于声明IBOutlets和实例变量,管理它们,使用正确的访问器并正确释放它们,我今天已经完成了各种关于最佳实践的研究.我几乎就在那里,但我有一些利基问题,希望有人能够提出最佳实践建议.我将它们格式化为代码并对问题进行评论,以便更容易理解.我已经排除了一些我认为不相关的明显部分,可以安全地假设它们起作用(比如预处理器的东西,@ end,必需的实现方法等).

MyViewController.h

@class OtherViewController;

@interface MyViewController : UIViewController {

     NSString *_myString;
     BOOL _myBOOL;

}

// The first two properties aren't declared in the interface
// above as per best practices when compiling with LLVM 2.0

@property (nonatomic,retain) OtherViewController *otherViewController;
@property (nonatomic,retain) UIButton *myButton;
@property (nonatomic,copy) NSString *myString;
@property (readwrite) BOOL myBOOL;

MyViewController.m

@implementation MyViewController

// Synthesizing IBOutlets on iOS will cause them to be
// retained when they are created by the nib

@synthesize otherViewController;
@synthesize myButton;

// Assign instance variables so as to force compiler
// warnings when not using self.variable

@synthesize myString = _myString;
@synthesize myBOOL = _myBOOL;

- (void)viewDidLoad {

     // QUESTIONS:

     // 1. Ignoring convenience methods,can you still alloc and init in dot notation
     //    even when it's being properly synthesized?

     self.myString = [[NSString alloc] initWithString:@"myString"];
     self.myString = existingNSStringObject;

     // 2. Should you always call methods for IBOutlets and instance variables using dot notation?
     //    Is there any difference seeing as these aren't directly invoking setters/getters?

     [self.myButton setText:self.myString];
     [myButton setText:self.myString];

     [self.otherViewController.view addSubview:mySubview];
     [otherViewController.view addSubview:mySubview];

     [self.myButton setAlpha:0.1f];
     [myButton setAlpha:0.1f];
     self.myButton.alpha = 0.1f;
     myButton.alpha = 0.1f;

     // 3. How fussy are scalar variables in terms of getters and setters,//    given that there is a @synthesize declaration for them?

     self.myBOOL = YES;
     myBOOL = NO;

     if(self.myBOOL) { ... }
     if(myBOOL) { ... }

     // 4. On instantiation of new view controllers from NIBs,should you use
     //    dot notation? (I haven't been doing this prevIoUsly).

     otherViewController = [[OtherViewController alloc] initWithNibName:@"OtherView" bundle:nil];
     self.otherViewController = [[OtherViewController alloc] ... ]

}

- (void)viewDidUnload {

     // 5. Best practice states that you nil-value retained IBOutlets in viewDidUnload
     //    Should you also nil-value the other instance variables in here?

     self.otherViewController = nil;
     self.myButton = nil;

     self.myString = nil;

}

- (void)dealloc {

     [otherViewController release];
     [myButton release];
     [_myString release];   

}

解决方法

我总是声明并显式设置属性的底层实例变量.这是前面的一点工作,但在我看来,明确区分变量和属性并一目了然地看一个类有哪些实例变量是值得的.我还为实例变量名称添加前缀,因此如果我不小心输入属性而不是object.property,编译器会抱怨.

>调用alloc / init会创建一个保留计数为1的对象.您的合成属性也将保留该对象,在释放时导致内存泄漏(除非您在之后释放您的属性,但这是不好的形式).最好在单独的行上分配/释放对象.>点符号实际上与调用[self setObject:obj]相同.不使用点表示法直接访问底层实例变量.在init和dealloc中,始终直接访问实例变量,因为访问器方法可以包括在创建或销毁对象时无效的额外操作(例如键值观察通知).所有其他时间使用合成的访问器方法.即使您现在没有做任何特殊操作,稍后您可能会稍后重写这些方法以更改设置变量时发生的情况.> Scalars以同样的方式工作,只有你不必担心内存.>一个访问合成的访问器方法,另一个访问实例变量.再看第一和第二个问题,并注意内存泄漏!>视图控制器可能会再次被推到屏幕上,在这种情况下,您的viewDidLoad方法将再次被调用.如果您在viewDidLoad中设置初始值,请继续并在此处将属性设置为nil.这对于使用大量内存且不会影响视图状态的属性有意义.另一方面,如果您希望属性保持不变直到您确定不再需要该属性,请在init方法中创建它,并且在dealloc之前不要释放它.

原文链接:https://www.f2er.com/c/118753.html

猜你在找的C&C++相关文章