How a class extension works as a means of implemen

2019-05-26 10:26发布

I believe a popular way to declare "private methods" in Objective-C is to create its class extension and declare methods that you would like to make as private.

I would like to know more in detail on how an class extension makes the methods work as private.

  • Update: I asked this question with the term empty category which is incorrect. I now changed it as class extension

2条回答
Evening l夕情丶
2楼-- · 2019-05-26 11:01

That's because you create your empty category in your implementation file, not your header file so other classes can't access it.


//TestClass.h

@interface TestClass : NSObject 
{
}

-(void)publicMethod;

@end

//TestClass.m

@interface TestClass()

-(void)privateMethod;

@end

@implementation TestClass

-(void)publicMethod
{
NSLog (@"public");
}

-(void)privateMethod
{
NSLog (@"private");
}

@end


查看更多
姐就是有狂的资本
3楼-- · 2019-05-26 11:04

That's not an "empty category", it's a class extension. Read Bbum's explanation of them at the link I provided.

查看更多
登录 后发表回答