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 theNSObject 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).
分享到:
相关推荐
用法很简单:只需将NSObject+OTDeallocHandler.h和NSObject+OTDeallocHandler.m 复制到您的项目中即可。 然后将deallocHandler属性设置为任何对象: NSObject *foo = [[NSObject alloc] init]; __weak NSObject *...
2. **NSObject是基类**:所有的Objective-C类都间接或直接继承自NSObject类,它提供了基本的对象行为。 3. **单继承**:Objective-C支持单继承,意味着一个类只能有一个父类,但可以通过协议实现多重继承的效果。 ...
如果一个类需要同时遵循两个或更多包含相同方法的协议,可以通过将它们都列在 `<...>` 中来实现,就像 `@protocol MyTextFieldDelegate <NSObject, UITextFieldDelegate>` 这样。这允许类实现来自多个源的相同方法,...
#import <Foundation/Foundation.h> // A sample class demonstrating good Objective-C style. All interfaces, // categories, and protocols (read: all top-level declarations in a header) // MUST be ...
@interface MyClass : NSObject <Foo> // 方法实现... @end ``` 该类必须实现所有非`@optional`的方法。通过这种方式,可以更清晰地表达对象之间的交互,并有助于编译器进行类型检查。 ### UIView和UIWindow类 ...
属性的访问通常使用`getter`和`setter`方法,Objective-C2.0后引入的点语法使得我们可以更直观地访问和修改属性,如`foo.value`,虽然看起来像直接访问变量,但实际上仍然是调用方法。 总之,Objective-C是一种强大...
let valuePointer = UnsafeMutablePointer<T>.allocate(capacity: 1) defer { valuePointer.deallocate(capacity: 1) } // use pointer... } ``` 这里,`valuePointer`在函数结束时会被正确释放。 4. **锁的...
客观分析器 获取Objective-C头文件并将其转换为等效的javascript调用 安装 $ npm install objective-c-parser ...# import < Foundation> @protocol Ponies, Foo; @interface BasicName : NSObject // Another comment
JNSwapIMP 允许您从继承自 NSObject 的任何对象中交换任何方法的实现,从而非常容易重新设计特定方法的功能或侵入密封类并重新编程其某些功能。 这个项目的灵感来自项目。 代替拥有两个对象或类并“调配”它们的...