Questions tagged «associated-object»

18
如何在Swift中存储属性,就像在Objective-C中一样?
我将应用程序从Objective-C切换到Swift,我将其分为具有存储属性的几个类别,例如: @interface UIView (MyCategory) - (void)alignToView:(UIView *)view alignment:(UIViewRelativeAlignment)alignment; - (UIView *)clone; @property (strong) PFObject *xo; @property (nonatomic) BOOL isAnimating; @end 由于Swift扩展不接受此类存储的属性,因此我不知道如何维护与Objc代码相同的结构。存储的属性对于我的应用程序确实非常重要,我相信Apple一定已经创建了一些解决方案来在Swift中实现。 正如jou所说,我正在寻找的实际上是使用关联的对象,所以我做了(在另一种情况下): import Foundation import QuartzCore import ObjectiveC extension CALayer { var shapeLayer: CAShapeLayer? { get { return objc_getAssociatedObject(self, "shapeLayer") as? CAShapeLayer } set(newValue) { objc_setAssociatedObject(self, "shapeLayer", newValue, UInt(OBJC_ASSOCIATION_RETAIN)) } …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.