我是Objective-C和CoreData的新手,想要学习它并尝试使用Xcode 5,我试着制作这个tutorial。
我已经跟着一些其他的CoreData表名,但是我的“ViewController.m”出现了一些错误,并且不知道要改变什么,我可以看到它建议将“NSEntityDescription”更改为“kSetAttrDescription”但不知道如果那是对还是错,希望有人可以告诉我该做什么 - 所以我下次就知道了。
错误问题
错误说明
我的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
更新
在字段中添加文本并点击保存时,我会收到一条“保存确定”消息,它会跳转到xcode并显示给我。
答案 0 :(得分:0)
您好像忘了导入<CoreData/CoreData.h>
。
您可以将导入添加到预编译头(...- Prefix.pch)文件中
看起来与此类似:
#import <Availability.h>
#ifndef __IPHONE_5_0
#warning "This project uses features only available in iOS SDK 5.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h> // <-- Add this !!
#endif
您可能还需要添加&#34; CoreData.framework&#34;和#34; Link Binary With Libraries&#34; 目标部分&#34;构建阶段&#34;。