Is it possible in Swift to add variables to an object at runtime?

后端 未结 3 2241
温柔的废话
温柔的废话 2020-12-30 16:09

Specifically, I would like to add a variable of type enum to an instance of UIView, without subclassing or create an extension.

Thanks.

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-30 16:22

    You can use the objc_setAssociatedObject() function from the Objective-C runtime to attach an object (you can wrap that enum into an object) onto another object, and objc_getAssociatedObject() to retrieve it.

提交回复
热议问题