ここでは、コアデータ2エンティティResistration &ユニークでリレーションシップネームはroshanです。リレーションシップは1対1です。 データを挿入しようとしましたが、コードが長すぎてコアデータに挿入データを実行するための短いコードを作成する手助けがありません。コアデータにデータを挿入
- (IBAction)submitData:(id)sender {
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate manageObjectContext];
Resgistration *newContact = [NSEntityDescription insertNewObjectForEntityForName:@"Resgistration" inManagedObjectContext:context];
//newContact = [NSEntityDescription insertNewObjectForEntityForName:@"Resgistration" inManagedObjectContext:context];
Unique *number = [NSEntityDescription insertNewObjectForEntityForName:@"Unique" inManagedObjectContext:context];
//number = [NSEntityDescription insertNewObjectForEntityForName:@"Unique" inManagedObjectContext:context];
[number setValue:_numberText.text forKey:@"number"];
[number setValue:_studyText.text forKey:@"study"];
NSMutableSet *roshanSet = [[NSMutableSet alloc] init];
[roshanSet addObject:number];
[newContact addRoshan:roshanSet];
[newContact setValue:number forKey:@"roshan"];
//[newContact setValue:_numberText.text forKey:@"number"];
[newContact setValue:_nameText.text forKey:@"name"];
[newContact setValue:_addressText.text forKey:@"address"];
[newContact setValue:_emailText.text forKey:@"email"];
[newContact setValue:_othernoText.text forKey:@"otheNo"];
[newContact setValue:_hobbyText.text forKey:@"hobby"];
[newContact setValue:_contactText.text forKey:@"contact"];
NSError *error;
[context save:&error];
_nameText.text = @"";
_addressText.text = @"";
_emailText.text = @"";
_othernoText.text = @"";
_hobbyText.text = @"";
_contactText.text = @"";
_numberText.text = @"";
TableViewController *table = [self.storyboard instantiateViewControllerWithIdentifier:@"TableViewController"];
[self.navigationController pushViewController:table animated:YES];
}
ユニーク+ CoreDataClass.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class Resgistration;
NS_ASSUME_NONNULL_BEGIN
@interface Unique : NSManagedObject
@end
NS_ASSUME_NONNULL_END
#import "Unique+CoreDataProperties.h"
ユニーク+ CoreDataClass.m
#import "Unique+CoreDataClass.h"
#import "Resgistration+CoreDataClass.h"
@implementation Unique
@end
Resgistration + CoreDataClass.h
import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class Unique;
NS_ASSUME_NONNULL_BEGIN
@interface Resgistration : NSManagedObject
@end
NS_ASSUME_NONNULL_END
#import "Resgistration+CoreDataProperties.h"
Resgistration + CoreDataClass.m
#import "Resgistration+CoreDataClass.h"
#import "Unique+CoreDataClass.h"
@implementation Resgistration
@end
、Resgistrationとユニークのクラス定義では、これらのプロパティを追加する必要がありますResgistrationクラスコードは – ddb
です。これはNSMAnagedObjectサブクラスです。更新されたコードを確認します。可能な小さなコードへの簡単な方法は –