1

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.

2
  • 1
    First you say "The column in Parse is an array of Strings", then you try as String, what a surprise that is not working, you should maybe use [String] Commented Jun 20, 2015 at 11:32
  • Indeed, Thanks a lot. I got confused as my objects in Parse were themselves arrays. I do admit that the mistake was silly. Thanks again for your response. Commented Jun 21, 2015 at 11:04

1 Answer 1

1

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") 
}
Sign up to request clarification or add additional context in comments.

6 Comments

I think you have an extra ?. I don't have Xcode open... but shouldn't it just be user["key"].last as? String?
@nhgrif You are correct, I was thinking because the array could be empty it would be an optional but I test in XCode and the compiler ask me to delete. I already fix the code above. Thanks
You are correct. The array could be empty and last could return nil, but the ? is unnecessary here anyway.
@viktor I am glad that helped you, please don't forget to check the issue as solved if it is fixed now, and good luck with your app!
@Icaro, everything solved and fine. Aside this slight mistake, I actually had the identical column in Parse being constantly added with objects as I was trying to modify them. I therefore had to stop the addition of objects before applying the modification. And while trying a considerable number of solutions without having actually identify the exact source of my problem, I got lost in the casting of my objects and so on. I had to take a good step back from the project, a bit of sleep and a game of tennis, and then fresh mind good to go. But thanks a million for your help and for the wishes.
|

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.