I am a newbie in Objective-c and I would like to implement fluent interface pattern in my OC class. Here is my updated and simplified case from my project:

// .h file @interface MyLogger : NSObject { ... } - (MyLogger*) indent:(BOOL)indent; - (MyLogger*) debug:(NSString*)message, ...; - (id) warning:(NSString*)message, ...; .... @end // .m file @implement MyLogger { - (MyLogger*) indent:(BOOL)indent { // some codes to set indent or unindent return self; // I think it should return [self autorelease]; } - (MyLogger*) debug:(NSString*)message, ... { // build message and log the message return [self autorelease]; } - (id) warning:(NSString*)message, ... { // similar as above, but log a warning message return self; } //. usage in another .m -(id) initAnotherClass { if (self = [supper init]) { // ... // instance was defined as MyLogger in .h as class var instance = [[[MyLogger alloc] initWithContext:@"AnotherClassName"] retain]; //... } return self; } -(void)method1 { [[instance debug:@"method1"] indent:YES]; ... [instance warning:@"some debug message with obj: %@", var]; ... [[instance indent:NO] debug:@"method1 DONE"]; } // in my Xcode output I'll see debug output like [date time] [app id] [DEBUG] AnotherClassName - method1 [date time] [app id] [WARNING] AnotherClassName - some debug message with obj: ... [date time] [app id] [DEBUG] AnotherClassName - method1 DONE

Here in indent , I return self, while in debug : I return [self autorelease] . It works fine if I only return self like in debug . However, I think I should always return in the same way as I did in debug : in terms of OC memory management. Any suggestions?

Updated: I added another method warning with return type of id. Should I return self as id type or my class type in OC? It seems both works fine and there is no compile error or warning. I have seem Cocoa framework classes return id. For example, here are some methods in NSString.h

+ (id)string; + (id)stringWithString:(NSString *)string;

It seems that Cocoa has some FI pattern like methods. Should be id type better than the class itself?

Update: as Pat Wallace's suggestion, I am actually using this pattern in an iPhone project.