Created
January 13, 2020 16:01
-
-
Save letatas/21c0f9f31b8702090aab6771775acc6f to your computer and use it in GitHub Desktop.
ReusableXibViews - NibWrapped - v2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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