Swiftpack.co -  IMFWorks/Associated as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
IMFWorks/Associated
add property for protocol、extension and class
.package(url: "https://github.com/IMFWorks/Associated.git", from: "v1.0.1")

Associated

Swift Build Status

✨ Add associated property for extension of protocol or class

At a Glance

Add associated property for extension of protocol


protocol FullNamed: class {
    var name: String {get set}
}

extension FullNamed where Self: AssociatedCompatible {
    var name: String {
        get {
            return self.associated.value(default: "")
        }
        
        set {
            self.associated.setValue(newValue)
        }
    }
}

class Person: FullNamed, AssociatedCompatible {
    
}

let person: FullNamed = Person()
person.name = "roger"
let name = person.name
XCTAssertEqual(name, "roger")

Tips and Tricks

  • You can use Associated to all of NSObject subclasses.
class Animal: NSObject {
}

extension Animal: FullNamed {
}

extension Animal {
    var age: Int? {
        get {
            return self.associated.value()
        }
        
        set {
            self.associated.setValue(newValue)
        }
    }
}

let animal = Animal()
animal.age = 100;
let age = animal.age
XCTAssertEqual(age, 100)

Installation

import PackageDescription

let package = Package(
  name: "MyAwesomeApp",
  dependencies: [
    .Package(url: "https://github.com/IMFWorks/Associated", majorVersion: 1),
  ]
)

License

Associated is under Apache license. See the LICENSE file for more info.

GitHub

link
Stars: 4
Last commit: 5 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

1 year ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API