在@implementation 中添加 ivars [英] Add ivars in @implementation
问题描述
为了更好的封装,体面的 Objective-C
程序员将他们的 private
ivars 放在主实现文件中声明的私有 extension
中,像这样:
For good encapsulation, decent Objective-C
programmers put their private
ivars in a private extension
declared in the main implementation file, like this:
// MyClass.m
@interface MyClass () {
float value;
}
@end
@implementation MyClass
@end
但最近,我发现了一种更简单的方法来隐藏 private
ivars:可以在 @implementation
之后的 {}
块中声明 ivars,像这样:
But recently, I found a simpler way to hide private
ivars: ivars can be declared in a {}
block following @implementation
, like this:
// MyClass.m
@implementation MyClass {
float value;
}
@end
当不需要隐藏私有方法而只需要隐藏 private
ivars 时,这真的很方便.
It is really handy when no private methods but only private
ivars need to be hidden.
但是,我不确定它的语法有效性.任何人都可以使用一些规范引用来验证或使其无效吗?
However, I'm not sure about its syntax validity. Can anyone validate or invalidate it with some canonical references?
推荐答案
这是完全有效的,这里是 Apple 的文档:
It's perfectly valid and here is a document by Apple talking about it:
我个人不使用它,因为我更喜欢类延续类别的语法.
I don't personally use it as I prefer the syntax of a class continuation category.
这篇关于在@implementation 中添加 ivars的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!