Alternative to poseAsClass in Mac OS X 10.5 and higher?



Apple has deprecated NSObject's poseAsClass: method for OS X v10.5 and above. Is there another way to make class posing work?

3 ответов


I don't think there is a class-level equivalent, but you can exchange the implementation of two methods, which was often the purpose of using poseAsClass: (of course, you can exchange more than one method if you need to override multiple methods in a class). You want method_exchangeImplementations in the Objective-C 2.0 runtime (#import objc/runtime.h). A word of warning: after calling method_exchangeImplementations, calling the 'new' method actually calls the original method definition.

  • Спасибо! Мне удалось заставить это работать, используя комбинацию ответа mcandre и документации, на которую вы ссылались. Небольшое примечание: мне пришлось набрать номер #include "objc/runtime.h", чтобы использовать описанные вами методы.

    e.James17 августа 2009, 21:25

What are you trying to do? There are often ways around posing. I will concede, though, that it is sometimes the only way :)

  • I’m trying to keep track of all binding (and unbinding) messages in a Cocoa application. I want to NSLog() some debug information whenever a binding is made between one object and another. I ended up using method_exchangeImplementations on the binding methods for NSObject, and it did the trick.

    e.James18 августа 2009, 13:46
  • Sounds like you’re Doing It Right :) I’d be wary of doing it in shipping code.

    Jonathan Dann19 августа 2009, 10:28

Lap Cat gives an alternative.