有没有一种方法可以将扩展添加到AnyObject? [英] Is there a way to add an extension to AnyObject?

查看:94
本文介绍了有没有一种方法可以将扩展添加到AnyObject?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Objective-C中,我创建了一个非常方便的类别NSObject(用Swift来说是扩展),增加了在运行时向任意NSObject添加任意键/值对的功能.它使用关联的对象将可变字典附加到该对象,然后提供get和set方法,以从该字典获取/设置键/值对.只是几行代码.

In Objective-C, I created a very handy category (an extension, in Swift terms) of NSObject that added the ability to add arbitrary key/value pairs to any NSObject at runtime. It uses associated objects to attach a mutable dictionary to the object, and then provides get and set methods that get/set key/value pairs to/from that dictionary. It's only a few lines of code.

这使得可以在运行时将任意键/值对附加到ANY对象,包括系统创建的对象.那是关键.在某些情况下,系统框架会向您返回一个对象,并且您需要能够为其附加值.

This makes it possible to attach arbitrary key/value pairs to ANY object at runtime, including objects created by the system. That's the key. There are cases where a system framework returns an object to you and you need to be able to attach a value to it.

此技巧还使创建具有新实例变量的类别成为可能. (好吧,它们并不是真的,但是因为它确实允许您向类别中的对象添加新的状态变量.)

This trick also makes it possible to create categories that have new instance variables. (Ok, they don't really, but for it does let you add new state variables to objects in a category.)

在Swift 1.2中这是不可能的,因为:

This isn't possible in Swift 1.2 because:

  • Swift没有像NSObject这样的所有对象都有基类 目标C.它使用AnyObject,这是一个协议.
  • 迅捷1.2 不允许扩展协议.
  • Swift doesn't have a base class for all objects like NSObject in Objective-C. It uses AnyObject, which is a protocol.
  • Swift 1.2 doesn't allow extensions to protocols.

在Swift 1.2下,我不得不放弃这一点.

I had to give up on this under Swift 1.2.

但是Swift 2允许扩展协议.我以为太好了,现在我可以添加扩展名,使我可以将键/值对添加到AnyObject!"

But Swift 2 allows extensions to protocols. I thought "Great, now I can add my extension that lets me add key/value pairs to AnyObject!"

不高兴.

当我尝试为AnyObject创建扩展名时

When I try to create my extension for AnyObject:

extension AnyObject: AssociatedObjectProtocol

我收到错误消息

'AnyObject'协议无法扩展

'AnyObject' Protocol cannot be extended

啊!这么近,但不行.似乎该语言明确禁止扩展AnyObject.为什么会这样,并且有什么解决方法?

Arghh! So close, but nope. It seems like the language explicitly forbids extending AnyObject. Why is this, and is there any way around it?

我不经常在NSObject上使用类别,但是当我这样做时,它就是一个救命稻草.我想将其添加到Swift中的技巧包中.

I don't use my category on NSObject that often, but when I do, it's a lifesaver. I'd like to add it to my bag of tricks in Swift.

我可以像在Objective-C中一样将其添加到NSObject中,但这意味着它仅适用于从NSObject继承的对象-使其不适用于本机Swift类.

I could add it to NSObject just like I do in Objective-C, but that means it only works for objects that inherit from NSObject - making it not work for native Swift classes.

推荐答案

不幸的是,不存在一种与您想要的完全相同的解决方法.因此,我建议创建一个扩展了添加默认实现的协议:

Unfortunately a workaround which does exactly the same as you want doesn't exist. So I would suggest to make a protocol which has an extension to add default implementations:

protocol A: AnyObject {}

// in Swift you would rather use this
protocol A: class {}

// add default implementations
extension A {
    func aMethod() {
        // code
    }
}

// Example
class B: A {}

// use the method
B().aMethod()

此方法不会使所有类都自动符合此协议(但只有类可以符合此协议).因此,您必须使他们适应自己.由于您使用的方式不多,因此这是一个合理的解决方案.

This approach does not make all classes automatically conform to this protocol (but only classes can conform to it). So you have to make them conform yourself. Since you don't use this as much this would be a reasonable solution.

这篇关于有没有一种方法可以将扩展添加到AnyObject?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆