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

后端 未结 12 1927
小鲜肉
小鲜肉 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:07

    If you wanted to avoid the @interface block at the top you could always put the private declarations in another file MyClassPrivate.h not ideal but its not cluttering up the implementation.

    MyClass.h

    interface MyClass : NSObject {
     @private
      BOOL publicIvar_;
      BOOL privateIvar_;
    }
    
    @property (nonatomic, assign) BOOL publicIvar;
    //any other public methods. etc
    @end
    

    MyClassPrivate.h

    @interface MyClass ()
    
    @property (nonatomic, assign) BOOL privateIvar;
    //any other private methods etc.
    @end
    

    MyClass.m

    #import "MyClass.h"
    #import "MyClassPrivate.h"
    @implementation MyClass
    
    @synthesize privateIvar = privateIvar_;
    @synthesize publicIvar = publicIvar_;
    
    @end
    

提交回复
热议问题