Skip to content

Instantly share code, notes, and snippets.

@letatas
Created January 13, 2020 16:01
Show Gist options
  • Save letatas/21c0f9f31b8702090aab6771775acc6f to your computer and use it in GitHub Desktop.
Save letatas/21c0f9f31b8702090aab6771775acc6f to your computer and use it in GitHub Desktop.
ReusableXibViews - NibWrapped - v2
//
// NibWrapped.swift
// ReusableXibViews
//
// Created by Matthias Lamoureux on 13/01/2020.
// Copyright © 2020 QSC. All rights reserved.
//
import UIKit
/// Property wrapper used to wrapp a view instanciated from a Nib
@propertyWrapper @dynamicMemberLookup public struct NibWrapped<T: UIView> {
/// Initializer
///
/// - Parameter type: Type of the wrapped view
public init(_ type: T.Type) { }
/// The wrapped value
public var wrappedValue: UIView!
/// The final view
public var unwrapped: T { (wrappedValue as! NibWrapperView<T>).contentView }
/// Dynamic member lookup to transfer keypath to the final view
public subscript<U>(dynamicMember keyPath: KeyPath<T,U>) -> U { unwrapped[keyPath: keyPath] }
/// Dynamic member lookup to transfer writable keypath to the final view
public subscript<U>(dynamicMember keyPath: WritableKeyPath<T,U>) -> U {
get { unwrapped[keyPath: keyPath] }
set {
var unwrappedView = unwrapped
unwrappedView[keyPath: keyPath] = newValue
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment