Delete Objects From Parse/ Cannot cast to PFObject/ NSArrayM

184 views Asked by At

My problem is: I am querying user["key"] that I am attempting to assign to a new variable, as on the documentation.

The column in Parse is an array of Strings, in my query, if I println(user["key"]) it returns the correct objects.

However, when I do:

let myVariable = user["key"] as String

or

let myVariable = user["key"] as! String

I have the error:

Could not cast a value of type _NSArrayM to NSString

My end goal is to retrieve objects from Parse, and submit an "if" condition and then delete the results. For this I need to convert the objects into PFObject and this is where I struggle.

To add, I can only downcast to AnyObject! from let myVariable = user["key"]

and when I try to delete this object, I have the error

NSArrayM delete: unrecognised selector sent to instance.

Any help would be greatly appreciated.

1

There are 1 answers

6
Icaro On BEST ANSWER

Parse.com normally return an array, as you can see in your error message:

Could not cast a value of type _NSArrayM to NSString

__NSArrayM is a code-word for a mutable array, so you are trying to cast an array to a string.

If you are sure your query is return just one result you can retrieve just the last (and only) element in the array and cast to string.

if let myVariable = user["key"].last as? String{
    println("myVariable key is \(myVariable)"
} else { 
    println("Could not retrieve key") 
}