IBOutlets,实例变量和属性:最佳实践

Whe*_*rdy 7 memory properties objective-c ios

关于声明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;
Run Code Online (Sandbox Code Playgroud)

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];   

}
Run Code Online (Sandbox Code Playgroud)

Dan*_*lea 1

    \n
  1. 点表示法和括号表示法几乎相同。
  2. \n
  3. 您可以self.myVariable访问实例变量属性的 getter myVariable,也myVariable可以访问局部变量。它们不是同一件事。
  4. \n
  5. 您可以通过重写方法并为其指定一些特定条件来自定义 setter 和 getter。
  6. \n
  7. 请参阅第一个答案(最好使用括号 - 更好地理解代码)
  8. \n
  9. 最好制定一个单独的方法。
  10. \n
\n\n

喜欢:

\n\n
- (void) releaseOutlets {\n self.firstOutlet = nil;\n self.mySecondOutlet = nil;\n \xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6\xe2\x80\xa6\n self.myLastOutlet = nil;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

然后在方法中viewDidUnloaddealloc方法中调用此方法。

\n\n

希望能帮助到你 !

\n