Swift instagram 克隆:加载关注的用户帖子
Posted
技术标签:
【中文标题】Swift instagram 克隆:加载关注的用户帖子【英文标题】:Swift instagram clone : loading followed user post 【发布时间】:2014-09-11 22:14:02 【问题描述】:我正在尝试基于 Ribbit tuto 用 swift 制作一个 instagram 克隆。人们可以分享照片并关注其他用户等。我使用 Parse 在用户之间创建了PFRelation
。我现在想做的是显示在我的WalltableViewController
,只有关注的用户发布。
我创建了一个函数来将所有帖子加载到一个名为 timeLineData
的 NSMutable
数组中。
我什至制作了一个函数来获取 NSMutableArray
的关注用户,称为 followedFriends
..
但我没有成功使用followedFriends
过滤加载帖子功能。我有这个错误:Terminating app due to uncaught exception 'NSInvalidArgumentException'
,原因:'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()
【问题讨论】:
这一行的错误是:findPost.whereKey("from", equalTo: followFriends) 吗?因为看起来您正在尝试将单个字段的值与整个数组进行比较。 【参考方案1】:FollowedFriends 是一个数组,所以你需要使用containedIn
。如果您要与一个单独的对象进行比较,则使用 equalTo
。改成这个。
findPost.whereKey("from", containedIn:followedFriends)
【讨论】:
【参考方案2】:我也创建了这个解决方案:
//query for the friends of the user
var friendsRelation: AnyObject! = PFUser.currentUser().objectForKey("KfriendsRelation")
var findFriends : PFQuery = friendsRelation.query()
//using the friends from the query above, we find all the posts of the friends of the current user
var findPosts:PFQuery = PFQuery(className: "UserPost")
findPosts.whereKey("from", matchesQuery:findFriends)
findPost.orderByDescending("createdAt")
//run the query
findPosts.findObjectsInBackgroundWithBlock (objects:[AnyObject]!, error:NSError!) -> Void in
if !(error != nil)
//found posts
self.tableView.reloadData()
else
// Log details of the failure
println("error loadind posts ")
【讨论】:
以上是关于Swift instagram 克隆:加载关注的用户帖子的主要内容,如果未能解决你的问题,请参考以下文章
如何为 Instagram 克隆构建 Cloud Firestore 数据?
Swift / Instagram API - 如何使用 Instagram 应用进行身份验证
Swift:在 UITableViewCell 中异步加载图像 - 自动布局问题
xml Android空布局模板。来源:工具栏和BottomNavigationView(第2部分) - [构建Instagram克隆] https://www.youtube.com/watch?