0

I've seen several Obj-C tutorials. The delegate classes all inherit from NSObject. For example, the applicationDidFinishLaunching delegate method, in some tutorials, it inherited from NSObject but NSApplication to implement it. The reason I don't think it should inherited from NSObject is that I didn't find any delegate protocol declaration in it, but I found that delegate protocol declaration in NSApplication. My Objective-C toy environment is GnuSep.

Here is some code:

@interface browserController : NSObject //here. inheriting from NSObject,but NSObject don'have any protocols declaration about applicationDidFinishLaunching.
{
  NSBrowser *browser;
}
@end

@implementation browserController

- (void)menuAction:menuItem
{
  ..............................

}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
  NSWindow *win;
  ActiveBrowserDelegate * abd;
  WindowDelegate *wd;
  NSRect wf = {{100, 100}, {600, 500}};
  NSRect bf = {{10, 10}, {580, 350}};

  .............................
}
Monolo
  • 18,205
  • 17
  • 69
  • 103
hao haochao
  • 91
  • 1
  • 8

2 Answers2

1

It is called informal protocol (though GNUstep declared it anyway as GSAppDelegateProtocol for documentation purpose) NSApplication will simply check it at runtime if your delegate object will respond to the message, (using -respondsToSelector:) A delegate can be a view, a string, a proxy, anything as long as you make it responds to the selector. You don't need to make your delegate implement every method in such protocol since all verifications would be done at runtime. To make it looks cleaner you could just redeclare -applicationDidFinishLaunching: in @interface though you don't really need to, just make one in the @implementaiton is enough.

  • yes, you're right. it's look like that i'm not got adapted to dynamic language things such as obj-c because of my c background. – hao haochao Jan 12 '13 at 03:12
  • Since method calls usually look like id objc_msgSend(id anObj, SEL aSelector, ...); You shouldn't think that you are calling functions but sending messages, like internetworking, just superfaster. Obj-C is C, and the only way C-background would be a blockage is that you didn't actually have enough of it. – Fred Frith-MacDonald Jan 12 '13 at 11:41
  • hi, @Fred Frith-MacDonald,thanks. i'm just objective-c training myself few hours through some quick tutorials on GNUSTEP.obj-c is superset of c,but this post is not relating to c basis,right. some of obj-c runtime mechanisms got me confused.by the way. i think that is how method is dispatched and veritified,and even reflection is key for the post,and programming in objective-c. again, thanks.:) – hao haochao Jan 12 '13 at 14:22
  • And by the way, I think you could also ask your questions on GNUstep mailing list, because not much people here would have a clue for a GNUstep question and they would have to guess the question based on Cocoa's behaviour (that could be either good or bad). The list is very responsive. – Fred Frith-MacDonald Jan 12 '13 at 20:12
0

A delegate may inherit from anything appropriate. It is usually supposed to implement a certain protocol. A protocol is a way of implementing a formal communication interface between two classes. However, it is most unlikly that a delegate will inherit from its communication partner class.

With other words: Protocols are often used to overcome the unavailability of multiple inheritance. (Pretty much like interfaces in Java)

Example: A UIViewController subclass' instance controls a view that contains a UITableView. Rather than subclassing the UITableView for the implementation of its look or data, there are two delegates assigned to the table view object. One delegate serves as provider for custom layout (provides items such as the header view) and another (?) delegate provides the data that is being displayed. Now, this delegate could be any object, inheriting from NSObject and implementing the two protocols. This object cold then be instanciated by the view controller and assigned to the table. However, it is common practice that the view controller itself serves as delgate for the table(s) that it controls. That is a good pattern but strictly spoken not required. It could be any object. Now the custom view contoller inherits from UITableViewController (which already implements the protocols and inherits from ViewController) and serves as delgate for the table view. The table view itself could be any subclass of UITableView. (Although this is a bad example here because subclassing UITableView is normally not advisable)

If the delegate does not need to inherit from any class and just implements the protocol, then it shold at least inherit from the cocoa base class NSObject. That ensures that it inherits all the usual capabilites and behaviour of any object. (init method, copy method, description method etc.) That may be required to work properly with other classes of the framework such as beeing used as an object within an NSArray, NSLog etc.

Hermann Klecker
  • 14,039
  • 5
  • 48
  • 71
  • hi! @Hermann Klecker i edit and posting some code that i mentioned. – hao haochao Jan 10 '13 at 12:54
  • Hi @haohaochao. How is that code related to your question? Where do you acutally intanciate and use browserController? (BTW, it should be "BrowserController" as class names should begin with a capital. Just a convention) – Hermann Klecker Jan 10 '13 at 16:30
  • thank you again.@Hermann Klecker. i 've commented the question at first code line. – hao haochao Jan 10 '13 at 16:45
  • I do not now why you must. I did not even say that you must. I said that you may. And with those not really relating code sniplets I cannot say why or why not. I cannot even see how you intend to use the delegate. – Hermann Klecker Jan 10 '13 at 16:53
  • sorry about that. i mean that i haven't found some protocol containing "applicationDidFinishLaunching" method declaration in NSObject.h header. how could i inherit from NSObject class to implement "applicationDidFinishLaunching" method(delegate) because NSobject(nsobject.h header) have no relating protocol containning "applicationDidFinishLaunching" method. – hao haochao Jan 10 '13 at 17:20
  • Now we are getting closer. I did not know it myself but google told me that applicationDidFinishLaunching is declared in the protocol UPApplicationDelegate. http://developer.apple.com/library/ios/#documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html – Hermann Klecker Jan 10 '13 at 18:01
  • Usually when you create a new iOS project within xcode, xcode creates a more or less empty template. All of these templates (that I know) come with an AppDelegate.h and AppDelegate.c . And that is the implementation of an UIResponder (for other reasons it inherits from UIResponder) implementing the protocol. You don't have to care for its instanciation and assignment as delegate to UIApplication. The template should take care of that. Plus, there is no need to actually subclass it. Just take the template of AppDelegate and go on with it. – Hermann Klecker Jan 10 '13 at 18:05
  • Sorry, I meant UIApplicationDelegate two comments above. – Hermann Klecker Jan 10 '13 at 18:06
  • whatever, i think i got what the problem is . thanks you @Hermann Klecker. i really appriciate it.. – hao haochao Jan 12 '13 at 02:46