I'm trying to save some data to core data from a different view and I keep getting a error when I press the button to save the data. The that I get is:
Unresolved error (null), (null)
Here's all my code:
SomeViewController.h
#import <UIKit/UIKit.h>
@class NameData;
@interface SomeViewController : UIViewController <NSFetchedResultsControllerDelegate> {
NSFetchedResultsController *fetchedResultsController;
NSManagedObjectContext* managedObjectContext;
NameData *nameData;
}
@property (nonatomic,retain) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic,retain) NSManagedObjectConte开发者_高级运维xt* managedObjectContext;
@property (nonatomic,retain) NameData *nameData;
- (IBAction)saveData;
@end
SomeViewController.m
#import "SomeViewController.h"
#import "NameData"
@implementation SomeViewController
@synthesize addTeam;
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Add Name;
}
- (IBAction)saveData{
NSString *string = @"demo";
nameData.nameContent = string;
nameData.name2Content = string;
nameData.name3Content = string;
NSError *error = nil;
if (![nameData.managedObjectContext save:&error]) {
// Handle error
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort(); // Fail
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[nameData release];
}
@end
NameData.h
#import <CoreData/CoreData.h>
@interface NameData : NSManagedObject
{
}
@property (nonatomic, retain) NSString * nameContent;
@property (nonatomic, retain) NSString * name2Content;
@property (nonatomic, retain) NSString * name3Content;
@end
NameData.m
#import "NameData"
@implementation NameData
@dynamic nameContent;
@dynamic name2Content;
@dynamic name3Content;
@end
save should be performed on the managedObjectContext instance, not the model itself.
Example:
NSError *error;
NSManagedObjectContext *moc = [self.fetchedResultsController managedObjectContext];
if (![moc save:&error]) {
NSLog(@"Failed to save to data store: %@ - %@",
[error localizedDescription], [error userInfo]);
}
精彩评论