【发布时间】:2018-10-08 08:37:14
【问题描述】:
我正在尝试实现一个数据库系统,其中一个实体在其 DataClass.m 中包含一些方法,但我无法在我的 ViewController.m 中调用它们
在上面的代码中,我尝试调用 savePersona 但构建显示
选择器没有已知的类方法 'savePersona:etaLabel:indirizzoLabel:contextLabel:'
ViewController.m
#import "ViewController.h"
#import "Giovanni+CoreDataClass.h"
#import "AppDelegate.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
/*
* Event after clicking button Save.
*/
- (IBAction)btnSave:(id)sender {
NSString *nome = _insertName.text;
NSString *eta = _insertAge.text;
NSString *indirizzo = _insertIndirizzo.text;
// Check for all inputs
if ([nome isEqualToString:@""] || [eta isEqualToString:@""] || [eta intValue] <= 0 || [indirizzo isEqualToString:@""] ) {
_resultText.text = @"All fields are required";
return;
}
[Giovanni savePersona:nome etaLabel:[eta intValue] indirizzoLabel:indirizzo contextLabel:[((AppDelegate *)[[UIApplication sharedApplication] delegate]) managedObjectContext]];
}
Giovanni+CoreDataClass.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@interface Giovanni : NSManagedObject
+(void)savePersona: (NSString*)nome etaLabel:(int16_t)eta indirizzoLabel:(NSString*)indirizzo contextLabel:(NSManagedObjectContext *)context;
@end
#import "Giovanni+CoreDataProperties.h"
Giovanni+CoreDataClass.m
#import "Giovanni+CoreDataClass.h"
@implementation Giovanni
+(void)savePersona: (NSString*)nome etaLabel:(int16_t)eta indirizzoLabel:(NSString*)indirizzo contextLabel:(NSManagedObjectContext *)context {
Giovanni *p1 = [[Giovanni alloc] initWithContext:context];
p1.nome = nome;
p1.eta = eta;
p1.indirizzo = indirizzo;
}
在构建阶段,我有 Giovanni+CoreDataClass.m 和 Giovanni+CoreDataProperties.m。 在 Copy Bundle Recources 我有 DatabaseExample.xcdatamodeld (DatabaseExample 是项目的名称)
【问题讨论】:
-
阅读苹果文档以实现objective-c中的类别developer.apple.com/library/archive/documentation/Cocoa/… ..在Swift中,通过使用
extension ExistingClassName扩展类非常容易。 -
你试过用 @interface Giovanni(CoreDataClass) 代替 interface Giovanni : NSManagedObject 吗?
标签: ios objective-c xcode10