NSObject 为什么 NSObject 和 NSProxy 中都声明了 alloc,而 NSObject 协议中却声明了 keep

发布于 2024-12-01 08:19:28 字数 531 浏览 1 评论 0原文

keep是在NSObject协议中声明的。

因此 NSObject 类和 NSProxy 类实现了它。

然而 NSProxy 和 NSObject 类都有一个 alloc。

为什么 NSObject 协议中没有声明 alloc?

附带问题: NSObject 协议还用于存储方法的类版本,其中实例方法位于 NSObject 类中。有什么办法可以阻止在 NSObject 协议中声明类和实例吗?为什么要把他们分开?

@protocol NSCopying

- (id)copyWithZone:(NSZone *)zone;     //INSTANCE METHOD version of copyWithZone

@end
@interface NSObject <NSObject> {
    Class   isa;
}
...

+ (id)copyWithZone:(NSZone *)zone;     //CLASS METHOD version of copyWithZone

干杯

retain is declared in NSObject protocol.

Therefore NSObject class and NSProxy class implement it.

yet both NSProxy and NSObject classes both have an alloc.

Why isnt alloc declared in NSObject protocol?

Side question:
NSObject protocol is also used to stored the class version of a method where as the instance method is in NSObject class. Is there anything to stop both class and instance being declared in the NSObject protocol. Why split them up?

@protocol NSCopying

- (id)copyWithZone:(NSZone *)zone;     //INSTANCE METHOD version of copyWithZone

@end
@interface NSObject <NSObject> {
    Class   isa;
}
...

+ (id)copyWithZone:(NSZone *)zone;     //CLASS METHOD version of copyWithZone

Cheers

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

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

发布评论

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

评论(1

梦罢 2024-12-08 08:19:28

我认为这是由Objective-C的要求和实现的要求决定的。

为了创建一个对象,您需要一种分配它的方法。这是通过 alloc 方法完成的。由于这是使用 Objective-C 所必需的,因此意味着所有根对象都应该实现它。然而,内存管理不一定要通过引用计数来完成。您可以使用垃圾收集来代替。因此,使用 Objective-C 不需要 retain 方法。 Apple 的实现创建了 retain 方法作为内存管理的一种形式。他们希望确保所有对象都可用,因此将其添加到 NSObject 协议中。 Cocoa 中的所有根类都应该遵守该协议,因此它们都应该有一个 retain 方法。

类可以遵循 NSCopying 协议来表明该类的实例可以被复制。通常,您不想复制类,因此那里没有定义类方法。但是,有时您不知道对象是类还是实例,但调用 conformsToProtocol: 无论如何都会返回相同的值。通过创建一个名称相同的类方法,即使您不知道是否有实例或类,您也知道调用 copyWithZone: 是安全的。

I think this is determined by what is required by Objective-C, and what is required by implementation.

In order to create an object, you need a way to allocate it. This is done by the alloc method. Since this is required to use objective-c, it is implied that all root objects should implement it. However, memory management does not have to be done through reference counting. You could use garbage collection instead. Because of this, the retain method is not required to use objective-c. Apple's implementation created the retain method as a form of memory management. They wanted to ensure that all objects had it available, so they added it to the NSObject protocol. All root classes in Cocoa are supposed to conform to that protocol, so they should all have a retain method.

A class can conform to the NSCopying protocol to show that instances of that class can be copied. Normally, you wouldn't want to copy a class, so a class method isn't defined there. However, sometimes you don't know whether an object is a class or instance, but calling conformsToProtocol: will return the same value either way. By making an identically named class method, you know that it is safe to call copyWithZone: even if you don't know whether you have an instance or class.

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