2

I have been making an app in Swift but I keep getting an error in my TableViewController class. I could not find any way to fix this and kept getting this error :

override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!  {
        let cell : TextTableViewCell = tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath : indexPath!) as TextTableViewCell

    let madan : PFObject = self.timeLineData.objectAtIndex(indexPath!.row) as PFObject

    cell.timestampLabel.alpha = 0
    cell.usernameLabel.alpha = 0
    cell.madanTextView.alpha = 0

    cell.madanTextView.text = madan.objectForKey("content") as String

    var dateFormatter : NSDateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "MM-dd-yyyy HH:mm"
    cell.timestampLabel.text = dateFormatter.stringFromDate(madan.createdAt)

    var findSender : PFQuery = PFUser.query()
    findSender.whereKey("objectId", equalTo: madan.objectForKey("sender").objectId)
    var i = 1
    findSender.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]!, error: NSError!) -> Void in
        if !error {
            let user : PFUser = (objects as NSArray).lastObject as PFUser // Error here

            cell.usernameLabel.text = user.username 

            UIView.animateWithDuration(0.5, animations: {

                cell.timestampLabel.alpha = 1
                cell.usernameLabel.alpha = 1
                cell.madanTextView.alpha = 1

            })
        }
    }    
    return cell
}

I cannot find any way to fix this.

FlipFloop
  • 1,143
  • 1
  • 12
  • 22
Maanit
  • 186
  • 2
  • 3
  • 11
  • Check the length of the objects array – Kevin Jul 12 '14 at 17:12
  • possible duplicate of [Fatal error: unexpectedly found nil while unwrapping an Optional values](http://stackoverflow.com/questions/24643522/fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-values) – jww Sep 14 '14 at 15:41

2 Answers2

2

It looks as if objects is coming back as nil. First check for nil:

if !error {
    if let actualObjects = objects {
        let possibleUser = (actualObjects as NSArray).lastObject as? PFUser
        if let user = possibleUser {
            cell.usernameLabel.text = user.username
            // ...
        }
    }
}

Note: I changed your as to as?. lastObject is already returning an Optional and so you might as well let the execution continue if the last object cannot be converted to PFUser. Also, because lastObject might return nil, you also need to check that for nil.

drewag
  • 88,073
  • 28
  • 133
  • 127
  • Thanks, the error is gone, but when I run the program the cell is blank. Is there a way to fix that – Maanit Jul 12 '14 at 18:24
  • @Maanit you need to figure out why `findObjectsInBackgroundWithBlock` is returning nil for `objects` – drewag Jul 12 '14 at 18:25
0

To avoid this Error, must have to make sure one importing thing while declaration of Object :

var isActive: Bool?

? is the important to add, so now you can check nil values whenever you are, accessing this variable named isActive like below:

  1. if isActive == nil
  2. if let _isActive = isActive as Bool?

So Optional values must be declared with the ?.

Rodrigo Taboada
  • 2,659
  • 4
  • 21
  • 26
Sandip
  • 21
  • 3