菜鸟教程小白 发表于 2022-12-12 18:21:25

objective-c - 为什么不定义 UIWindow *_window 就可以访问 _window?


                                            <p><p>这是一个后续问题:<a href="https://stackoverflow.com/questions/5466496/why-rename-synthesized-properties-in-ios-with-leading-underscores" rel="noreferrer noopener nofollow">Why rename synthesized properties in iOS with leading underscores?</a> </p>

<p>在 <code>MyDelegate.h</code></p>

<pre><code>#import &lt;UIKit/UIKit.h&gt;

@interface MyAppDelegate

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) MyMainViewController *mainViewController;

@end
</code></pre>

<p>在 <code>MyDelegate.m</code></p>

<pre><code>#import &#34;MyAppDelegate.h&#34;

@implementation MyAppDelegate

@synthesize window = _window;
@synthesize mainViewController = _mainViewController;
</code></pre>

<p>除了 <a href="https://stackoverflow.com/questions/5466496/why-rename-synthesized-properties-in-ios-with-leading-underscores" rel="noreferrer noopener nofollow">original question</a> 中解释的内容外关于 <code>_</code> 前缀的好处,我想知道为什么 <code>_window</code> 是可访问的</p>

<pre><code>@synthesize window = _window;
</code></pre>

<p>在第一次使用之前没有在任何地方定义?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这既是声明又是定义。 <code>@synthesize</code> 指令能够创建访问器方法和实例变量。来自 TOCPL,<a href="http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17-SW9" rel="noreferrer noopener nofollow">&#34;Declared Properties: Property Implementation Directives&#34;</a> :</p>

<blockquote>
<p>You can use the form <code>property=ivar</code> to indicate that a particular instance variable should be used for the property...<br/>
For the modern runtimes (see <a href="http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtVersionsPlatforms.html#//apple_ref/doc/uid/TP40008048-CH106" rel="noreferrer noopener nofollow">“Runtime Versions and Platforms”</a> in Objective-C Runtime Programming Guide), instance variables are <strong>synthesized as needed.</strong> If an instance variable of the same name already exists, it is used.</p>
</blockquote>

<p>所以如果你已经有一个 ivar 的声明,</p>

<pre><code>@interface MyAppDelegate : NSObject
{
    NSWindow * _window;
}
</code></pre>

<p>将被使用(这在“旧版”平台中是必需的)。否则,它由 <code>@synthesize</code> 指令创建。在任何一种情况下,类中的结果都是相同的,尽管应该注意,合成变量的创建就像它们是用 <code>@private</code> 指令声明的一样;子类可以访问该属性,但不能直接访问 ivar。在独一无二的 Cocoa With Love 进行更多调查:<a href="http://cocoawithlove.com/2010/03/dynamic-ivars-solving-fragile-base.html" rel="noreferrer noopener nofollow">Dynamic ivars: solving a fragile base class problem</a> .</p></p>
                                   
                                                <p style="font-size: 20px;">关于objective-c - 为什么不定义 UIWindow *_window 就可以访问 _window?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/8322936/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/8322936/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: objective-c - 为什么不定义 UIWindow *_window 就可以访问 _window?