问题
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 my "own convention" to use from now on while coding in Objective-C.
The convention that I want to figure out is ultimately how to (using best coding practices for production level code) use private methods in a class. Coming from a background in C#, when I write classes, usually there is a block of code that is repeated in multiple public methods (such as error checking, or WCF service connection setup). I usually create one block of this code and put it in a private method for only these public methods to access. This way if I need to make a change, I only need to do it in one spot, as opposed to 10 different places in a class, but then never giving users the ability to call this private method. For example:
public Class A
{
public void method1()
{
doErrorChecking()
// Do more stuff
}
public void method2()
{
doErrorChecking()
// Do more stuff
}
private doErrorChecking() { //Error Checking Code}
}
I understand that there is no real way to truly make that last method private in Objective-C, but just really want to make sure that when I create all future classes in Objective-C for iOS development I'm following the best practice available so future code refactoring on this matter won't be needed (hopefully). I've noticed people talking about categories, others just don't put the method in the @interface file, and others use extension methods. At the moment I'm just putting the method implementation in the @implementation file, but not the interface file. I'm also making the "wannabe" private method have a really distinct name so that sub-classing or overwriting methods is not an issue. Is this the path I should be following? Or for these particular scenarios is there a better way to do it?
回答1:
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 — apparently Apple reserves this for themselves, so you should pick a different prefix. The language doesn't enforce anything._
, though not everyone does
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/6929300/proper-objective-c-helper-wannabe-private-methods