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

halfer
  • 19,824
  • 17
  • 99
  • 186
  • What line are you getting the error on? – Carter Sep 01 '16 at 21:26
  • this line self.courses.append(object["subject"] as! String) – Ahmad Khawatmi Sep 01 '16 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. – Carter Sep 01 '16 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 – Ahmad Khawatmi Sep 01 '16 at 21:34
  • How is `object["subject"]` not the issue, you said that is where the error is occurring.. – Carter Sep 01 '16 at 21:44
  • self.dates.append((object.valueForKey("timeToShow") as! String)) // this line shows the error down the error is in this code i said this – Ahmad Khawatmi Sep 01 '16 at 21:46

1 Answers1

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.

Community
  • 1
  • 1
Carter
  • 3,053
  • 1
  • 17
  • 22
  • 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 =( ? – Ahmad Khawatmi Sep 01 '16 at 22:08
  • @AhmadKhawatmi just updated the answer, you need to cast the `AnyObject` to `[String]` (`let times = object.valueForKey("timeToShow") as! [String] `) – Carter Sep 01 '16 at 23:17
  • 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 =) – Ahmad Khawatmi Sep 01 '16 at 23:31