2

Is there a naming convention for protocols and concrete implementations of them in Objective-C? Specifically regarding a protocol that's used as an abstract 'service' or 'manager' class declaration.

For example, in Java you might have an interface SomeService to describe some data service and call its implementing class SomeServiceImpl (or ISomeService and its implementing class SomeService).

If I have a protocol @protocol SomeService <NSObject>, is there a naming convention for the class that implements said protocol?
e.g. @interface SomeServiceImpl : NSObject <SomeService>

1

Coding Guidelines for Cocoa and Objective-C style guide is a great resource for answering any naming convention questions. My answer is as much as possible based off of this.

Init Method

The init method looks good.

- (id) initWithName:(NSString *) name;

Class Method

The class method looks good.

+ (NSString *) aliasForName:(NSString *) name

Class methods can also be used to instantiate an instance of an object. In this instance, Apple's API's generally have the method start with the name of the class like UIButton's buttonWithType: method that has the signature:

+ (id)buttonWithType:(UIButtonType)buttonType

Instance Methods

Good resource for coding conventions for methods can be found under General Rules.

The following method should drop the ands:

- (void) methodWithApple:(NSString *) apple andOrange:(NSString *) orange andMango:(NSString *) mango  // BAD

Don’t use “and” to link keywords that are attributes of the receiver.

- (int)runModalForDirectory:(NSString *)path file:(NSString *) name types:(NSArray *)fileTypes; //RIGHT 

- (int)runModalForDirectory:(NSString *)path andFile:(NSString *)name andTypes:(NSArray *)fileTypes; //WRONG

The signature should look more like the following:

    - (void) methodWithApple:(NSString*)apple orange:(NSString*)orange
    mango:(NSString*)mango  // GOOD

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.