Override a method in an instance of a single object

Not sure how to do this, and I could not find the answer because of my inability to find words to express what I was looking for. (!)

In Java, I did something like this (I don't remember):

JPanel myButton = new JPanel("Press me"){
    public void add(JComponent component){
        //override add method
    }
};

But I could not find how to do this in Objective-C. What I found in my search were categories and strange characters {{} ...

So how can I override a method in a newly created object?

(For example, override -(BOOL)isEqual;in the newly created NSString*?)

I'm sorry if the question is a little vague.

EDIT:

Obviously without a subclass :)

EDIT:

You can also post my problem if anyone has a better idea:

CCT- COCOS2D, . , , , - (void); ( CCTransition)

CCTransition :)

EDIT:

- (void) onEnterTransitionDidFinish;... - , , ......

, CCTransition CCNode: D!

+5
3

, Java, . Objective-C (, , Obj-C).

Objective-C iOS 4 OS X 10.6 "", ^{}. Objective-C closure. , API-, , , -, .

Objective-C.

+2

, , , , - ():

Method methodToReplace =
          [targetClass instanceMethodSignatureForSelector:@selector(methodToReplace)];
IMP implementationToSet =
          [someProxyClass instanceMethodForSelector:@selector(implementationYouWant)];

method_setImplementation(methodToReplace, implementationToSet);

- Objective-C Runtime , , NSObject ( , , , class_getInstanceMethod , instanceMethodSigntureForSelector:).

, , . method_setImplementation , .

, : iOS, OS 3.2 4.0. , 3.2. , ( - , , Apple iOS SDK , ...).

+5

, NSObject

[anObject overrideMethod:@selector(foo:) 
          byBlock:^(id self,id super,id originalArg){
          ...
}];

,

  • objc_allocateClassPair self ,
  • , , , this this
  • method_setImplementationto set a new implementation to a temporary class
  • use object_setClassto selfto set the class to a new temporary class
  • I did not understand how to provide the superblock: p

He believed that this is basically how Apple does KVO, see for example this discussion.

Read the Runtime Link .

+3
source

All Articles