gpt4 book ai didi

iphone - 在 Objective-C 中这有什么意义

转载 作者:行者123 更新时间:2023-12-03 20:20:01 24 4
gpt4 key购买 nike

SomeObject *temp = [[SomeObject alloc] init]
self.theObject = temp;
[temp release];

为什么总是这样?为什么不呢

self.theObject = [[SomeObject alloc] init];

最佳答案

如果 theObject 属性是保留属性,则第一种方法是正确的,因为它不会泄漏内存。它也比编写第二个版本的正确方法更有效,第二个版本是这样的:

self.theObject = [[[SomeObject alloc] init] autorelease];

每当您使用 alloc 创建对象时,您都负责以某种方式释放它,无论是通过 release 还是 autorelease

关于iphone - 在 Objective-C 中这有什么意义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1210713/

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