核心数据,NSManagedObject,
我从MacResearch.org获得名为Molecular Core的演示应用程序的源代码
有一个名为Molecule的类。它是NSManagedObject的子类。 当我尝试在Molecule实例上调用实例方法时 我在下面的NSLog消息后面得到一个'无法识别的选择器'错误消息:
2011-04-04 20:41:44.778 Molecular Core [13766:903] symbol = Ag molecule =(entity:Molecule; id:0x1230b70; data:{ atoms =( ); name =“New Molecule”; }) COORDS [1] = 2.500000
2011-04-04 20:41:45.917 Molecular Core [13766:903] - [NSManagedObject addAtomWithPosition:andSymbol:]: 无法识别的选择器发送到实例0x1234d00
这是我的问题:
Molecule.h声明方法addAtomWithPosition:andSymbol:与Molecule.m中的定义完全相同。 为什么系统无法找到此方法。 Molecule的例子似乎只被视为 它的超类NSManagedObject的一个实例。那是为什么?
感谢阅读! 标记
//
// 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
答案 0 :(得分:1)
NSManagedObject
不会响应选择器,但Molecule
的子类NSManagedObject
会响应。您需要将实体Molecule
的类设置为Core Data模型中的类Molecule
。