Swift中的关联对象,全局键是否实际产生特定的实例? [英] Associated objects in Swift, does global key actually produce specific instances?

查看:65
本文介绍了Swift中的关联对象,全局键是否实际产生特定的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在Swift中具有关联的对象,您只需使用内存地址作为句柄,然后使用objc调用即可.

To have an associated object in Swift, you simply use a memory address as a handle, and then use the objc calls.

您可以在所有地方使用google的常见示例代码是:

The usual example code you can google everywhere is:

var keyA:UInt8 = 0
var keyB:UInt8 = 0
extension UIViewController {

    var aoAA: String? {
        get { return objc_getAssociatedObject(self, &keyA) as? String }
        set { objc_setAssociatedObject(self, &keyA, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN) }
    }
    var aoBB: String? {
        get { return objc_getAssociatedObject(self, &keyB) as? String }
        set { objc_setAssociatedObject(self, &keyB, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN) }
    }
}

这很好,

class Thing: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        aoAA = "this is a"
        print("...... \(aoAA)")
        aoBB = "this is b"
        print("...... \(aoBB)")
        aoAA = "changed A"
        print("...... \(aoAA) \(aoBB)")
        aoBB = "changed B"
        print("...... \(aoAA) \(aoBB)")
        aoAA = aoBB
        print("...... \(aoAA) \(aoBB)")
        }

但是请稍候...

句柄keyA和keyB 对整个项目是全局的.

The handles, keyA and keyB, are global to the whole project.

当您在不同的 UI UIViewController中使用aoAA和aoBB时,怎么可能aaAA和aoBB充当了特定于该实例的属性?

When you use aoAA and aoBB in different UIViewController, how can it possibly be that aoAA and aoBB act as properties specific to that instance?

项目中到处肯定只有一个"aoAA"吗?

Surely there is only "one" aoAA everywhere in the project?

即,aoAA是全局的-就像handle keyA是全局的一样?

ie, aoAA is a global - just as the handle keyA is a global?

我的测试似乎表明它们 是独立变量,特定于不同UIViewControllers的不同实例,但这似乎很愚蠢.

My testing seemed to show that they are independent variables, specific to different instances of different UIViewControllers, but that seems barmy.

aoAA的每个实例怎么可能不同-它使用相同的全局句柄?

How can each instance of aoAA possibly be different - it's using the same global handle?

推荐答案

Objective-C的关联对象"概念使您可以将一个目标"对象实例与一个源"对象实例连接起来.这是一种单向关联,其中只有源才知道目标:

Objective-C's concept of "associated objects" lets you connect one "target" object instance with one "source" object instance. It's a unidirectional association where only the source knows the target:

objc_setAssociatedObject(source, key, target, ...)

该关联使用一把钥匙能够与一个来源连接和区分任意数量的关联对象.密钥显然必须不同,但只能用于一个源实例.

The association uses a key to be able to connect and distinguish any number of associated objects with one source. The keys must obviously be different—but just for one source instance.

因为必须提供密钥和源实例才能检索关联的对象,所以不必使用真正唯一的密钥. objc_***AssociatedObject的实现可以通过组合实例指针和键来形成进程唯一键.

Because you have to provide both the key and the source instance to retrieve the associated object, it's not necessary to use really unique keys. The implementation of objc_***AssociatedObject can form a process-unique key by combining the instance pointer and the key.

因此,在您的示例中,是的,即使keyAkeyB是全局的,aoAAaoBB都将为每个UIViewController实例返回单独的值.

So for your example, yes, both aoAA and aoBB will return individual values per each UIViewController instance, even though keyA and keyB are global.

要完全清楚,您要做在给定扩展名中需要每个关联对象的不同键.因此,aoAAaoBB每个都需要它们自己的密钥,如示例代码所示(即,指向keyAkeyB的指针).但是,正如该问题所问的那样,正确的是每个关联对象的每个键只有一个键,无论该扩展使用了多少符合类.

To be absolutely clear, you do need different keys for each associated object in a given extension. So, aoAA and aoBB each need their own key, as shown in the example code (to wit, the pointers to keyA and keyB). But as is asked in the question it's correct have only one key for each associated object, no matter how many conforming classes the extension is used in.

这篇关于Swift中的关联对象,全局键是否实际产生特定的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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