ios - Swift instagram 克隆 : loading followed user post

标签 ios swift parse-platform nsmutablearray

我正在尝试基于 Ribbit tuto 使用 swift 制作一个 Instagram 克隆。人们可以分享照片并关注其他用户等等。我使用 Parse 在用户之间创建了一个 PFRelation。我现在想做的是在我的 WalltableViewController 中显示,只有关注的用户发帖。

我创建了一个函数,将所有帖子加载到名为 timeLineDataNSMutable 数组中。

我什至做了一个函数来在一个名为 followedFriendsNSMutableArray 中获取关注的用户..

但我没有成功过滤带有followedFriends 的加载后功能。我有这个错误:Termating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot do a comparison query for type: __NSArrayM'

这是我的代码:

 import UIKit
 import QuartzCore

class WallTableViewController: UITableViewController, UINavigationControllerDelegate {

  @IBOutlet var posterAvatar: UIImageView!

   override func preferredStatusBarStyle() -> UIStatusBarStyle {
    return UIStatusBarStyle.LightContent
    }


     var timeLineData:NSMutableArray = NSMutableArray ()
    var followedFriends:NSMutableArray = NSMutableArray ()

     func loadUser () {

    followedFriends.removeAllObjects()
    var friendsRelation: AnyObject! = PFUser.currentUser().objectForKey("KfriendsRelation")


var findUser : PFQuery = friendsRelation.query()


findUser.findObjectsInBackgroundWithBlock { (objects:[AnyObject]!, error:NSError!) -> Void in
    if !(error != nil) {
        // The find succeeded.
        println("succesfull load Users")
        // Do something with the found objects
        for object  in objects  {
            self.followedFriends.addObject(object)
            println("users added to userlist")

            for item in self.followedFriends {
                println(item)                    }
        }
        self.tableView.reloadData()
    } else {
        // Log details of the failure
        println("error loadind user ")

    }

   }
  }


   func loadPost () {

  timeLineData.removeAllObjects()

   let currentUser = PFUser.currentUser()

    var findPost:PFQuery = PFQuery(className: "UserPost")


   findPost.whereKey("from", equalTo: followedFriends )


    findPost.orderByDescending("createdAt")


    findPost.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]! , error: NSError!) -> Void in
        if !(error != nil) {
            // The find succeeded.
            println("current user post finded")
            // Do something with the found objects
            for object  in objects  {
                self.timeLineData.addObject(object)
            }
            self.tableView.reloadData()
        } else {
            // Log details of the failure
            println("error loadind user post")
        }


    }

}


 override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return timeLineData.count
}

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell:WallTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as WallTableViewCell

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

//define the username

var findUser:PFQuery = PFUser.query()
findUser.whereKey("objectId", equalTo: userPost.objectForKey("from").objectId)

findUser.findObjectsInBackgroundWithBlock{
    (objects:[AnyObject]!, error:NSError!) -> Void in

    if !(error != nil) {
        if let user:PFUser = (objects as NSArray).lastObject as? PFUser {
            cell.usernameLabel.text = user.username

            // define avatar poster

               if let avatarImage:PFFile = user["profileImage"] as? PFFile {
                avatarImage.getDataInBackgroundWithBlock{(imageData:NSData!, error:NSError!)->    Void in

                    if !(error != nil) {

                        let image:UIImage = UIImage(data: imageData)


                        cell.posterAvatar.image = image as UIImage
                        cell.posterAvatar.layer.cornerRadius = 24
                        cell.posterAvatar.clipsToBounds = true

                    }

                }
            }
            else {
                cell.posterAvatar.image = UIImage(named: "Avatar-1")
                cell.posterAvatar.layer.cornerRadius = 24
                cell.posterAvatar.clipsToBounds = true
            }
        }

    }

}

//define the imagepost

let imagesPost:PFFile = userPost["imageFile"] as PFFile


imagesPost.getDataInBackgroundWithBlock{(imageData:NSData!, error:NSError!)-> Void in

    if !(error != nil) {

        let image:UIImage = UIImage(data: imageData)

        cell.imagePosted.image = image as UIImage


    }
    else {
        println("error")
    }
  }


      return cell
     }


  override func viewDidAppear(animated: Bool) {
     var currentUser = PFUser.currentUser()
       if (currentUser != nil) {
     println("User allready logued")
      }

    else {
    // Show the signup or login screen

    self.performSegueWithIdentifier("goToLogIn", sender: self)
    }

    }


  override func viewWillAppear(animated: Bool) {
   self.tableView.separatorColor = UIColor.whiteColor()

   tabBarController?.tabBar.tintColor = UIColor.whiteColor()
   self.loadUser()
  self.loadPost()
   self.tableView.reloadData()

  }
  }

最佳答案

FollowedFriends 是一个数组,所以需要使用containedIn。如果您要与一个单独的对象进行比较,则可以使用 equalTo。改成这样。

findPost.whereKey("from", containedIn:followedFriends)

关于ios - Swift instagram 克隆 : loading followed user post,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25797805/

相关文章:

ios - UITableViewCell 在出队时重绘

ios - 结束后台任务并等待应用程序再次激活 - BLE 处理数据

xcode - 导航栏颜色问题

ios - UITableView Cell 只需点击几下即可呈现 View Controller

javascript - 解析: How to restrict access to only certain properties of an object in Parse?

ios - 输入附件 View Swift 错误

ios - UISearchController 的 searchBar 没有填满整个宽度

javascript - Parse.com 和关闭

swift - 将特定图像从 Parse 加载到 UIPageViewController : SWIFT

ios - 如何将数据从 UIViewController 传递到嵌入在该 UIViewController 上的容器中的 UITableViewController?