我如何知道指针是否是 NSObject?

发布于 2024-08-06 23:30:52 字数 513 浏览 8 评论 0原文

我在 Objective-C 类中有一个指针,我需要向其发送消息。指针可能是任何东西,因此我需要确保它在发送消息之前能够响应我的消息。这是我用来进行检查的函数:

int delegatePreparedForSelector(id delegate, SEL aSelector) {
    if (delegate 
        && [delegate isKindOfClass:[NSObject class]]
        && [delegate respondsToSelector:aSelector]) {
        return YES;
    }
    return NO;
}

问题是有时委托指针是一个 struct objc-object * ,并且当我遇到 EXC_BAD_ACCESS 错误访问错误时我发送 isKindOfClass 消息。

我应该使用更好的测试来确定代表是否会回复我的消息?

I have a pointer in an objective-C class that I need to send messages to. The pointer can potentially be anything, so I need to make sure that it will respond to my messages before I send them. Here's the function I'm using to do the checking:

int delegatePreparedForSelector(id delegate, SEL aSelector) {
    if (delegate 
        && [delegate isKindOfClass:[NSObject class]]
        && [delegate respondsToSelector:aSelector]) {
        return YES;
    }
    return NO;
}

The problem is that sometimes the delegate pointer is a struct objc-object * and I get a EXC_BAD_ACCESS bad access error when i send the isKindOfClass message.

Is there a better test i should be using to determine if the delegate will respond to my messages?

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

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

发布评论

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

评论(3

长途伴 2024-08-13 23:30:52

等等,你真的是说指针可以是任何东西吗?就像指向一块原始 malloc 内存的 void * 一样,或者不是从 NSObject 派生的 objc_object 吗?如果确实如此,那么就没有办法安全地进行这项工作。这相当于说“如果不取消引用这个指针,我怎么知道取消引用是安全的?”唯一的方法是先验地知道无论传递给你的是什么都没有给你一个坏指针。

您可以尝试编写一些信号处理程序代码来清理 EXEC_BAD_ACCESS,但最终它会运行缓慢、效果不佳,并掩盖许多其他真正的错误。实际上,您要么对传入的内容有一些限制,要么需要重新构建项目的这一部分。

Wait, do you really mean the pointer can be anything? Like a void * pointing to a chunk of raw malloc'ed memory, or an objc_object that does not derive from NSObject? If that is really the case then there is no way to make this work safely. It is equivalent to saying "Without dereferencing this pointer how can I know it is safe to dereference?" The only way is to have a priori knowledge that whatever passed it into you did not hand you a bad pointer.

You can try to write some signal handler code to cleanup an EXEC_BAD_ACCESS, but ultimately it will work slowly, poorly, and mask lots of other real bugs. Realistically you either have some constraints on what you are being passed in, or you need to re-architect this part of your project.

-黛色若梦 2024-08-13 23:30:52

听起来你的委托在调用之前就被处理掉了,但这并不是说这段代码一定有什么问题。

此外,您还可以对参数强制实施协议,如下所示:iddelegate 而不是仅仅使用裸 ID。

It sounds like your delegate is being disposed of before the call, not that there is anything necessarily wrong with this code.

Also, you can enforce a protocol implementation on the parameter like so: id<MyDelegateProtocol> delegate instead of just using a bare id.

木緿 2024-08-13 23:30:52

指向 struct objc_object 类型的委托指针导致问题令人困惑,因为 Obj-C 中的所有对象都是类型(深入研究 obj-c 对象):

struct objc_object
{
    struct objc_class *isa;
    /* extra stuff */
};

*isa 指向一个类..某个类。因此,您设置为委托的对象可能不存在或指向错误的内存。

the delegate pointer pointing to type struct objc_object causing a problem is confusing, as all objects in Obj-C are of type (digging into an obj-c object):

struct objc_object
{
    struct objc_class *isa;
    /* extra stuff */
};

the *isa points to a class.. some class. So the object you're setting as a delegate might just not exist or point to bad memory.

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