解析Swift自定义对象ID?

时间:2015-08-23 02:15:56

标签: ios swift parse-platform

我想创建一个具有自定义Object Id的对象。我想这样做的原因是因为如果尝试创建具有相同数据的行,我希望save方法失败。例如:如果您尝试使用已经拍摄的电子邮件注册,则Parse会自动无法保存。对于非User类的类中的数据,我希望发生同样的事情。

或者,如果我知道如何做到这一点,我可以让事情发挥作用

if(class contains column with "this string of data"){
    do nothing}
else{
    save "this string of data"}

我正在做的是实施一个向上投票和向下投票的系统,我不希望用户能够在一个帖子上多次投票。

每次用户投票时,它都会进入解析一行数据的行,该列的字符串类型将是post + voter的内容。所以如果post + voter的内容组合会再次尝试保存,我希望它失败。 这是tableview中单元格的代码。这是downVote代码。 (upVote代码非常相似)

//intVotes goes into a label with the amount of votes 
//thisReview contains the content of the post
//downVote() increments number of votes by -1 in Parse

@IBAction func downVote(sender: AnyObject) {

    var reviewQuery: PFQuery = PFQuery(className: "reviews")
    reviewQuery.whereKey("content", equalTo: reviewTextView.text)
    reviewQuery.findObjectsInBackgroundWithBlock{
        (objects:[AnyObject]!, error:NSError!)->Void in

        if error == nil{

            for object in objects{
                let review:PFObject = object as! PFObject
                self.defaults.setValue(review["content"], forKey: "thisReview")
            }
        }
    }

var vote:PFObject = PFObject(className: "votes")

    if String(stringInterpolationSegment: vote.valueForKey("votes")) != String(stringInterpolationSegment: defaults.valueForKey("thisReview")) + String(stringInterpolationSegment: PFUser.currentUser()){
        vote.setValue(String(stringInterpolationSegment: defaults.valueForKey("thisReview")) + String(stringInterpolationSegment: PFUser.currentUser()), forKey: "objectId")
        vote.saveInBackgroundWithBlock {
            (succeeded: Bool, error: NSError!) -> Void in
            if error == nil {

                self.downVote()
                var intVotes: Int = self.votes.text!.toInt()!
                intVotes = intVotes - 1
                self.votes.text = "\(intVotes)"
            } else {
                println ("Failed")
            }
        }

    }
    else{
        //do nothing
    }
 }   

这个ALMOST有效,除了if语句中的等式左边(vote.valueForKey部分)每次都返回nil。

1 个答案:

答案 0 :(得分:0)

你应该只翻译Parse已经在他们的名为“AnyPic”的教程中向他们展示了他们在ObjC中的上/和上/下投票“像按钮”系统,这里是转换为Swift的主要元素,你必须导入PAPCache.h / m PAPConstants.h / m和PAPUtility.h / m进入您的项目并构建一个桥接标题以链接到这些文件。从那里,您需要弄清楚的唯一部分是如何修改以下代码以满足您的需求,但这已经有效,您只需要重新排列变量,并将内容更改为Optionals或NON-optionals以使其工作为了你。我假设这个方法是最好的方法,因为Parse在他们的ObjC BIG教程中使用这个方法来展示应用程序“AnyPic”的所有内容,你必须在PFObject类型的UIVIewController中声明一个属性以及更多,但这是代码的内脏。没有人会给你完整的代码,因为这会占用很多代码,如果你在Swift中做到这一点会更容易,而不需要做我在过去30分钟内做的事情。将其与ObjC代码进行桥接将是费力的,但这就是它的完成方式。祝你好运!

您需要声明的内容,最低限度:

var likeUsers : NSArray?
var likeButton: UIButton?
var someObject: PFObject?

方法:

func didTapLikeButtonAction(button: UIButton) {
    var liked = Bool()
    liked  = !button.selected

    button.removeTarget(self, action: "didTapLikeButtonAction(button)", forControlEvents: UIControlEvents.TouchUpInside)
    var originalLikeUsersArray =  NSArray()
    originalLikeUsersArray = self.likeUsers!
    var newLikeUsersSet = NSMutableSet(capacity: self.likeUsers!.count)
    for id in self.likeUsers! {
        if id.objectId != PFUser.currentUser()?.objectId {
            newLikeUsersSet.addObject(id)
        }
    }
    if liked {
        PAPCache.sharedCache().incrementLikerCountForPhoto(self.someObject)
        newLikeUsersSet.addObject(PFUser.currentUser()!)
    } else {
        PAPCache.sharedCache().decrementLikerCountForPhoto(self.someObject)
    }
    PAPCache.sharedCache().setPhotoIsLikedByCurrentUser(self.someObject, liked: liked)
    likeUsers = newLikeUsersSet.allObjects
    if liked {
        PAPUtility.likePhotoInBackground(self.someObject, block: {(success: Bool, error: NSError?) -> Void in
            if !success {
                button.addTarget(self, action: "didTapLikeButtonAction(button)", forControlEvents:UIControlEvents.TouchUpInside)
                self.likeUsers = originalLikeUsersArray
                self.setLikeButtonState(true)
            }
        })
    } else {
        PAPUtility.unlikePhotoInBackground(self.someObject, block: {(success: Bool, error: NSError?) -> Void in
            if !success {
                button.addTarget(self, action: "didTapLikeButtonAction(button)", forControlEvents:UIControlEvents.TouchUpInside)
                self.likeUsers = originalLikeUsersArray
                self.setLikeButtonState(false)
            }
        })
    }
}

从ObjC翻译的LikeButtonFunction

func setLikeButtonState(selected: Bool) {
    if selected {
        likeButton?.titleEdgeInsets = UIEdgeInsetsMake( -1.0, 0.0, 0.0, 0.0)
    } else {
        likeButton?.titleEdgeInsets = UIEdgeInsetsMake( 0.0, 0.0, 0.0, 0.0)
    }
    likeButton?.selected = selected
}

您需要从此处下载“Anypic”项目:

https://parse.com/tutorials/anypic

你将需要导入Swift项目,最低限度,以下内容:

#import "PAPCache.h"
#import "PAPConstants.h"
#import "PAPUtility.h"

然后,您需要重新编码PAPCache,PAPUtility和PAPConstants以满足您的需求。祝你好运,由于Swift,这将是很多编码,但是如果你使用ObjC可能接近没有编码,因为Parse一遍又一遍地说他们不会在Swight测试之前大力推进Swift。他们最后一次再说这个是两个月前的六月。

原始代码,来自Objective-C,有些东西我没有为你做,因为这是你的应用程序,你必须自己做这些事情,如果你认为有必要,再次,ObjC代码完成,但是你选择使用Swift,所以重新编码已基本提供的内容,“开箱即用”就是你要处理的内容:

- (void)didTapLikePhotoButtonAction:(UIButton *)button {

    BOOL liked = !button.selected;
    [button removeTarget:self action:@selector(didTapLikePhotoButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self setLikeButtonState:liked];

    NSArray *originalLikeUsersArray = [NSArray arrayWithArray:self.likeUsers];
    NSMutableSet *newLikeUsersSet = [NSMutableSet setWithCapacity:[self.likeUsers count]];

    for (PFUser *likeUser in self.likeUsers) {
        if (![[likeUser objectId] isEqualToString:[[PFUser currentUser] objectId]]) {
            [newLikeUsersSet addObject:likeUser];
        }
    }

    if (liked) {
        [[PAPCache sharedCache] incrementLikerCountForPhoto:self.photo];
        [newLikeUsersSet addObject:[PFUser currentUser]];
    } else {
        [[PAPCache sharedCache] decrementLikerCountForPhoto:self.photo];
    }

    [[PAPCache sharedCache] setPhotoIsLikedByCurrentUser:self.photo liked:liked];

    [self setLikeUsers:[newLikeUsersSet allObjects]];

    if (liked) {
        [PAPUtility likePhotoInBackground:self.photo block:^(BOOL succeeded, NSError *error) {
            if (!succeeded) {
                [button addTarget:self action:@selector(didTapLikePhotoButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                [self setLikeUsers:originalLikeUsersArray];
                [self setLikeButtonState:NO];
            }
        }];
    } else {
        [PAPUtility unlikePhotoInBackground:self.photo block:^(BOOL succeeded, NSError *error) {
            if (!succeeded) {
                [button addTarget:self action:@selector(didTapLikePhotoButtonAction:) forControlEvents:UIControlEventTouchUpInside];
                [self setLikeUsers:originalLikeUsersArray];
                [self setLikeButtonState:YES];
            }
        }];
    }

    [[NSNotificationCenter defaultCenter] postNotificationName:PAPPhotoDetailsViewControllerUserLikedUnlikedPhotoNotification object:self.photo userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:liked] forKey:PAPPhotoDetailsViewControllerUserLikedUnlikedPhotoNotificationUserInfoLikedKey]];
}

- (void)didTapLikerButtonAction:(UIButton *)button {
    PFUser *user = [self.likeUsers objectAtIndex:button.tag];
    if (delegate && [delegate respondsToSelector:@selector(photoDetailsHeaderView:didTapUserButton:user:)]) {
        [delegate photoDetailsHeaderView:self didTapUserButton:button user:user];
    }    
}

- (void)didTapUserNameButtonAction:(UIButton *)button {
    if (delegate && [delegate respondsToSelector:@selector(photoDetailsHeaderView:didTapUserButton:user:)]) {
        [delegate photoDetailsHeaderView:self didTapUserButton:button user:self.photographer];
    }    
}

上面的ObjC代码来自Parse.com AnyPic github repo的文件“PAPPhotoDetailsHeaderView.m”,您可以在我们上面列出的网站上的网站上看到他们的OBJECTIVE-C教程。

顺便说一下,这对我来说很有用,它确实为我编译,但我不使用Swift,所以这对我来说没用,但如果你设置正确,你就不需要了搞乱PAPCache,PAPConstants和PAPUtility。但这假设你精通Parse的所有事情。祝你好运。

我刚刚添加了从ObjC翻译的likeButtonOn / Off函数

相关问题