gpt4 book ai didi

ios - 查看 Controller /内存管理

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:18:50 28 4
gpt4 key购买 nike

我对 View Controller 中的内存管理有点困惑。

假设我有这样的头文件:

@interface MyController : UIViewController {
NSMutableArray *data;
}
@property (nonatomic, retain) NSMutableArray *data;
@end

.m 文件看起来像这样:

@implementation MyController
@synthesize data;

- (void)dealloc
{
[self.data release];
[super dealloc];
}

- (void)viewDidLoad
{
[super viewDidLoad];

if (self.data == nil)
self.data = [[NSMutableArray alloc] init];
}

- (void)viewDidUnload
{
[super viewDidUnload];
[self.data release];
self.data = nil;
}

从正确的内存管理角度来看可以吗?这会在通过内存警告解除分配后起作用吗?您如何在您的应用中做到这一点?

感谢您的回答;)

最佳答案

虽然 alloc-retain 调用在 viewDidLoadviewDidUnload 中平衡并且在内存方面应该没有问题,但它会更干净只取得一次所有权并放弃一次而不是两次。

- (void)viewDidLoad
{
[super viewDidLoad];

if (self.data == nil)
self.data = [NSMutableArray array];
}

- (void)viewDidUnload
{
[super viewDidUnload];

self.data = nil;
}

关于ios - 查看 Controller /内存管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6431546/

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