I have seen couple of questions but Didn't find solution and a reason
Here is struct
struct MovementFormattedData {
... Other properties ...
lazy var timeAsDate:Date? = {
return MovementFormattedData.getUTCDate(movementTime: movementTime)
}()
static func getUTCDate(movementTime:String?) -> Date? {
// return date
}
}
Now I have array
var movements :[MovementFormattedData] = []
When I try
self?.movements.firstIndex(where: {$0.timeAsDate > Date() })
I am getting
Cannot use mutating getter on immutable value: '$0' is immutable
I am not modifying $0 anywhere. I am just access the property
Please help
I think initialising timeAsDate
counts as mutating so a lazy variable might not be what you need.
You could try a computed variable instead so long as you don't need to manually change it.
var timeAsDate: Date? {
return MovementFormattedData.getUTCDate(movementTime: movementTime)
}
No necessary to use lazy var
var timeAsDate: Date? {
return MovementFormattedData.getUTCDate(movementTime: movementTime)
}
lazy property is mutating getter and you cant use it with $0 because $0 is immutable
see following code block
struct MovementFormattedData {
... Other properties ...
var timeAsDate: Date? {
return MovementFormattedData.getUTCDate(movementTime: movementTime)
}
static func getUTCDate(movementTime:String?) -> Date? {
// return date
}
}
var movements: [MovementFormattedData] = []
self?.movements.firstIndex(where: { $0.timeAsDate ?? Date() > Date() })
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.