gpt4 book ai didi

objective-c - IBOutlets、实例变量和属性 : Best Practices

转载 作者:IT王子 更新时间:2023-10-28 23:37:48 27 4
gpt4 key购买 nike

我今天对关于声明 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];

}

最佳答案

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

  1. 调用 alloc/init 会创建一个保留计数为 1 的对象。您的合成属性也会保留该对象,从而在释放该对象时导致内存泄漏(除非您立即释放您的属性,但这是不好的形式) .最好在单独的行上分配/释放对象。

  2. 点表示法实际上与调用 [self setObject:obj] 相同。不使用点符号直接访问底层实例变量。在 initdealloc 中,始终直接访问实例变量,因为访问器方法可能包含在对象正在执行时无效的额外操作(例如键值观察通知)创建或销毁。所有其他时间都使用综合访问器方法。即使您现在没有做任何特别的事情,您也可以稍后覆盖这些方法以更改设置变量时发生的情况。

  3. 标量的工作方式相同,只是您不必担心内存问题。

  4. 一个访问合成的访问器方法,另一个直接访问实例变量。再看问题一和二,小心内存泄漏!

  5. View Controller 可能会再次被推送到屏幕上,在这种情况下,您的 viewDidLoad 方法将被第二次调用。如果您在 viewDidLoad 中设置初始值,请继续在此处将您的属性设置为 nil。这对于使用大量内存且不会影响 View 状态的属性是有意义的。另一方面,如果您希望该属性持续存在直到您确定不再需要它,请在您的 init 方法中创建它,并且在 dealloc 之前不要释放它。

关于objective-c - IBOutlets、实例变量和属性 : Best Practices,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6113755/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com