2017-07-12 71 views
0

在這裏,在我的核心數據兩個實體註冊&唯一與關係名稱是羅山,關係是一對一的。 我嘗試插入數據,但代碼太冗長了,任何人都可以幫助創建短代碼,以便在覈心數據中執行插入數據。在覈心數據中插入數據

- (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 
+0

發表您的獨特和註冊類代碼 – ddb

+0

它是我的NSMAnagedObject子類。檢查更新的代碼。 –

回答

0

這是你應該怎麼做,它只是一個更好一點,但是首先你需要添加類定義Resgistration和獨特的屬性,如您在覈心數據模型做

- (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") 
    } 
} 
+0

任何簡單的方法來可能的小代碼。 –

+0

每個語句都是必要的,沒有減少代碼的神奇方法,你可以用這種方式簡化語句 – ddb

+0

ohk。多謝,夥計。 –