Ios 在核心数据中插入数据
这里,在我的核心数据中,两个实体重新注册&唯一,关系名称是roshan,关系是一对一。 我尝试插入数据,但代码太长,任何人都无法帮助创建短代码,以便在核心数据中执行插入数据Ios 在核心数据中插入数据,ios,core-data,insert,Ios,Core Data,Insert,这里,在我的核心数据中,两个实体重新注册&唯一,关系名称是roshan,关系是一对一。 我尝试插入数据,但代码太长,任何人都无法帮助创建短代码,以便在核心数据中执行插入数据 - (IBAction)submitData:(id)sender { AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; NSManagedObjectContext *context = [appDelegat
- (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];
}
Unique+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"
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"
重新注册+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"
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"
这是您应该做得更好一点的方法,但首先您需要在类定义中添加这些属性,以便重新注册和唯一,就像在核心数据模型中一样
- (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.number = _numberText.text;
number.study = _studyText.text;
NSMutableSet *roshanSet = [[NSMutableSet alloc] init];
[roshanSet addObject:number];
[newContact addRoshan:roshanSet];
newContact.roshan = number;
newContact.name = _nameText.text;
newContact.address = _addressText.text;
newContact.email = _emailText.text;
newContact.otheNo = _othernoText.text;
newContact.hobby = _hobbyText.text;
newContact.contact = _contactText.text;
NSError *error;
[context save:&error];
if(error == nil)
{
_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];
}
else
{
NSLog("error")
}
}
发布您的唯一和重新注册类代码这是我的NSMAnagedObject子类。检查更新的代码。每个语句都是必需的,没有神奇的方法来减少代码,你可以用这种方式简化语句快乐编码:)嘿,在这个数字中,值并没有插入核心数据。tableview中显示空值。创建另一个问题,并在此处发布TableViewController源代码和共享链接,以便我可以帮助我们。