p-x9 / AssociatedObject

🔗 Swift Macro for allowing variable declarations even in class extensions
MIT License
126 stars 3 forks source link

Format expanded code #14

Closed p-x9 closed 11 months ago

p-x9 commented 11 months ago
@AssociatedObject(.OBJC_ASSOCIATION_ASSIGN) 
    var test: String = "" {
        willSet {
            print("willSet: new", newValue)
            print(backgroundColor)
        }
        didSet {
            print("didSet: old", oldValue)
        }
    }

Old

get {
    objc_getAssociatedObject(
        self,
        &Self.__associated_testKey
    ) as? String
    ?? ""
}

set {
    let willSet: (String) -> Void = { [self] newValue in
        print("willSet: new", newValue)
                    print(backgroundColor)
    }
willSet(newValue)
    let oldValue = test
    objc_setAssociatedObject(
        self,
        &Self.__associated_testKey,
        newValue,
        .OBJC_ASSOCIATION_ASSIGN
    )
    let didSet: (String) -> Void = { [self] oldValue in
        print("didSet: old", oldValue)
    }
didSet(oldValue)
}

New

get {
    objc_getAssociatedObject(
        self,
        &Self.__associated_testKey
    ) as? String
    ?? ""
}

set {
    let willSet: (String ) -> Void = { [self] newValue in
        print("willSet: new", newValue)
        print(backgroundColor)
    }
    willSet(newValue)

    let oldValue = test
    objc_setAssociatedObject(
        self,
        &Self.__associated_testKey,
        newValue,
        .OBJC_ASSOCIATION_ASSIGN
    )

    let didSet: (String ) -> Void = { [self] oldValue in
        print("didSet: old", oldValue)
    }
    didSet(oldValue)
}