'[AnyObject]?不能转换为'NSArray'

时间:2015-05-24 07:45:21

标签: xcode swift

错误是让USER部分 我正在使用新的swift1.2 请帮忙 [AnyObject]无法转换为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()
        }
    }
}

以下是cellForRowAtIndexPath方法

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
}

2 个答案:

答案 0 :(得分:1)

试试这样。

   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
}

答案 1 :(得分:0)

尝试强制转发,例如(objects as! NSArray)