0

I am making a chat app. I am having a problem while accessing an array of objects via indexPath(row) of a tableViewController. It is giving me an error: " Cannot subscript a value of type '[AnyObject]?'" The code is below. How do I fix this?

var user1 = PFUser.currentUser()
var user2 = self.objects[indexPath.row] as! PFUser
Dipen Panchasara
  • 13,480
  • 5
  • 47
  • 57
ojassethi
  • 379
  • 1
  • 5
  • 16
  • possible duplicate of [Cannot subscript a value of AnyObject? with an index of type Int](http://stackoverflow.com/questions/29874414/cannot-subscript-a-value-of-anyobject-with-an-index-of-type-int) – Dipen Panchasara Dec 16 '15 at 06:10
  • try unwrapping objects as follows: var user1 = PFUser.currentUser() var user2 = self.objects![indexPath.row] as! PFUser – Ajinkya Patil Dec 16 '15 at 06:22

1 Answers1

0

You may change the declaration of your self.objects to:

var objects = Array<PFUser>()

and use it like:

let user = self.objects[indexPath.row] as BusinessLocation!

Since you have specified the type, it won't consider it as AnyObject.

NeverHopeless
  • 11,077
  • 4
  • 35
  • 56