ios - Objective C - 如何测试私有(private)变量
<p><p>如何对隐藏变量 <em>aVar</em> 进行单元测试?</p>
<pre><code>// .h file
@interface Class: NSObject
@end
// .m file
@implementation Class{
id aVar
}
@end
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您可以将私有(private)变量移动到 <a href="https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html#//apple_ref/doc/uid/TP40011210-CH6-SW3" rel="noreferrer noopener nofollow">class extension</a> ,这使它们保持私有(private),但随后创建一个使它们公开的 -Private 类别。例如。</p>
<pre><code>// Class.h
@interface Class : NSObject
@end
// Class.m
@interface Class ()
@property (nonatomic, strong) id aVar;
@end
@implementation Class
@end
// Class+Private.h
@interface Class (Private)
@property (nonatomic, strong) id aVar;
@end
...
</code></pre>
<p>然后<em>仅</em>在您的单元测试目标中导入/编译 Class+Private。</p></p>
<p style="font-size: 20px;">关于ios - Objective C - 如何测试私有(private)变量,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/31975663/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/31975663/
</a>
</p>
页:
[1]