我有一个 UserFeed 课程,我存储了所有帖子。我有一个 UserProfile 类,我存储了所有用户详细信息(名称,年龄,职业)。目前我有一个指向UserProfile的指针。但是当我尝试设置职业标签时,它会给我 nil 。
// PostsCollectionViewCell.swift
import UIKit
import DateTools
import Parse
class PostsCollectionViewCell: UICollectionViewCell {
var post: Post! {
didSet {
updateUI()
}
}
@IBOutlet var postLabel: UILabel!
@IBOutlet var genderLabel: UILabel!
@IBOutlet var occupationLabel: UILabel!
@IBOutlet var timeLabel: UILabel!
@IBOutlet var likeButton: UIButton!
func layoutSubview() {
super.layoutSubviews()
}
private func updateUI() {
occupationLabel?.text! = post.userProfile.occupation
timeLabel?.text! = post.createdAt?.shortTimeAgoSinceDate(NSDate()) ?? ""
postLabel?.text! = post.postText
}
@IBAction func likeButtonDidTouch(sender: AnyObject) {
}
}
在我的DiscoverViewController中发布查询
func queryForPosts() {
PFGeoPoint.geoPointForCurrentLocationInBackground { (geopoint, error) in
if !(error != nil) {
if let geoPoint = geopoint {
let query = PFQuery(className: "UserFeed")
query.whereKey("location", nearGeoPoint: geoPoint, withinMiles: 5)
query.addDescendingOrder("createdAt")
query.includeKey("userProfile")
query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if error == nil {
if let postObjects = objects as? [PFObject] {
self.posts.removeAll()
for postObject in postObjects {
let post = postObject as! Post
self.posts.append(post)
}
self.collectionView.reloadData()
}
} else {
print("\(error!.localizedDescription)")
}
})
}
}
}
}
后子类化
import UIKit
import Parse
public class Post: PFObject, PFSubclassing{
// MARK: - Public API
@NSManaged public var username: PFUser
@NSManaged public var location: PFGeoPoint?
@NSManaged public var userProfile: String!
@NSManaged public var postText: String!
@NSManaged public var numberOfLikes: Int
@NSManaged public var likedUserIdCollection: [String]!
public func incrementNumberOfLikes() {
numberOfLikes++
self.saveInBackground()
}
//Mark: - Convience init
init(username: PFUser, location: PFGeoPoint?, userProfile: String, postText: String, numberOfLikes: Int) {
super.init()
self.username = username
self.location = location
self.userProfile = userProfile
self.postText = postText
self.numberOfLikes = numberOfLikes
self.likedUserIdCollection = [String]()
}
override init() {
super.init()
}
//MARK: - Like / Dislike
public func like(){
let currentUserObjectId = PFUser.currentUser()!.objectId!
if !likedUserIdCollection.contains(currentUserObjectId) {
numberOfLikes++
likedUserIdCollection.insert(currentUserObjectId, atIndex: 0)
self.saveInBackground()
}
}
public func dislike() {
let currentUserObjectId = PFUser.currentUser()!.objectId!
if likedUserIdCollection.contains(currentUserObjectId) {
numberOfLikes--
for (index, userId) in likedUserIdCollection.enumerate() {
if userId == currentUserObjectId {
likedUserIdCollection.removeAtIndex(index)
break
}
}
self.saveInBackground()
}
}
// MARK: - PFSubClassing
override public class func initialize() {
struct Static {
static var onceToken : dispatch_once_t = 0;
}
dispatch_once(&Static.onceToken) {
self.registerSubclass()
}
}
public static func parseClassName() -> String {
return "UserFeed"
}
}
答案 0 :(得分:4)
对象在初始化之前无法存储值,需要在代码
中调用init以下init(用户名:PFUser,位置:PFGeoPoint?,userProfile:String,postText:String,numberOfLikes:Int){
super.init()
self.username = username
self.location = location
self.userProfile = userProfile
self.postText = postText
self.numberOfLikes = numberOfLikes
self.likedUserIdCollection = [String]()
}
答案 1 :(得分:2)
需要初始化Post对象,好像你没有成功。