I spent lot of time to fix this issue with other solutions unable to make it work.
public struct LoanDetails {
public var dueDate: String?
public init() {}
}
public func getLoanDetails(_ result: @escaping (_ loanDetails: LoanDetails?, _ error: Error?) -> Void) {
//This is an API call it returns data here mentioned to get current date.
var loanDetails = LoanDetails()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM d, yyyy"
loanDetails.dueDate = dateFormatter.string(from: Date())
result(loanDetails, nil)
}
When I'm calling this getLoanDetails function in viewModel it throws an error: escaping closure captures mutating 'self' parameter
struct MyViewModel {
var var dueDate: String?
mutating func getLoanData() { //must be mutating to set some other properties
getLoanDetails({ (loanDetails, error) in
dueDate = loanDetails?.dueDate // Getting error here
})
}
}
Calling getLoanData() from viewDidload can anyone suggest correct way of doing this.
I know by changing viewModel struct to class and removing mutating keyword it works but I wanted to maintain all viewModels as struct in app.