- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
这之间有什么区别:
编号:
#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 usingNSObject*
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 anNSObject
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 anNSObject
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 specifiedNSObject
protocol**
.
**
the protocol (@protocol
) namedNSObject
. There is also a class namedNSObject
that does indeed conform to theNSObject
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 anyNSObject
(becauseNSObject
conforms to theNSObject
protocol), but it could also hold anyNSProxy
, becauseNSProxy
also conforms to the NSObject protocol.
In english, the declarationid<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/
SWIFT代码 print("1", NSObject() == NSObject()) print("2", ObjectIdentifier(NSObject()) == ObjectIdenti
保留在 NSObject 协议(protocol)中声明。 因此NSObject类和NSProxy类实现了它。 然而 NSProxy 和 NSObject 类都有一个分配。 为什么 alloc 没有在
我有一个已在App Store中发布的应用程序。大约有1%-2%的用户报告该应用程序崩溃了。这不是完全意外的行为,因此我要求提供崩溃日志。这是最后一个异常回溯(实际显示问题的部分): Last E
我有一个自定义 NSObject 类,可以调用 People,还有一个来自 CloudMade RMMarker.h 的名为 RMMarker 的类。 RMMarker 类有一个名为 data 的属性
这是崩溃报告,不知道为什么。我使用 AFHTTPRequestOperation setCompletionBlockWithSuccess:failure: 调用电话。在完成 block 中,我得到
应用程序有时会因 [NSObject(NSObject) doesNotRecognizeSelector:] 而崩溃,选择器为 [UIImageView setImage:]。 我通过设置异常断点捕
崩溃报告: 0 CoreFoundation!__exceptionPreprocess + 0x7c 1 libobjc.A.dylib!objc_exception_throw
我正在将我的整个项目从 Objective-C 转换为 Swift...在转换时我遇到了这个问题:“无法将‘Facility’类型的值转换为预期的参数类型“Facility!” 错误出现在我提到的最后
我在将数组[String]保存到核心数据时遇到问题。在核心数据中,我设置了字段“countires”的类型 - 可转换。 我通过以下方式添加数据: filters!.countries = selec
假设您有一个返回 NSDictionary 的方法。要构建字典,您可能需要创建一个 NSMutableDictionary。返回字典的不可变副本而不是仅返回可变字典是否有任何必要或优势? 例如 - (
在一个小型 RTS 项目中,我有一个按钮列表,每个按钮都分配有一个构建对象。有些建筑物非常通用,但其他建筑物则非常特殊,因此我将某些建筑物作为子类。 当我创建一个按钮列表时,每个按钮都有一个 PEHo
这个问题在这里已经有了答案: Objective-C: Property / instance variable in category (6 个答案) Objective-C: Instance
我实际上想知道如何从 NSObject 或任何其他类访问 encodeWithCoder、init 等方法而不继承 NSObject 类。 因为我读到如果我们从 NSObject 类继承,那么它在 S
我正致力于从 Objective-C 教程转换此委托(delegate)协议(protocol)函数,但在尝试通过使用下标返回基于键的值来访问字典中的值时遇到错误。我不太确定这里的错误是什么意思。任何
在我的 Swift 应用程序中,我有一个类: open class CustomCluster : NSObject { open var coordinate = CLLocationCoo
我有一个字典/数组,看起来像这样: var myArray: [[String:NSObject]] = [] let newItem = [ [
这很奇怪,我可以使用 Xcode 5 (5A1413) 在模拟器、iPhone 4S 和 iPhone 5 中运行我的应用程序而没有任何问题,但是当我将应用程序提交到 iTunes 商店时被拒绝并且此
我使用 NSKeyedArchiver.archivedDataWithRootObject(obj) 将对象转换为 NSData。 archivedDataWithRootObject(obj) 方
背景。 请考虑以下步骤: 1) 在 Xcode 中创建一个新的“单 View 应用程序”。 2)创建类NSObject+Extension.h和.m文件: // .h @interface NSObj
在 ARC 环境中遇到一些小问题。创建一个将 View 添加到父 View 的 NSObject - 它基本上是一个可以处理一些文本并显示它的“弹出类”。 在 View Controller 中它被实
我是一名优秀的程序员,十分优秀!