I have graphql object returned from server.
Iam trying to edit the values inside the object but iam getting :
Cannot assign to property: '$0' is immutable
Attached an image

How can we update data inside retrieved graphql pbject ?
this is more of how swift works, don't have much to do with apollo.
If you want to update on server you need to do mutation.
If you want to mutate local object you need direct access to it.
Maybe something like:
for (index, productInSection) in self.sortedSectionsp[0].products!.enumerated() {
guard productInSection.fragments.productDetails.id == product.id else { continue }
self.sortedSections[0].products![index].fragments.productDetails.defaultImage = ""
}
@RolandasRazma is that mutable because you're mutating self.sortedSections[0].products![index] directly instead of productInSection? It's all structs but all the properties are vars so I'm not sure why it works one place and not the other
@sam961 It would help to know where you want to make the change - locally or on the server. @RolandasRazma is correct that if you want to make it on the server you need to use a mutation.
you can mutate productInSection as well, but that wouldn't be the same as in @sam961 example
I'm still not entirely clear on why - the only difference I see is that his is filtered into a new array before the forEach closure, which I didn't think would make the products immutable
products is mutable, $0 isn't
struct S {
var property: Int = 0
}
var array = [S(), S(), S()]
// Works
// array[0].property = 1
// Doesn't
// array.first!.property = 1
// Doesn't
// array.forEach({ $0.property = 1 })
if S would be class all of those would work
forEach takes closure as argument, so input argument struct can't be mutated
func forEach(_ body: (Element) throws -> Void) rethrows
You can iterate this way
struct Hello {
var propery: String
}
var array = [Hello(propery: "one"), Hello(propery: "two"), Hello(propery: "three")]
for index in (0 ..< array.count) where array[index].propery == "two" {
array[index].propery = "changed"
}
print(array)
Ah interesting - once you lose the direct reference it becomes immutable. TIL.
Actually i was trying to make changes locally ...
for (sectionIndex, sectionObject) in self.sortedSecions.enumerated() {
for (productIndex, product) in sectionObject.products!.enumerated() {
if product.fragments.productDetails.id == productNotification.id {
self.sortedSecions[sectionIndex].products![productIndex].fragments.productDetails.flashRemainingTime! = product.fragments.productDetails.flashRemainingTime!
}
}
}
This helped in updating the object locally.
The idea was the following ....
I have a tableview , each cell contain collectionview of products.
Some products has down time counter.
I think the best approach is to decrement the inital timer and update the product timer new value in the main object.
I know my question is out of the scope,
But i appreciate if some one tell me if this is the best approach.
Note : while updating the the local object locally ... The Big O for that will be NxM
Any suggestions ?
Attached an image for the page:

I think it depends on how you get how much time is remaining. You should be able to fairly easily calculate remaining time locally in the cell if you have the expiration time by comparing expiration vs Date() (ie, now).
That's certainly going to be easier than trying to update the cell by updating the underlying object on a timer.
@sam961 - Have we answered your question about the property being immutable? If so, I think maybe we should move further questions to Spectrum Chat since we're trying to reserve GitHub issues for problems or feature requests. Let me know if that works for you!
Thank you @designatednerd and @RolandasRazma ... yes it helped
Most helpful comment
forEach takes closure as argument, so input argument
structcan't be mutatedfunc forEach(_ body: (Element) throws -> Void) rethrowsYou can iterate this way