Swiftpack.co - IMFWorks/Associated as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by IMFWorks.
IMFWorks/Associated v1.0.1
add property for protocol、extension and class
⭐️ 4
🕓 2 years ago
.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.

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