iOS: saving nsmutablearray for user
I have looked into a few posts and saw some related posts on it but I still don't get it.
My app is a rating system. I want to store which posts the user rated so he/she can't rate again. Each post has an ID. How should I go ab开发者_JAVA百科out this?
If the rating is happening locally, you should look into CoreData to store your Post objects along with the user rating. You could then query the store using the post id to see whether it's been rated already.
A more lightweight approach (although I wouldn't recommend it) would be to save the rating directly to NSUserDefaults using a unique key, containing the post id. For example:
NSNumber *rating = [NSNumber numberWithFloat:0.8];
NSString *postKey = [NSString stringWithFormat:@"post_%i", post_id];
[[NSUserDefaults standardUserDefaults] setValue:rating forKey:postKey];
[[NSUserDefaults standardUserDefaults] synchronize];
To know whether the user has already rated a post, you would do so with:
if([[NSUserDefaults standardUserDefaults] valueForKey:postKey] == nil)
// post has not been rated
It is quite simple store the post id and rating in either database or in nsuserdefaults and check if the id is already rated restrict user to rate again.
精彩评论