Proper Objective-C Helper “Wannabe” Private methods?

前端 未结 3 472
南旧
南旧 2021-02-06 18:01

While I hate to beat a horse to death on this subject (I\'ve read through various articles about this), but would just like to get more opinions on this matter before I create m

相关标签:
3条回答
  • 2021-02-06 18:22

    I would use a class extension, definitely. In the implementation file, include something like this above your @implementation:

    @interface A ()
    - (void) doErrorChecking;
    @end
    

    Then use the method in code as needed. Although due to objective-c's dynamic nature no method is truly private, this will obscure the method from your interface file while still technically including it in your own 'private' interface. In general, keep your .h file for methods and properties that are ok for public use, while limiting private use methods and properties to a class extension in the implementation file.

    0 讨论(0)
  • 2021-02-06 18:26

    Yes, it's perfectly reasonable to want to extract your functionality out into another method. The best way to do this in my opinion is using a class continuation, which you can put your private method declarations in. It can go above your @implementation block in your .m file, so it's not in the public header.

    @interface MyClass ()
    - (void)_privateMethod:(id)arg;
    @end
    

    The difference between a class continuation and a normal category (such as @interface MyClass (PrivateMethods)) is that the compiler will require you to implement the methods in your main @implementation block, rather than having a separate @implementation MyClass (PrivateMethods) block. This is arguably desirable when implementing helper methods like you described.

    In terms of naming, it's relatively common to start private method names (and ivar names, for that matter) with an _, though not everyone does — apparently Apple reserves this for themselves, so you should pick a different prefix. The language doesn't enforce anything.

    0 讨论(0)
  • 2021-02-06 18:27

    If you just need a reusable set of code that absolutely cannot be overridden by a subclass, you could just make a regular C function instead of a method. If the function is declared within the scope of the class @implementation block, it can still get access to all the private ivars of the object. You'd need to pass in a pointer to self, though, since a function isn't bound to a particular object

    So it would look like this:

    static BOOL isInValidState(MyClass *);
    
    @implementation MyClass
    
    static BOOL isInValidState(MyClass *self) {
        if (self->somePrivateIvar == nil) {
            return NO;
        }
    
        if ([self->someString isEqualToString:@"pigsAreFlying"]) {
            return NO;
        }
    
        return YES;
    }
    
    - (void)method1 {
        if (isInValidState(self) == NO) {
            return;
        }
        // Do whatever method 1 does
    }
    
    - (void)method2 {
        if (isInValidState(self) == NO) {
            return;
        }
        // Do whatever method 2 does
    }
    
    @end
    

    Since functions are not part of the method list of a class, this error checking method cannot ever be overridden. Since we declared it static, it is only accessible within the scope of this file, which means that it's effectively private; it cannot be called by an object of any other class.

    0 讨论(0)
提交回复
热议问题