我正在将一个应用程序从Objective-C切换到Swift,我有几个带有存储属性的类别,例如:
@interface UIView (MyCategory)
- (void)alignToView:(UIView *)view
alignment:(UIViewRelativeAlignment)alignment;
- (UIView *)clone;
@property (strong) PFObject *xo;
@property (nonatomic) BOOL isAnimating;
@end
Run Code Online (Sandbox Code Playgroud)
由于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))
}
}
var initialPath: CGPathRef! {
get {
return objc_getAssociatedObject(self, "initialPath") as CGPathRef
}
set {
objc_setAssociatedObject(self, "initialPath", newValue, UInt(OBJC_ASSOCIATION_RETAIN))
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是在执行以下操作时我得到了EXC_BAD_ACCESS:
class UIBubble : UIView {
required init(coder …Run Code Online (Sandbox Code Playgroud)