菜鸟教程小白 发表于 2022-12-12 22:57:36

ios - 使用@property ivars 的最佳实践


                                            <p><p>有人可以分享一些关于在初始化方法或指定初始化程序中使用@property iVars 的最佳实践/代码约定的知识吗?</p>

<p>请看我的例子:</p>

<pre><code>@interface MyClass ()
@property(nonatomic,strong) nsstring *tempString;
@property(nonatomic,strong) NSMutableArray *arrItems;
@end

@implementation ViewController

- (id)init
{
    if (self = ) {

      //Is this best practice / correct
      _tempString = @&#34;&#34;;
      _arrItems = [ initWithCapacity:0];
      ...
      ...

      //Or this
      self.tempString = @&#34;&#34;;
      self.arrItems = [ initWithCapacity:0];
    }
    return self;
}

@end
</code></pre>

<p>关于为什么应该使用其中一个或另一个的任何建议?</p>

<p>谢谢...</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>Apple 关于此主题的指南包含在恰当命名的部分 <a href="https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmPractical.html#//apple_ref/doc/uid/TP40004447-SW6" rel="noreferrer noopener nofollow">Don’t Use Accessor Methods in Initializer Methods and dealloc</a> 中.</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 使用@property ivars 的最佳实践,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/12752946/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/12752946/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 使用@property ivars 的最佳实践