Cocoa,NSProxy,如何接管对象中的方法?

发布于 2024-09-11 08:00:36 字数 327 浏览 7 评论 0原文

我需要用我自己的实现替换对象中的方法。例如,

Person *p; // some object
NSMutableArray *array = [NSMutableArray array];
[array addObject: p];

如何用我自己的方法替换 addObject?

换句话说,有没有办法用另一个实现替换 SPECIFIC 对象的 addObject: 实现?

我一直在玩 NSProxy 但不知道我应该做什么。

任何帮助将不胜感激。

谢谢

I need to replace a method in an object with my own implementation. For example,

Person *p; // some object
NSMutableArray *array = [NSMutableArray array];
[array addObject: p];

How can I replace addObject with a method of my own?

In other words, is there a way to replace the implementation of addObject: of a SPECIFIC object with another implementation?

I have been playing around with NSProxy but couldnt find out what I should do.

Any help will be highly appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只有一腔孤勇 2024-09-18 08:00:36

使该对象成为具有不同方法实现的不同类的实例。

Make that object an instance of a different class with a different implementation for the method.

乞讨 2024-09-18 08:00:36

您可以使用类别来覆盖 NSMutableArray:addObject 方法。有关详细信息,请参阅学习 Objective-C 教程(第 11 节)。

You could use a category to override the :addObject method of NSMutableArray. See the Learn Objective-C tutorial (section 11) for more information.

扎心 2024-09-18 08:00:36

您可以使用方法调配方法来替换方法运行时。实际上有一个像你一样的问题,我认为 finsl 解决方案也应该适合你 - Method swizzling for NSArray

You can use method swizzling approach for replacing methods runtime. There was actually a question as yours on SO, I think the finsl solution should work for you also - Method swizzling for NSArray

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文