Best way to define private methods for a class in Objective-C

后端 未结 12 1926
小鲜肉
小鲜肉 2020-11-22 14:34

I just started programming Objective-C and, having a background in Java, wonder how people writing Objective-C programs deal with private methods.

I understand there

12条回答
  •  旧巷少年郎
    2020-11-22 15:23

    every objects in Objective C conform to NSObject protocol, which holds onto the performSelector: method. I was also previously looking for a way to create some "helper or private" methods that I did not need exposed on a public level. If you want to create a private method with no overhead and not having to define it in your header file then give this a shot...

    define the your method with a similar signature as the code below...

    -(void)myHelperMethod: (id) sender{
         // code here...
    }
    

    then when you need to reference the method simply call it as a selector...

    [self performSelector:@selector(myHelperMethod:)];
    

    this line of code will invoke the method you created and not have an annoying warning about not having it defined in the header file.

提交回复
热议问题