0

This error always show when I try to retrieve and append the elements of an array in Parse to an array created in the code:

Could not cast value of type '__NSArrayM' (0x10b281b60) to 'NSString' (0x10bdc5b48).

However, when I use print, it works with no errors and I can get the data

 var query = PFQuery(className: "Courses")

 query.whereKey("subject", equalTo: "\((object["course"] as! String))")                        
 query.findObjectsInBackgroundWithBlock { (objects, error) in

 if let objects = objects
 {
     for object in objects
     {                       
         print(object["subject"] as! String)
         self.courses.append(object["subject"] as! String)
         print(object.valueForKey("timeToShow")!)  
         // it works to print the elemnts in array from parse self.dates.append((object.valueForKey("timeToShow") as! String))  
         // this line shows the error down !

         self.tableView.reloadData()
     }
}

Picture with more details

6
  • What line are you getting the error on? Commented Sep 1, 2016 at 21:26
  • this line self.courses.append(object["subject"] as! String) Commented Sep 1, 2016 at 21:29
  • And what is self.courses defined as? Is it a String array? Because object["subject'] is clearly an array (as the print statement shows) so that wouldn't work. Commented Sep 1, 2016 at 21:31
  • (object["subject"]) is not the deal it is defined as a string self.dates.append((object.valueForKey("timeToShow") as! String)) is the problem dates is defined as an array of strings and i want to retrieve from parse an array and i want to append them to dates which is again an array of strings Commented Sep 1, 2016 at 21:34
  • How is object["subject"] not the issue, you said that is where the error is occurring.. Commented Sep 1, 2016 at 21:44

1 Answer 1

3

From what you said:

  1. self.dates is an array of type String
  2. object.valueForKey("timeToShow") is an array of type String
  3. You want to append the values in object.valueForKey("timeToShow") to the end of self.dates

So instead of casting to a String and trying to append, you need to append all of the values of the array (note this depends on the version of Swift that you are using):

let times = object.valueForKey("timeToShow") as! [String]
self.dates += times 

// Or:
self.dates.extend(times) // Swift 1.2
self.dates.appendContentsOf(btimes) // Swift 2
self.dates.append(contentsOf: times) // Swift 3

Appending an array to another array taken from this StackOverflow example.

Sign up to request clarification or add additional context in comments.

3 Comments

in the second line there is an error "Binary operator +=cannot be applied of type ['String'] and 'AnyObject' what is the alternative way to do it =( ?
@AhmadKhawatmi just updated the answer, you need to cast the AnyObject to [String] (let times = object.valueForKey("timeToShow") as! [String] )
AnyObject to [String] (let times = object.valueForKey("timeToShow") as! [String] ) it didnt work =( but i just solved it with your help using another tricky way after trying for hours lol i used this var times = object.valueForKey("timeToShow")! times = String(times) self.dates.append(times as! String) sorry for the confusion that i made and thanks a lot Carter =)

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.