菜鸟教程小白 发表于 2022-12-11 20:51:36

objective-c - 如何在objective-c中为其他类变量赋值


                                            <p><p>下面的代码正在运行,我可以在第二个屏幕中看到这些值。但是我在其他具有这种格式的不同变量的类中使用相同的方法。但是,如果在我用点键入类名之后,它不会向我显示变量。我想不通。有没有办法将值传递给其他类。</p>

<pre><code>      InstallProfiler_2 *installProfiler2 = [ initWithNibName:@&#34;InstallProfiler_2&#34; bundle:nil];

      installProfiler2.profilerType2 = profilerType;

      ;
      ;
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>确保:</p>

<ul>
<li>您已导入类头。</li>
<li><code>@property</code> 声明位于此 header 中,而不是类扩展。</li>
</ul>

<p><code>@property</code> 指的是 ivars 所以当你说 </p>

<blockquote>
<p>if after i type the classname with a dot</p>
</blockquote>

<p>这个术语不正确,您可能是指在您开始输入指向类实例的变量名称之后。</p>

<p><strong>ClassA.h</strong> </p>

<pre><code>@interface ClassA : NSObject

@property (nonatomic, weak) NSInteger myInt;

@end
</code></pre>

<p><strong>ClassA.m</strong> </p>

<pre><code>@implementation ClassA

@synthesize myInt = _myInt;

@end
</code></pre>

<p><strong>ClassB.m</strong> </p>

<pre><code>#import &#34;ClassA.h&#34;   // &lt;- Import the header of the class

@ implementation ClassB

//.. other methods and stuff

- (void)myMethod;
{
   ClassA *instanceOfClassA = [ init];   // &lt;- Working with an instance not a class
   instanceOfClassA.myInt = 1;
}

@end
</code></pre>

<p><strong>更新</strong> </p>

<p>确保您的 <code>@property ()</code> 在圆括号之间没有 <code>readonly</code>。
还要确保您在实现中具有 <code>@synthesize</code> 的 ivar,或者为 ivar 同时提供了 getter 和 setter。</p>

<p>未能显示一些相关代码,因此我们实际上可以看到您在做什么 - 我们在这里非常盲目地回答。</p></p>
                                   
                                                <p style="font-size: 20px;">关于objective-c - 如何在objective-c中为其他类变量赋值,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/8729258/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/8729258/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: objective-c - 如何在objective-c中为其他类变量赋值