除了在 *.h 文件中的接口中声明之外,如何使从 NSManagedObject 子类化的对象的选择器对编译器可见?

发布于 2024-10-29 20:39:12 字数 4169 浏览 2 评论 0原文

Core Data,NSManagedObject,

我从 MacResearch.org 获得了一个名为 Molecular Core 的演示应用程序的源代码,

其中有一个名为 Molecule 的类。它是 NSManagedObject 的子类。 当我尝试在 Molecule 实例上调用实例方法时 我在下面的 NSLog 消息后面收到“无法识别的选择器”错误消息:

2011-04-04 20:41:44.778 Molecular Core[13766:903] symbol=Ag 分子=(实体:分子;id:0x1230b70;数据:{ 原子 = ( ); name = "新分子"; }) 坐标[1]=2.500000


2011-04-04 20:41:45.917 分子核心[13766:903] -[NSManagedObject addAtomWithPosition:andSymbol:]: 无法识别的选择器发送到实例 0x1234d00


这是我的问题:

Molecule.h 声明方法 - addAtomWithPosition:andSymbol: 与 Molecule.m 中的定义相同。 为什么系统找不到这个方法。分子的实例似乎仅被视为 其超类 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

Core Data, NSManagedObject,

I obtained source code for a demo app from MacResearch.org named Molecular Core

There is a class named Molecule. It is a subclass of NSManagedObject.
When I try to invoke an instance method on an instance of Molecule
I get an 'unrecognized selector' error message following an NSLog message just below:

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:]:
unrecognized selector sent to instance 0x1234d00


THIS IS MY QUESTION:

Molecule.h declares the method- addAtomWithPosition:andSymbol: identically to the definition in Molecule.m.
Why does the system fail to find this method. The instance of Molecule seems to be regarded solely as
an instance of its superclass NSManagedObject . Why is that?

Thanks for reading!
Mark

//
//  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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耳根太软 2024-11-05 20:39:12

NSManagedObject 不会响应选择器,但 NSManagedObject 的子类 Molecule 会响应。您需要将实体 Molecule 的类设置为 Core Data 模型中的类 Molecule

NSManagedObject doesn't respond to the selector, but your subclass Molecule of NSManagedObject does. You need to set the class for the entity Molecule to the class Molecule in your Core Data model.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文