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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
viktor
  • 31
  • 9
  • 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]` – luk2302 Jun 20 '15 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. – viktor Jun 21 '15 at 11:04

1 Answers1

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") 
}
Icaro
  • 14,585
  • 6
  • 60
  • 75
  • 1
    I think you have an extra `?`. I don't have Xcode open... but shouldn't it just be `user["key"].last as? String`? – nhgrif Jun 20 '15 at 12:30
  • @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 – Icaro Jun 20 '15 at 12:54
  • You *are* correct. The array could be empty and `last` could return `nil`, but the `?` is unnecessary here anyway. – nhgrif Jun 20 '15 at 12:55
  • @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 Jun 21 '15 at 22:12
  • @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. – viktor Jun 22 '15 at 10:44
  • @viktor Sometimes a break is all we need. Are you able to check the green box next to the question so it is marked as resolved, otherwise other people may try to answer it too! – Icaro Jun 22 '15 at 22:09