0

I'm trying to upload an array with the following code, but I am getting an error on the second to last line which says Cannot assign to value: function call returns immutable array

for object in objects! {
  let newstring = NSString(format: ".0f", self.slider.value)
  var newarray = [object.objectForKey("times")]
  newarray.append(newstring)
  object.objectForKey("times") = newarray
  object.saveInBackground()
}

"times" is of type array in parse by the way.

2 Answers 2

1

Rather than requesting the array and trying to set it you should be using setObject:forKey: to replace the existing value.

object.setObject(newarray, forKey: "times")
Sign up to request clarification or add additional context in comments.

2 Comments

Oh ok, so just replace the second to last line with that
Just to clarify why you got the error object.objectForKey("times") is a function call that returns the value at key "times", being an immutable array. You then tried to assign a value to that immutable array. Which even if the array was mutable would not update the value of the array in the dictionary. This is why you need to use setObject() like this answer gives.
0

May I suggest using the addObject:forKey: method instead?

for object in objects! {
  let newstring = NSString(format: ".0f", self.slider.value)
  object.addObject(newstring, forKey: "times")
  object.saveInBackground()
}

It is much cleaner and doesn't require retrieval :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.