Cocoa 除了在*.h文件的接口中声明之外,如何使从NSManagedObject子类化的对象的选择器对编译器可见?
核心数据,NSManagedObject 我从MacResearch.org获得了一个名为Molecular Core的演示应用程序的源代码 有一个叫做分子的类。它是NSManagedObject的一个子类。 当我试图调用一个分子实例上的实例方法时 我在下面的NSLog消息之后收到一条“无法识别的选择器”错误消息: 2011-04-04 20:41:44.778分子核[13766:903]symbol=Ag 分子=(实体:分子;id:0x1230b70;数据:{ 原子=( ); name=“新分子”; }) 坐标[1]=2.500000Cocoa 除了在*.h文件的接口中声明之外,如何使从NSManagedObject子类化的对象的选择器对编译器可见?,cocoa,core-data,selector,nsmanagedobject,Cocoa,Core Data,Selector,Nsmanagedobject,核心数据,NSManagedObject 我从MacResearch.org获得了一个名为Molecular Core的演示应用程序的源代码 有一个叫做分子的类。它是NSManagedObject的一个子类。 当我试图调用一个分子实例上的实例方法时 我在下面的NSLog消息之后收到一条“无法识别的选择器”错误消息: 2011-04-04 20:41:44.778分子核[13766:903]symbol=Ag 分子=(实体:分子;id:0x1230b70;数据:{ 原子=( ); name=“新分
2011-04-04 20:41:45.917分子核[13766:903]-[NSManagedObject AddAtomWith Position:andSymbol:]: 已将无法识别的选择器发送到实例0x1234d00
这是我的问题: h声明方法-addAtomWithPosition:和symbol:与molector.m中的定义相同。 为什么系统找不到这种方法。分子的例子似乎只被视为 其超类NSManagedObject的实例。为什么呢 谢谢你的阅读! 标记
//
//分子
//分子核
#进口
@类原子;
@界面分子:NSManagedObject{
@私人的
}
@属性(读写、复制)NSString*名称;
///此选择器/方法对编译器“不可见”//////
-(id)添加位置为:(双*)的位置和符号为:(NSString*)的符号;
///此类方法对编译器的用户可见//////
+(id)molecleFromXYZ字符串:(NSString*)带名称的XYZ字符串:(NSString*)名称
inManagedObjectContext:(NSManagedObjectContext*)上下文;
@结束
///末端分子/////////////////
//开始/////////////
//
//分子
#导入“molector.h”
#导入“Atom.h”
#导入“Element.h”
@实现分子
@动态名称;
-(id)init
{
self=[super init];
如果(自我){
//这里是初始化代码。
}
回归自我;
}
////该类方法运行////////////
+(id)MolecleFromxyzString:(NSString*)xyzString
托管对象上下文中的withName:(NSString*)名称:(NSManagedObjectContext*)上下文
{
分子*分子;
分子=(分子*)[N实体描述insertNewObjectForEntityForName:@“分子”
inManagedObjectContext:context];
molecular.name=名称;
NSScanner*scanner=[NSScanner scannerWithString:(nil==xyzString?@”“:xyzString)];
[scanner setCharacterStobeskiped:[NSCharacterSet whitespaceCharacterSet]];
BOOL xyzLine=否;
BOOL firstLineFound=否;
NSCharacterSet*换行符集;
newlineSet=[NSCharacterSet characterSetWithCharactersInString:@“\n”];
而(![scanner isattend]&&((xyzLine&&firstLineFound)| |!firstLineFound))
{
NSString*line=nil;
NSScanner*线扫描仪;
双坐标[3];
NSString*符号;
if(![scanner scanupCharacters fromset:newlineSet intoString:&line])
行=@“;
[扫描仪扫描字符串:@“\n”intoString:NULL];
lineScanner=[NSScanner scannerWithString:line];//为扫描行内容创建扫描仪
[lineScanner SetCharacterStobeskiped:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
xyzLine=[lineScanner scanCharactersFromSet:[NSCharacterSet-letterCharacterSet]intoString:&symbol]&&
[lineScanner scanDouble:&(coords[0])]&&
[lineScanner scanDouble:&(coords[1])]&&
[lineScanner scanDouble:&(coords[2]);
NSLog(@“symbol=%@\n分子=%@\n单词[1]=%f],符号,分子,(坐标[1]);
if(xyzLine){
firstLineFound=是;
////这是失败的电话/////////////////////
[((Molecule*)Molecule)addatomwith position:(双*)坐标和符号:(NSString*)符号];
}
}
返回分子;
}
-(id)添加位置为:(双*)的位置和符号为:(NSString*)的符号{
Atom*Atom=[NSEntityDescription insertNewObjectForEntityForName:@“Atom”
inManagedObjectContext:[self-managedObjectContext]];
Element*Element=[Element elementWithSymbol:symbol-inManagedObjectContext:[self-managedObjectContext]];
原子分子=自身;
原子元素=元素;
atom.positionX=[NSNumber numberWithDouble:position[0]];
atom.positionY=[NSNumber numberWithDouble:position[1]];
atom.positionZ=[NSNumber numberWithDouble:位置[2]];
NSLog(@“positionY=%@),atom.positionY);
返回原子;
}
-(无效)解除锁定
{
[super dealoc];
}
@结束
NSManagedObject
不响应选择器,但是NSManagedObject
的子类molecular
会响应选择器。您需要将实体molecular
的类设置为核心数据模型中的类molecular
。完全正确!非常感谢。
//
// Molecule.h
// Molecular Core
#import <Foundation/Foundation.h>
@class Atom;
@interface Molecule : NSManagedObject {
@private
}
@property (readwrite, copy) NSString *name;
/// THIS SELECTOR/METHOD IS 'INVISIBLE' TO COMPILER //////
-(id)addAtomWithPosition:(double *)position andSymbol:(NSString *)symbol;
/// THIS CLASS METHOD IS VISIBLE TO COMPILER' //////
+(id)moleculeFromXYZString:(NSString *)xyzString withName:(NSString *)name
inManagedObjectContext:(NSManagedObjectContext *)context;
@end
/// end molecule.h /////////////////
// begin molecule.m /////////////
//
// Molecule.m
#import "Molecule.h"
#import "Atom.h"
#import "Element.h"
@implementation Molecule
@dynamic name;
-(id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
//// THIS CLASS METHOD RUNS ////////////
+(id)moleculeFromXYZString:(NSString *)xyzString
withName:(NSString *)name inManagedObjectContext:(NSManagedObjectContext *)context
{
Molecule *molecule;
molecule = (Molecule*) [NSEntityDescription insertNewObjectForEntityForName:@"Molecule"
inManagedObjectContext:context];
molecule.name = name;
NSScanner *scanner = [NSScanner scannerWithString:(nil == xyzString ? @"" : xyzString)];
[scanner setCharactersToBeSkipped:[NSCharacterSet whitespaceCharacterSet]];
BOOL xyzLine = NO;
BOOL firstLineFound = NO;
NSCharacterSet *newlineSet;
newlineSet = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
while ( ![scanner isAtEnd] && ( ( xyzLine && firstLineFound ) || !firstLineFound ) )
{
NSString *line = nil;
NSScanner *lineScanner;
double coords[3];
NSString *symbol;
if ( ![scanner scanUpToCharactersFromSet:newlineSet intoString:&line] )
line = @"";
[scanner scanString:@"\n" intoString:NULL];
lineScanner = [NSScanner scannerWithString:line]; // Create scanner for scanning line content
[lineScanner setCharactersToBeSkipped:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
xyzLine = [lineScanner scanCharactersFromSet:[NSCharacterSet letterCharacterSet] intoString:&symbol] &&
[lineScanner scanDouble:&(coords[0])] &&
[lineScanner scanDouble:&(coords[1])] &&
[lineScanner scanDouble:&(coords[2])];
NSLog(@"symbol=%@\nmolecule=%@\ncoords[1]=%f",symbol,molecule,(coords[1]));
if ( xyzLine ) {
firstLineFound = YES;
//// THIS IS THE CALL THAT FAILS /////////////////////
[((Molecule*)molecule) addAtomWithPosition:(double *)coords andSymbol:(NSString*)symbol];
}
}
return molecule;
}
-(id)addAtomWithPosition:(double *)position andSymbol:(NSString *)symbol {
Atom *atom = [NSEntityDescription insertNewObjectForEntityForName:@"Atom"
inManagedObjectContext:[self managedObjectContext]];
Element *element = [Element elementWithSymbol:symbol inManagedObjectContext:[self managedObjectContext]];
atom.molecule = self;
atom.element = element;
atom.positionX = [NSNumber numberWithDouble:position[0]];
atom.positionY = [NSNumber numberWithDouble:position[1]];
atom.positionZ = [NSNumber numberWithDouble:position[2]];
NSLog(@"positionY=%@",atom.positionY);
return atom;
}
-(void)dealloc
{
[super dealloc];
}
@end