vendredi 8 mai 2015

Translate computed property from Category into Swift Extension

I have the following objective-c category:

@implementation UINavigationBar (Awesome)
static char overlayKey;
static char emptyImageKey;

- (UIView *)overlay
{
    return objc_getAssociatedObject(self, &overlayKey);
}

- (void)setOverlay:(UIView *)overlay
{
    objc_setAssociatedObject(self, &overlayKey, overlay, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end

I tried to translate it into a Swift extension like this:

var AssociatedObjectHandle: UInt8 = 0

extension UINavigationBar {

  var stringProperty:Character {
    get {
      return objc_getAssociatedObject(self, &AssociatedObjectHandle) as! Character
    }
  }


  func setOverlay(overlay: UIView) {
    objc_setAssociatedObject(self, &AssociatedObjectHandle, overlay, objc_AssociationPolicy(OBJC_ASSOCIATION_RETAIN_NONATOMIC))

  }

}

It looks strange because I created a global variable AssociatedObjectHandle and I am not sure what the Character actually does?

Is this the correct way to translate the computed property?

Aucun commentaire:

Enregistrer un commentaire