我正在构建一个小的 iPhone 应用程序,它允许用户记录他们可能与朋友一起玩的游戏的分数。我现在需要在 Core Data 中使用关系,但似乎不能让它正常工作。
我希望能够将新数据添加到一个实体中,同时创建与不同实体中现有数据的关系。我怎样才能做到这一点?
请注意,我是 Core Data 的新手,今天大部分时间都在试图解决这个问题,但运气不好。任何帮助将不胜感激。
我有 3 个实体:Scores、Games和Players。
分数属性:date
、player1Score
和player2Score
。status
游戏属性:title
.
球员属性:name
.
我在( Scores <<--->> Games)和(Scores <<--->> Players)之间有多对多的关系
我已经有了游戏和玩家的列表。用户选择哪个游戏和谁在玩,并使用此信息在与所选游戏和玩家的关系的分数实体中创建一组对象。
这是我的来源:
// Scores.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class Games, Players;
@interface Scores : NSManagedObject
@property (nonatomic, retain) NSDate * date;
@property (nonatomic, retain) NSNumber * player1Score;
@property (nonatomic, retain) NSNumber * player2Score;
@property (nonatomic, retain) NSNumber * status;
@property (nonatomic, retain) NSSet *game;
@property (nonatomic, retain) NSSet *player;
@end
@interface Scores (CoreDataGeneratedAccessors)
- (void)addGameObject:(Games *)value;
- (void)removeGameObject:(Games *)value;
- (void)addGame:(NSSet *)values;
- (void)removeGame:(NSSet *)values;
- (void)addPlayerObject:(Players *)value;
- (void)removePlayerObject:(Players *)value;
- (void)addPlayer:(NSSet *)values;
- (void)removePlayer:(NSSet *)values;
@end
// SC_ScoreViewController.h
#import <UIKit/UIKit.h>
@interface SC_ScoreViewController : UIViewController
@property (strong) NSIndexPath *game;
@property (strong) NSIndexPath *playerOne;
@property (strong) NSIndexPath *playerTwo;
@property (weak, nonatomic) IBOutlet UILabel *playerOneName;
@property (weak, nonatomic) IBOutlet UILabel *playerTwoName;
@end
// SC_ScoreViewController.m
#import "SC_ScoreViewController.h"
#import "Scores.h"
#import "Players.h"
#import "Games.h"
@interface SC_ScoreViewController ()
@end
@implementation SC_ScoreViewController
@synthesize game;
@synthesize playerOne;
@synthesize playerTwo;
// managedObjectContext (context)
- (NSManagedObjectContext *)managedObjectContext {
NSManagedObjectContext *context = nil;
id delegate = [[UIApplication sharedApplication] delegate];
if ([delegate respondsToSelector:@selector(managedObjectContext)]) {
context = [delegate managedObjectContext];
}
return context;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Setup Nav Title
self.navigationItem.title = [self.game valueForKey:@"title"];
// Setup Player's Names
[self.playerOneName setText:[self.playerOne valueForKey:@"name"]];
[self.playerTwoName setText:[self.playerTwo valueForKey:@"name"]];
Scores * newEntry = [NSEntityDescription insertNewObjectForEntityForName:@"Scores" inManagedObjectContext:self.managedObjectContext];
newEntry.player1Score = 0;
newEntry.player2Score = 0;
newEntry.status = nil;
newEntry.player = self.playerOne; // Incompatible pointer types assigning to NSSet from NSIndexPath
NSError *error;
if (![self.managedObjectContext save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
我希望我已经清楚并提供了足够的信息。这是我的第一个问题,所以我希望一切顺利。任何帮助都会很棒,谢谢。