Im новое для Objective-C и CoreData и хочу изучить его и попробовать в Xcode 5, im пытается сделать это tutorial.iOS: CoreData Сохранение и поиск NSEntityDescription или kSetAttrDescription
Я следил за ним с некоторым другим именем таблицы CoreData, но я получаю некоторые ошибки с моим «ViewController.m» и не знаю, что делать с изменениями, я могу увидеть, что он переписывает изменения «NSEntityDescription» на «kSetAttrDescription», но dont знайте, правильно ли это или что неправильно делать, надейтесь, что кто-то может сказать, что нужно сделать - так что я знаю это в следующий раз.
Ошибка выдает
Описания ошибок
Мой ViewController.m код.
#import "ViewController.h"
@interface ViewController()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//Save data as CoreData, to CoreData Table "Kunder" field "navn", "adresse", "alder" from textfield _name.text, _adress.text, _age.text.
- (IBAction)saveData:(id)sender {
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSManagedObject *newContact;
newContact = [NSEntityDescription insertNewObjectForEntityForName:@"Kunder" inManagedObjectContext:context];
[newContact setValue: _name.text forKey:@"navn"];
[newContact setValue: _adress.text forKey:@"adresse"];
[newContact setValue: _age.text forKey:@"alder"];
//if textfield empty, then error else save and show label message "Kunde Gemt".
_name.text = @"";
_adress.text = @"";
_age.text = @"";
NSError *error;
[context save:&error];
_status.text = @"Kunde Gemt";
}
//Find-search for user by name.
- (IBAction)findKunde:(id)sender {
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Kunder" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
NSPredicate *pred =
[NSPredicate predicateWithFormat:@"(navn = %@)", _name.text];
[request setPredicate:pred];
NSManagedObject *matches = nil;
//if no user then error, else take name match and get "adresse" and "alder" from CoreData and show it in the text fields _adress.text and _age.text and show matche count in status label.
NSError *error;
NSArray *objects = [context executeFetchRequest:request error:&error];
if ([objects count] == 0) {
_status.text = @"Ingen fundet";
} else {
matches = objects[0];
_adress.text = [matches valueForKey:@"adresse"];
_age.text = [matches valueForKey:@"alder"];
_status.text = [NSString stringWithFormat: @"%lu antal fundet", (unsigned long)[objects count]];
}
}
@end
Мои ViewController.h страница (нет ошибок)
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *name;
@property (strong, nonatomic) IBOutlet UITextField *adress;
@property (strong, nonatomic) IBOutlet UITextField *age;
@property (strong, nonatomic) IBOutlet UILabel *status;
- (IBAction)saveData:(id)sender;
- (IBAction)findKunde:(id)sender;
@end
Мой AppDelegate.m файл
#import "AppDelegate.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}
....
@end
UPDATE
При добавлении текста в поля и нажмите save, я не получаю сообщение «Save ok», он переходит на xcode и показывает мне thi s.
Вы уверены, что '' импортирован в ваш предварительно скомпилированный файл заголовка? - И, пожалуйста, скопируйте/запустите метод 'saveData:' как * text * в вопрос, а не как (обрезанный) снимок экрана. –
Привет, Мартин. hmm Я добавил saveData: в разделе «Мой код ViewController.m». ! –
Извините, я не обратил на это внимания. Что относительно ''? –