在 header 文件中声明私有变量与在 class 扩展中声明变量
Declaring private variables in header file vs declaring variables in class extension
在 header 文件中声明一个 @private ivar 与在 class 扩展中声明相同的 ivar 而没有 @private 有什么区别?据我了解,这是一回事。
此外,您可以在 header 中声明一个私有的 属性 吗?
概念是在头文件中只声明那些 public 的东西(方法、属性等)。在实现文件的 class 扩展名中声明所有私有项。
这仅向 class 用户提供可供他们使用的信息,并隐藏所有其他信息。它还使 class 的用户更容易快速查看他可用的功能。编写代码的关键在于开发人员的可读性和可理解性。
这样开发人员可以自由更改头文件中未公开的任何内容,而无需进行任何外部可见的更改。
在 Objective 的最新版本中,这最终可以通过 class 扩展完全发布。
What's the difference between declaring a @private ivar in the header file and declaring the same ivar in the class extension without @private?
有一些不同。总之,头文件中声明的变量对子classes和class类是可见的。实现中声明的变量不是。
1) 在 class 的主 @interface
块中声明的实例变量可用于外部 class 类别或扩展,即使这些变量声明为 @private
.例如:
// YourClass.h
@interface YourClass : NSObject {
@private
int _yourPrivateIVar;
}
@end
// MyExtension.m
@implementation YourClass(MyExtension)
- (void)reset { _yourPrivateIVar = 0; } // This is allowed.
@end
实现中声明的实例变量不可用于外部 class 类别。
2) 基础 class 及其子 class 不能在它们的 @interface
中都声明相同的 ivar,即使两个 ivar 都是 @private
。例如,这是不允许的:
@interface Base : NSObject
{
@private
int _foo;
}
@end
@interface Subclass : Base
{
@private
int _foo; // Error: Duplicate member _foo
}
@end
如果两个 ivar 都在 class 扩展或实现块中声明,那么它不仅可以编译,而且可以按预期工作:两个 classes 都有自己单独的 _foo
ivar彼此不冲突。换句话说,这两个变量都是真正私有且独立的:
@implementation Base {
int _foo;
}
@end
@implementation Subclass {
int _foo;
}
- (void)reset { _foo = 123; } // Does not affect base class's _foo
@end
注意:如果基础 class 和 subclass 声明了一个 "private" 属性 或同名的方法,它将在没有警告或错误的情况下编译,但它由于两个 classes 都在不知不觉中干扰了彼此的私有数据,因此在运行时将失败。
在 header 文件中声明一个 @private ivar 与在 class 扩展中声明相同的 ivar 而没有 @private 有什么区别?据我了解,这是一回事。
此外,您可以在 header 中声明一个私有的 属性 吗?
概念是在头文件中只声明那些 public 的东西(方法、属性等)。在实现文件的 class 扩展名中声明所有私有项。
这仅向 class 用户提供可供他们使用的信息,并隐藏所有其他信息。它还使 class 的用户更容易快速查看他可用的功能。编写代码的关键在于开发人员的可读性和可理解性。
这样开发人员可以自由更改头文件中未公开的任何内容,而无需进行任何外部可见的更改。
在 Objective 的最新版本中,这最终可以通过 class 扩展完全发布。
What's the difference between declaring a @private ivar in the header file and declaring the same ivar in the class extension without @private?
有一些不同。总之,头文件中声明的变量对子classes和class类是可见的。实现中声明的变量不是。
1) 在 class 的主 @interface
块中声明的实例变量可用于外部 class 类别或扩展,即使这些变量声明为 @private
.例如:
// YourClass.h
@interface YourClass : NSObject {
@private
int _yourPrivateIVar;
}
@end
// MyExtension.m
@implementation YourClass(MyExtension)
- (void)reset { _yourPrivateIVar = 0; } // This is allowed.
@end
实现中声明的实例变量不可用于外部 class 类别。
2) 基础 class 及其子 class 不能在它们的 @interface
中都声明相同的 ivar,即使两个 ivar 都是 @private
。例如,这是不允许的:
@interface Base : NSObject
{
@private
int _foo;
}
@end
@interface Subclass : Base
{
@private
int _foo; // Error: Duplicate member _foo
}
@end
如果两个 ivar 都在 class 扩展或实现块中声明,那么它不仅可以编译,而且可以按预期工作:两个 classes 都有自己单独的 _foo
ivar彼此不冲突。换句话说,这两个变量都是真正私有且独立的:
@implementation Base {
int _foo;
}
@end
@implementation Subclass {
int _foo;
}
- (void)reset { _foo = 123; } // Does not affect base class's _foo
@end
注意:如果基础 class 和 subclass 声明了一个 "private" 属性 或同名的方法,它将在没有警告或错误的情况下编译,但它由于两个 classes 都在不知不觉中干扰了彼此的私有数据,因此在运行时将失败。