Skip to content

Instantly share code, notes, and snippets.

@madcato
Created January 12, 2020 17:42
Show Gist options
  • Save madcato/c2c450ed1c5780da102a8e5a2e1839d8 to your computer and use it in GitHub Desktop.
Save madcato/c2c450ed1c5780da102a8e5a2e1839d8 to your computer and use it in GitHub Desktop.
Logger sample to explain how to use @PropertyWarpper
//
// User.swift
// TestAspect
//
// Created by Daniel Vela on 12/01/2020.
// Copyright © 2020 veladan. All rights reserved.
//
@propertyWrapper
struct Logger<Value> {
var innerValue: Value
var variableName: String
init(_ initialValue: Value, _ variableName: String) {
self.innerValue = initialValue;
self.variableName = variableName;
}
var value: Value {
get {
print("Getting the \(self.variableName) value of \(self.innerValue)")
return self.innerValue
}
set {
print("Will set the initial \(self.variableName) value to \(newValue)")
self.innerValue = newValue
print("Has set the \(self.variableName) value to \(self.innerValue)")
}
}
}
struct User {
@Logger("", "User.name")
var name: String
}
func run() {
var u = User()
u.name = "Daniel Vela"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment