开发者

Core Data Strings

Im new to using core data and having really basic problems. Im trying to have the user enter a string and then be able to save that string and allow it to be returned to them at some point. But i cannot seem to get it to save. In fact the program quits when I attempt to run the following method. I can post the rest of my project, but i thought maybe that would be annoying so let me know if seeing it in greater detail would help. Thanks so much.

James

.h: file

#import <UIKit/UIKit.h>
#import "People.h"

@class rootViewController;

@interface data : UIView <UITextFieldDelegate>{
    rootViewController *viewController;
    UITextField *firstName;
    UITextField *lastName;
    UITextField *phone;
    UIButton *saveButton;
    NSMutableDictionary *savedData;

    //Used for Core Data.
    NSManagedObjectContext *managedObjectContext;
    NSMutableArray *peopleArray;
}

@property (nonatomic, assign) rootViewController *viewController;
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSMutableArray *eventArray;


- (id)initWithFrame:(CGRect)frame viewController:(rootViewController *)aController;
- (void)setUpTextFields;
- (void)saveAndReturn:(id)sender; 
- (void)fetchRecords;

@end

.m file:

-(void)saveAndReturn:(id)sender{


    People *userEnteredName = (People *)[NSEntityDescription insertNewObjectForEntityForName:@"People" inManagedObjectContext:managedObjectContext];
    [userEnteredName setName:firstName.text];

    //NSError *error;
    //if (![managedObjectContext save:&error]) {开发者_开发技巧
        // This is a serious error saying the record could not be saved.
        // Advise the user to restart the application
    //}

    [peopleArray insertObject:userEnteredName atIndex:0];
}


From the error you gave you must have named the People object differently - in the model are you using "People" for both class and entity name (those can be the same)?

Edit:

After reviewing your code, you had multiple problems:

1) In the app delegate you did "[data alloc]" but no init. That was where you set the managed object context, but it was never used... not just because of the lack of an init but because...

2) The place where the data controller was really built and used from was the rootViewController. That's the one that is actually doing all the work, the one in the app delegate is just discarded.

3) So where to get the context then? Honestly the best spot is in the data controller, one fix I know worked was putting this line before every time the context was accessed:

#import "UserProfileAppDelegate.h"

// Then in the method before the use of context........

self.managedObjectContext = [((UserProfileAppDelegate *)[[UIApplication sharedApplication] delegate]) managedObjectContext];

When that was in place, the project ran. I think though you should put that into something like a viewDidLoad on the data controller (if it has a view that is ever used).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜