gpt4 book ai didi

objective-c - cocoa /ObjC : Are there any performance drawbacks to my style of allocing objects for property iVars?

转载 作者:行者123 更新时间:2023-12-03 17:00:07 25 4
gpt4 key购买 nike

每当我的类需要 iVar 时,我都会使用属性。对于保留的属性,我已经习惯了使用访问器方法来分配/初始化实际 iVar 的特定方式:

- (void)anInitOrAccessorMethod
{
self.property = [[AClass alloc] init];
[self.property release];
}

每当我需要设置一个非自动释放的 iVar 时,我都会使用上述设置属性的方式(保留对象),然后向其发送一条释放消息以抵消分配+保留。这与我看到的许多社区代码示例形成鲜明对比,在这些示例中,如果使用 alloc/create 创建对象,人们只需直接设置 iVar,从而消除了额外的释放。

除了额外的代码开销之外,我的风格是否存在任何性能缺陷?

最佳答案

不要这样做。如果您的属性被声明为具有copy语义,您的代码将泄漏第一个对象并过度释放第二个对象。

关于objective-c - cocoa /ObjC : Are there any performance drawbacks to my style of allocing objects for property iVars?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3324263/

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