Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How Do Classes Respond to performSelector?




On Jan 30, 2008, at 1:14 PM, Francisco Tolmasky wrote:

This is just a question out of curiosity. In the developer docs, only - (id)performSelector: is defined.
So I was going to write my own + (id)perfromSelector, but first figured I'd check whether there was
one already by writing up a quick test in XCode - which it turns out there is (saved me the work).

In Objective-C, a class object gets all the instance methods of the root class for its hierarchy. This means that every class object that descends from NSObject gets all of NSObject's instance methods - including performSelector:.


This isn't a magic property of NSObject. Your own root classes do this too:

#import <objc/objc.h>

@interface MyRootClass @end

@implementation MyRootClass
- (void)sayHi { puts("Hi!"); }
+ (void)initialize { /* Must be implemented */ }
@end

@interface MySubclass : MyRootClass @end
@implementation MySubclass @end

int main(void) {
    [MySubclass sayHi];
    return 0;
}

Calling sayHi on the class object invokes the instance method of the root class.

Hope that's clear,
-Peter

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >How Do Classes Respond to performSelector? (From: Francisco Tolmasky <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.