swiftreferenceoption-typeinout

How to guarantee that inout argument won't change Type and won't become nil in a function


Working in swift 4. I've got a function like this

func setFields<T>(_ fromView : UIView,  toObject : inout T!) -> T! 

setFields(self.view, toObject: &self.productExtended.product)

//inside ProductExtended
public var product: Product

When i call it like this i get the error:

"Inout argument could be set to a value with a type other than 'Product'; use a value declared as type '_?' instead"

Moreover if I try to call it for a field inside the ProductExtended.Product i get ambiguous context Is there a way to guarantee to the compiler that I don't change that argument's value type and i don't make it nil inside the function ?


Solution

  • inout won't accept Generic type , because the reference wants to store value backs to its original value. here is the link for inout https://docs.swift.org/swift-book/LanguageGuide/Functions.html