Is there a way in Swift that I can get a property from a struct without having to reference it?
For example
public struct ASValue<T: Hashable>: Hashable {
private let id = UUID()
public var item: T
public init(item: T) {
self.item = item
}
}
let val = ASValue(item: "Example")
print(val) // Prints: Example
It's possible by adopting CustomStringConvertible and constraining T also to CustomStringConvertible
public struct ASValue<T: Hashable>: Hashable, CustomStringConvertible where T: CustomStringConvertible {
private let id = UUID()
public var item: T
public var description : String {
return "\(item)"
}
}
let val = ASValue(item: "Example")
print(val) // Prints: Example
And this is a struct, the init method is for free.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With