1

error is in let USER part i am using new swift1.2 please help [AnyObject] is not convertible to NSArray

var PostData:NSMutableArray = NSMutableArray()

func loadData(){
    PostData.removeAllObjects()

    var findPostData:PFQuery = PFQuery(className: "Posts")

    findPostData.findObjectsInBackgroundWithBlock{
        (objects, error) -> Void in

        if error == nil {
            for object in objects! {
                let post:PFObject = object as! PFObject
                self.PostData.addObject(post)
            }

            let array:NSArray = self.PostData.reverseObjectEnumerator().allObjects
            self.PostData = NSMutableArray(array: array)

            self.tableView.reloadData()
        }
    }
}

Below is the cellForRowAtIndexPath method

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let postcell = tableView.dequeueReusableCellWithIdentifier("Postcell", forIndexPath: indexPath) as! PostscaamTableViewCell

    let post:PFObject = self.PostData.objectAtIndex(indexPath.row) as! PFObject

    postcell.postTimelineTextView.alpha = 0
    postcell.timeStamp.alpha = 0
    postcell.usernameLabel3.alpha = 0

    postcell.postTimelineTextView.text = post.objectForKey("Content") as! String

    var dateFormatter:NSDateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "dd-mm-yyyy HH:mm"
    postcell.timeStamp.text = dateFormatter.stringFromDate(post.createdAt!)

    var findPostedBy:PFQuery = PFUser.query()!

    findPostedBy.whereKey("objectId", equalTo: post.objectForKey("Postedby")!)

    findPostedBy.findObjectsInBackgroundWithBlock{
        (objects, error)->Void in
        if error == nil {
            let USER:PFUser = (objects as NSArray).lastObject as! PFUser
                postcell.usernameLabel3.text = USER.username

            UIView.animateWithDuration(0.2, animations: {
                postcell.postTimelineTextView.alpha = 1
                postcell.timeStamp.alpha = 1
                postcell.usernameLabel3.alpha = 1

            })
        }
    }

     return postcell
}
Antonio
  • 71,651
  • 11
  • 148
  • 165
motivation1
  • 131
  • 1
  • 11

2 Answers2

1

Try like this.

   override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let postcell = tableView.dequeueReusableCellWithIdentifier("Postcell", forIndexPath: indexPath) as! PostscaamTableViewCell

    let post:PFObject = self.PostData.objectAtIndex(indexPath.row) as! PFObject

    postcell.postTimelineTextView.alpha = 0
    postcell.timeStamp.alpha = 0
    postcell.usernameLabel3.alpha = 0

    postcell.postTimelineTextView.text = post.objectForKey("Content") as! String

    var dateFormatter:NSDateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "dd-mm-yyyy HH:mm"
    postcell.timeStamp.text = dateFormatter.stringFromDate(post.createdAt!)

    var findPostedBy:PFQuery = PFUser.query()!

    findPostedBy.whereKey("objectId", equalTo: post.objectForKey("Postedby")!)

    findPostedBy.findObjectsInBackgroundWithBlock{
        (objects, error) -> Void in
        if error == nil {
            if let users : [PFUser] = objects as? [PFUser] where users.count > 0 {
            let USER:PFUser = users.last!
                postcell.usernameLabel3.text = USER.username


                UIView.animateWithDuration(0.2, animations: {
                    postcell.postTimelineTextView.alpha = 1
                    postcell.timeStamp.alpha = 1
                    postcell.usernameLabel3.alpha = 1

                })
            }
        }
    }
    return postcell
}
Amit89
  • 3,000
  • 1
  • 19
  • 27
  • If it is working, accept the answer. It will help others as correct answer. – Amit89 May 24 '15 at 17:01
  • @Amit89 one more problem my post data is not being displayed i have tried to fix this but nothing is happening and no error is coming i think in swift 1.2 there is different method to display data from parse – motivation1 May 24 '15 at 17:07
  • For that you need to check Array content, if your array does not contain data how will you show? You have to debug using breakpoint or printing in the console. – Amit89 May 25 '15 at 05:24
  • @Amit89 ok now it is working but only usernameLabel3 is not displaying i am trying to debug – motivation1 May 25 '15 at 05:59
  • hey please help me in getting my label work i want help – motivation1 May 26 '15 at 18:19
  • Check the count of users Array. Print it println(users.count). If it is greater than zero, then println(USER). I doubt wether USER has value or not. – Amit89 May 26 '15 at 18:56
0

Try forced downcasting, such as (objects as! NSArray).

Schemetrical
  • 5,506
  • 2
  • 26
  • 43