“[AnyObject]?"不可转换为“NSArray" [英] '[AnyObject]?' is not convertible to 'NSArray'

查看:30
本文介绍了“[AnyObject]?"不可转换为“NSArray"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误在 let USER 部分我正在使用新的 swift1.2请帮忙[AnyObject] 不可转换为 NSArray

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()
        }
    }
}

下面是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
}

推荐答案

试试这个.

   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
}

这篇关于“[AnyObject]?"不可转换为“NSArray"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆