gpt4 book ai didi

objective-c - Objective C 中的 id 和 NSObject 有什么区别?

转载 作者:太空狗 更新时间:2023-10-30 03:21:45 25 4
gpt4 key购买 nike

这之间有什么区别:

编号:

#import <objc/Object.h>

@interface Forwarder : Object
{
id something;
}

NS对象:

#import <objc/Object.h>

@interface Forwarder : Object
{
NSObject *something;
}

谢谢你。

最佳答案

This Greg MILLER's blog post来自 unixjunkie blog总结差异

部分摘录:

There's often confusion about the difference between the following three declarations in Objective-C:

id foo1;
NSObject *foo2;
id<NSObject> foo3;
  • The first one is the most common.
    It simply declares a pointer to some Objective-C object (see /usr/include/objc/objc.h). id gives the compiler no information about the actual type of the object, so the compiler cannot do compile-time type checking for you.

  • Just because we know that an id is an Objective-C object does not mean that it points to an object that derives from NSObject, or that it even has common methods like retain and release.
    One solution is to statically type our variable using NSObject* as shown in number 2 above.
    This gives the compiler information about the class of the object pointed to by foo2 so the compiler can warn if you send a message to foo2 that an NSObject doesn't respond to. This means you can safely call retain, release, description, etc., but the compiler will warn if you call length or count or anything that an NSObject doesn't respond to.

  • Declaring an object as id<NSObject> tells the compiler that you don't care what type the object is, but you do care that it conforms to the specified NSObject protocol**.
    ** the protocol (@protocol) named NSObject. There is also a class named NSObject that does indeed conform to the NSObject protocol, but they are two different thing
    The compiler will ensure that all objects you assign to that pointer conform to the required protocol.
    A pointer typed like this can safely hold any NSObject (because NSObject conforms to the NSObject protocol), but it could also hold any NSProxy, because NSProxy also conforms to the NSObject protocol.
    In english, the declaration id<NSObject> foo3; says "foo3 is a pointer to an object of any type that behaves like an NSObject".
    This is very powerful, convenient, and expressive. In reality, we often don't care what type an object is, we just care that it responds to the messages that we want to send it (e.g., retain, release).


If you don't want (or can't have) any type checking, then use a plain id. This is very common for return types on methods that don't know the type of object they're returning (e.g., +alloc). It is also common to declare delegates to be type id, because delegates are generally checked at runtime with respondsToSelector:, and they usually aren't retained.

However, if you do want compile-time type checking, you must decide between the second and third cases. Well, let me just help you out—you want the third case! :-) I've very, very, VERY rarely seen a situation where NSObject * worked but id would not. And using the protocol form has the advantage that it will work with NSProxys.

关于objective-c - Objective C 中的 id 和 NSObject 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2891411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com