1. 私有方法
假设现有的类为MyClass: 在类的头文件代码为: @interface MyClass { // 添加变量 } - (void)PublicMethod;//公共方法,可以被继承类继承 @end 而在类的.m文件中,采用类别来实现私有方法,具体操作为: @interface MyClass()//注意(),即定义一个空类别 - (void)PrivateMethod;//在类别中定义私有方法 @end @implementation MyClass - (void)PublicMethod { // I共有方法实现代码 } - (void)PrivateMethod { // 私有方法代码 } @end 至此,私有方法实现完成。
2.定义私有变量
@interface RadioViewController (){@private UIBackgroundTaskIdentifier task;}@property (strong, nonatomic) AVPlayer *audioPlayer;@end
请注意,在m文件的categories需要使用花括号({})@property还是需要定义在花括号的外面。
这样在
@implementation RadioViewController
@end
中间就可以自由的使用这个成员变量(field)task了。