Swiftpack.co -  mediamonks/MMMPropertyWrappers as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
mediamonks/MMMPropertyWrappers
Small collection of property wrappers.
.package(url: "https://github.com/mediamonks/MMMPropertyWrappers.git", from: "0.1.0")

MMMPropertyWrappers

Build Test

Small collection of property wrappers.

(This is a part of MMMTemple suite of iOS libraries we use at MediaMonks.)

Installation

Podfile:

source 'https://github.com/mediamonks/MMMSpecs.git'
source 'https://github.com/CocoaPods/Specs.git'
...
pod 'MMMPropertyWrappers'

SPM:

.package(url: "https://github.com/mediamonks/MMMPropertyWrappers", .upToNextMajor(from: "0.1.0"))

Usage

Simple examples for each property wrapper.

@Once

A wrapper that allows to set a value only once after setting the initial value. This comes in useful in instances where you want to freeze the value after configuring it.

For instance on configuration object, where you want to assign a default value, but give the user the ability to alter this value in a configuration callback.

class Config {
    @Once var myValue: Bool = false
    @Once var otherValue: Bool = false

    init(_ config: (Config) -> Void) {
        config(self)
    }
}

// At call site
let config = Config {
    $0.myValue = true
}

Now config.myValue is 'frozen', if you try setting it afterwards it will throw an assertionFailure. However, config.otherValue is still open, since the user decided not to alter that value, so it's wise to call .freeze() after your configuration block.

init(_ config: (Config) -> Void) {
    config(self)

    _myValue.freeze()
    _otherValue.freeze()
}

@EventTrigger

A wrapper that will trigger a SimpleEvent or LazySimpleEvent, e.g. AnySimpleEvent when the value changes. The value should conform to Equatable, and the enclosing class to EventTriggerable.

This removes a lot of boilerplate code, going from:

protocol ViewModel {

	var foo: String { get }
	var bar: String { get }

	var didChange: SimpleEventObservable { get }
}

class DefaultViewModel: ViewModel {

	public private(set) var foo: String {
		didSet {
			_didChange.trigger(if: foo != oldValue)
		}
	}

	public private(set) var bar: String {
		didSet {
			_didChange.trigger(if: bar != oldValue)
		}
	}

	private let _didChange = SimpleEvent()
	public var didChange: SimpleEventObservable { _didChange }
}

To:

class DefaultViewModel: ViewModel, EventTriggerable {

	@EventTrigger public private(set) var foo: String
	@EventTrigger public private(set) var bar: String

	private let _didChange = SimpleEvent()
	public var didChange: SimpleEventObservable { _didChange }
}

Every time foo or bar is set, and the value != oldValue, the _didChange event will trigger.

If your property does not conform to Equatable, you could use @LenientEventTrigger.

@LenientEventTrigger

Same as @EventTrigger, but without requiring to conform to Equatable, this will trigger every time the value hits didSet.

GitHub

link
Stars: 0
Last commit: 3 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.

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