Sun*_*day 6 class-design interface objective-c
我注意到了@interfaceObjective-c类的各种声明.我想了解为什么开发人员@interface通过以下方式声明:
// in the .h file
@interface MyClass : NSObject
// ...
@end
// in the .m file (what's the purpose of the parens?)
@interface MyClass ()
// more property declarations which seem like they can go in the .h file
@end
// again in the .m file (what's the purpose of private?)
@interface MyClass (Private)
// some method declarations
@end
Run Code Online (Sandbox Code Playgroud)
这只是一个普通的类接口,继承自NSObject,您可以在其中声明ivars,属性和方法
// in the .h file
@interface MyClass : NSObject
// ...
@end
Run Code Online (Sandbox Code Playgroud)
以下两个是类别,允许您向类添加方法.但它不是子类(不要声明具有相同名称的方法,因为您将无法访问原始方法).如果你有一个命名的接口类别(比如@interface MyClass (Private)),那么应该提供实现@implementation MyClass (Private),对于未命名的类别(也称为扩展),可以像往常一样提供实现.请注意,扩展还允许您将ivars添加到类中,而(命名)类别则不允许.
// in the .m file (what's the purpose of the parens?)
@interface MyClass ()
// more property declarations which seem like they can go in the .h file
@end
// again in the .m file (what's the purpose of private?)
@interface MyClass (Private)
// some method declarations
@end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1761 次 |
| 最近记录: |