本文介绍了PFUser返回NIl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图通过部分遵循Makestagram教程来在解析中设置一个类似的系统.
I am attempting to set up a like system in parse by partially following the Makestagram tutorial.
我花了几个小时尝试调试它,但是没有运气.
I've spent hours trying to debug this and have had no luck..
DetailViewController:
@IBAction func likeButtonTapped(sender: AnyObject) {
merchant!.toggleLikePost(PFUser.currentUser()!)
}
此语句返回:
unexpectedly found nil while unwrapping an Optional value
error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0).
The process has been returned to the state before expression evaluation.
其余代码如下:
DetailViewController:
DetailViewController:
var likeBond: Observable<[PFUser]?>! = Observable(nil)
var parseHelper = ParseHelper()
var likeDisposable: DisposableType?
var merchant: Merchant? {
didSet {
likeDisposable!.dispose()
if let merchant = merchant {
likeDisposable = merchant.likes.observe { (value: [PFUser]?) -> () in
if let value = value {
self.likesButton.selected = value.contains(PFUser.currentUser()!)
self.likesImageView.hidden = (value.count == 0)
} else {
self.likesLabel.text = ""
self.likesButton.selected = false
self.likesImageView.hidden = true
}
}
}
}
}
商人阶层:
func toggleLikePost(user: PFUser) {
if (doesUserLikeMerchant(user)) {
// if image is liked, unlike it now
// 1
likes.value = likes.value?.filter { $0 != user }
ParseHelper.unlikePost(user, merchant: self)
} else {
// if this image is not liked yet, like it now
// 2
likes.value?.append(user)
ParseHelper.likePost(user, merchant: self)
}
}
func doesUserLikeMerchant(user: PFUser) -> Bool {
if let likes = likes.value {
return likes.contains(user)
} else {
return false
}
}
ParseHelper类:
ParseHelper Class:
static func likePost(user: PFUser, merchant: Merchant) {
let likedObject = PFObject(className: ParseLikeClass)
likedObject.setObject(user, forKey: parseLikeFromUser)
likedObject.setObject(merchant, forKey: ParseLikeToPost)
likedObject.saveInBackgroundWithBlock(ErrorHandling.errorHandlingCallback)
}
static func unlikePost(user: PFUser, merchant: Merchant) {
let query = PFQuery(className: ParseLikeClass)
query.whereKey(parseLikeFromUser, equalTo: user)
query.whereKey(ParseLikeToPost, equalTo: merchant)
query.findObjectsInBackgroundWithBlock {
(results: [PFObject]?, error: NSError?) -> Void in
if let error = error {
ErrorHandling.defaultErrorHandler(error)
}
if let results = results {
for likes in results {
likes.deleteInBackgroundWithBlock(ErrorHandling.errorHandlingCallback)
}
}
}
}
感谢您的帮助..如果需要,我将添加git repo.
Any help is appreciated.. I will add the git repo if needed.
推荐答案
您需要首先在委托中调用PFUser.enableAutomaticUser().调用此方法后,currentUser将始终具有一个值.
You need to call PFUser.enableAutomaticUser() in your delegate first.After calling this method, currentUser will always have a value.
这篇关于PFUser返回NIl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!