无法识别的选择器发送到类

发布于 2024-10-19 05:58:24 字数 7380 浏览 5 评论 0原文

使用我在其他地方找到的一个示例,该示例使用 6 个文件(一个委托 h/m、一个父 h/m 和一个子 h/m - 3 个类)将两个数组组合成一个大纲视图,我对项目进行了一些修改并将它们组合起来分成一个标题和一个正文(以避免额外的文件,因为项目预计会变得更大。我不知道这是否允许?)。该项目正在成功构建,但当它崩溃时我收到以下错误,并且无法弄清楚要在 initWithTitle 中解决什么问题来纠正它。我想这与我合并文件的方式有关。它们是一种文字复制并粘贴到一个文件中,没有遗漏任何内容,并且具有不同的实现和接口。您可以从评论中看到每个内容的删减。

崩溃错误

+[IFParentNode initWithTitle:children:]: unrecognized selector sent to class 0x1000054d8

clientProjViewController.h

#import <Cocoa/Cocoa.h>

// IFChildNode
@interface IFChildNode : NSObject {
    NSString *title;
}

@property (nonatomic, retain) NSString *title;

- (id)initWithTitle:(NSString *)theTitle;

@end

// IFParentNode
@interface IFParentNode : NSObject {
    NSString *title;
    NSMutableArray *children;
}

@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSMutableArray *children;

- (id)initWithTitle:(NSString *)theTitle children:(NSMutableArray *)theChildren;
- (void)addChild:(id)theChild;
- (void)insertChild:(id)theChild atIndex:(NSUInteger)theIndex;
- (void)removeChild:(id)theChild;
- (NSInteger)numberOfChildren;
- (id)childAtIndex:(NSUInteger)theIndex;
@end

//clientProjView
@interface clientProjViewController : NSObject {
    IBOutlet NSOutlineView *outlineView;
    IBOutlet NSArrayController *projectsController;
    IBOutlet NSArrayController *clientsController;
    IFParentNode *rootNode;
}


@end

clientProjViewController.m

#import "clientProjViewController.h"


@implementation clientProjViewController

- (void)awakeFromNib {
    rootNode = [[IFParentNode alloc] initWithTitle:@"Root" children:nil];
    NSInteger counter;
    for(counter = 0; counter < 5; counter++) {
        IFParentNode *tempNode = [[IFParentNode alloc] initWithTitle:[NSString stringWithFormat:@"Parent %i", counter] children:nil];
        NSInteger subCounter;
        for(subCounter = 0; subCounter < 5; subCounter++) {
            IFChildNode *subTempNode = [[IFChildNode alloc] initWithTitle:[NSString stringWithFormat:@"Child %i", subCounter]];
            [tempNode addChild:subTempNode];
            [subTempNode release];
        }

        [rootNode addChild:tempNode];
        [tempNode release];
    }
}

- (void)dealloc {
    [rootNode release];
    [super dealloc];
}

/* - - - - - - - - - - - - - - - - - - - -
 Required OutlineviewDataSource methods
- - - - - - - - - - - - - - - - - - - - */

- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
    if([item isKindOfClass:[IFChildNode class]]) {
        return nil;
    }

    return (item == nil ? [rootNode childAtIndex:index] : [(IFParentNode *)item childAtIndex:index]);
}

- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
    return (item == nil || [item isKindOfClass:[IFParentNode class]]);
}

- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
    if([item isKindOfClass:[IFChildNode class]]) {
        return 0;
    }

    return (item == nil ? [rootNode numberOfChildren] : [(IFParentNode *)item numberOfChildren]);
}

- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
    if([item isKindOfClass:[IFChildNode class]]) {
        return ((IFChildNode *)item).title;
    }

    if([item isKindOfClass:[IFParentNode class]]) {
        return ((IFParentNode *)item).title;
    }

    return nil;
}

// Extra methods for datasource

- (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item {
    return (item == nil || [item isKindOfClass:[IFParentNode class]]);
}

- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item {
    return ([item isKindOfClass:[IFChildNode class]]);
}

@end

/* - - - - - - - - - - - - - - - - - - - -
IfChild
 - - - - - - - - - - - - - - - - - - - - */

@implementation IFChildNode
@synthesize title;
- (id)initWithTitle:(NSString *)theTitle {
    if(self = [super init]) {
        self.title = theTitle;
    }

    return self;
}

- (void)dealloc {
    self.title = nil;
    [super dealloc];
}
@end

/* - - - - - - - - - - - - - - - - - - - -
IfParent
 - - - - - - - - - - - - - - - - - - - - */

@implementation IFParentNode
@synthesize title, children;
- (id)initWithTitle:(NSString *)theTitle children:(NSMutableArray *)theChildren {
    if(self = [super init]) {
        self.title = theTitle;
        self.children = (theChildren == nil ? [NSMutableArray new] : theChildren);
    }

    return self;
}

- (void)addChild:(id)theChild {
    [self.children addObject:theChild];
}

- (void)insertChild:(id)theChild atIndex:(NSUInteger)theIndex {
    [self.children insertObject:theChild atIndex:theIndex];
}

- (void)removeChild:(id)theChild {
    [self.children removeObject:theChild];
}

- (NSInteger)numberOfChildren {
    return [self.children count];
}

- (id)childAtIndex:(NSUInteger)theIndex {
    return [self.children objectAtIndex:theIndex];
}

- (void)dealloc {
    self.title = nil;
    self.children = nil;
    [super dealloc];
}

@end

堆栈跟踪

2011-02-27 19:05:44.165 ProjectName[2070:a0f] +[IFParentNode initWithTitle:children:]: unrecognized selector sent to class 0x1000054d8
2011-02-27 19:05:44.169 ProjectName[2070:a0f] An uncaught exception was raised
2011-02-27 19:05:44.169 ProjectName[2070:a0f] +[IFParentNode initWithTitle:children:]: unrecognized selector sent to class 0x1000054d8
2011-02-27 19:05:44.210 ProjectName[2070:a0f] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[IFParentNode initWithTitle:children:]: unrecognized selector sent to class 0x1000054d8'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00007fff865047b4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x00007fff83fca0f3 objc_exception_throw + 45
    2   CoreFoundation                      0x00007fff8655e1a0 __CFFullMethodName + 0
    3   CoreFoundation                      0x00007fff864d691f ___forwarding___ + 751
    4   CoreFoundation                      0x00007fff864d2a68 _CF_forwarding_prep_0 + 232
    5   ProjectName                              0x0000000100002a3f -[clientProjViewController awakeFromNib] + 81
    6   CoreFoundation                      0x00007fff864b2a2d -[NSSet makeObjectsPerformSelector:] + 205
    7   AppKit                              0x00007fff82528657 -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1445
    8   AppKit                              0x00007fff8252688d loadNib + 226
    9   AppKit                              0x00007fff82525d9a +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:] + 248
    10  AppKit                              0x00007fff82525bd2 +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 326
    11  AppKit                              0x00007fff82523153 NSApplicationMain + 279
    12  ProjectName                              0x0000000100001441 main + 33
    13  ProjectName                              0x0000000100001418 start + 52
    14  ???                                 0x0000000000000003 0x0 + 3
)
terminate called after throwing an instance of 'NSException'
Program received signal:  “SIGABRT”.
sharedlibrary apply-load-rules all

Using an example I found elsewhere that combined two arrays into an outlineview using 6 files (a delegate h/m, a parent h/m and a child h/m - 3 classes), I've ammended the project a little and combined them into one header and one body (to avoid extra files as the project is expected to grow much larger. I don't know if this is allowed?). The project is successfully building but I'm getting the following error when it crashes and can't figure out what to address in the initWithTitle to correct it. I imagine it has to do with how I've combined the files. They're a literal copy and paste into one file with nothing left out, as well as distinct implementations and interfaces left distinct. You can see the cut off of each from the comments.

Crash error :

+[IFParentNode initWithTitle:children:]: unrecognized selector sent to class 0x1000054d8

clientProjViewController.h :

#import <Cocoa/Cocoa.h>

// IFChildNode
@interface IFChildNode : NSObject {
    NSString *title;
}

@property (nonatomic, retain) NSString *title;

- (id)initWithTitle:(NSString *)theTitle;

@end

// IFParentNode
@interface IFParentNode : NSObject {
    NSString *title;
    NSMutableArray *children;
}

@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSMutableArray *children;

- (id)initWithTitle:(NSString *)theTitle children:(NSMutableArray *)theChildren;
- (void)addChild:(id)theChild;
- (void)insertChild:(id)theChild atIndex:(NSUInteger)theIndex;
- (void)removeChild:(id)theChild;
- (NSInteger)numberOfChildren;
- (id)childAtIndex:(NSUInteger)theIndex;
@end

//clientProjView
@interface clientProjViewController : NSObject {
    IBOutlet NSOutlineView *outlineView;
    IBOutlet NSArrayController *projectsController;
    IBOutlet NSArrayController *clientsController;
    IFParentNode *rootNode;
}


@end

clientProjViewController.m :

#import "clientProjViewController.h"


@implementation clientProjViewController

- (void)awakeFromNib {
    rootNode = [[IFParentNode alloc] initWithTitle:@"Root" children:nil];
    NSInteger counter;
    for(counter = 0; counter < 5; counter++) {
        IFParentNode *tempNode = [[IFParentNode alloc] initWithTitle:[NSString stringWithFormat:@"Parent %i", counter] children:nil];
        NSInteger subCounter;
        for(subCounter = 0; subCounter < 5; subCounter++) {
            IFChildNode *subTempNode = [[IFChildNode alloc] initWithTitle:[NSString stringWithFormat:@"Child %i", subCounter]];
            [tempNode addChild:subTempNode];
            [subTempNode release];
        }

        [rootNode addChild:tempNode];
        [tempNode release];
    }
}

- (void)dealloc {
    [rootNode release];
    [super dealloc];
}

/* - - - - - - - - - - - - - - - - - - - -
 Required OutlineviewDataSource methods
- - - - - - - - - - - - - - - - - - - - */

- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
    if([item isKindOfClass:[IFChildNode class]]) {
        return nil;
    }

    return (item == nil ? [rootNode childAtIndex:index] : [(IFParentNode *)item childAtIndex:index]);
}

- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item {
    return (item == nil || [item isKindOfClass:[IFParentNode class]]);
}

- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item {
    if([item isKindOfClass:[IFChildNode class]]) {
        return 0;
    }

    return (item == nil ? [rootNode numberOfChildren] : [(IFParentNode *)item numberOfChildren]);
}

- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {
    if([item isKindOfClass:[IFChildNode class]]) {
        return ((IFChildNode *)item).title;
    }

    if([item isKindOfClass:[IFParentNode class]]) {
        return ((IFParentNode *)item).title;
    }

    return nil;
}

// Extra methods for datasource

- (BOOL)outlineView:(NSOutlineView *)outlineView isGroupItem:(id)item {
    return (item == nil || [item isKindOfClass:[IFParentNode class]]);
}

- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item {
    return ([item isKindOfClass:[IFChildNode class]]);
}

@end

/* - - - - - - - - - - - - - - - - - - - -
IfChild
 - - - - - - - - - - - - - - - - - - - - */

@implementation IFChildNode
@synthesize title;
- (id)initWithTitle:(NSString *)theTitle {
    if(self = [super init]) {
        self.title = theTitle;
    }

    return self;
}

- (void)dealloc {
    self.title = nil;
    [super dealloc];
}
@end

/* - - - - - - - - - - - - - - - - - - - -
IfParent
 - - - - - - - - - - - - - - - - - - - - */

@implementation IFParentNode
@synthesize title, children;
- (id)initWithTitle:(NSString *)theTitle children:(NSMutableArray *)theChildren {
    if(self = [super init]) {
        self.title = theTitle;
        self.children = (theChildren == nil ? [NSMutableArray new] : theChildren);
    }

    return self;
}

- (void)addChild:(id)theChild {
    [self.children addObject:theChild];
}

- (void)insertChild:(id)theChild atIndex:(NSUInteger)theIndex {
    [self.children insertObject:theChild atIndex:theIndex];
}

- (void)removeChild:(id)theChild {
    [self.children removeObject:theChild];
}

- (NSInteger)numberOfChildren {
    return [self.children count];
}

- (id)childAtIndex:(NSUInteger)theIndex {
    return [self.children objectAtIndex:theIndex];
}

- (void)dealloc {
    self.title = nil;
    self.children = nil;
    [super dealloc];
}

@end

Stack Trace :

2011-02-27 19:05:44.165 ProjectName[2070:a0f] +[IFParentNode initWithTitle:children:]: unrecognized selector sent to class 0x1000054d8
2011-02-27 19:05:44.169 ProjectName[2070:a0f] An uncaught exception was raised
2011-02-27 19:05:44.169 ProjectName[2070:a0f] +[IFParentNode initWithTitle:children:]: unrecognized selector sent to class 0x1000054d8
2011-02-27 19:05:44.210 ProjectName[2070:a0f] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[IFParentNode initWithTitle:children:]: unrecognized selector sent to class 0x1000054d8'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00007fff865047b4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x00007fff83fca0f3 objc_exception_throw + 45
    2   CoreFoundation                      0x00007fff8655e1a0 __CFFullMethodName + 0
    3   CoreFoundation                      0x00007fff864d691f ___forwarding___ + 751
    4   CoreFoundation                      0x00007fff864d2a68 _CF_forwarding_prep_0 + 232
    5   ProjectName                              0x0000000100002a3f -[clientProjViewController awakeFromNib] + 81
    6   CoreFoundation                      0x00007fff864b2a2d -[NSSet makeObjectsPerformSelector:] + 205
    7   AppKit                              0x00007fff82528657 -[NSIBObjectData nibInstantiateWithOwner:topLevelObjects:] + 1445
    8   AppKit                              0x00007fff8252688d loadNib + 226
    9   AppKit                              0x00007fff82525d9a +[NSBundle(NSNibLoading) _loadNibFile:nameTable:withZone:ownerBundle:] + 248
    10  AppKit                              0x00007fff82525bd2 +[NSBundle(NSNibLoading) loadNibNamed:owner:] + 326
    11  AppKit                              0x00007fff82523153 NSApplicationMain + 279
    12  ProjectName                              0x0000000100001441 main + 33
    13  ProjectName                              0x0000000100001418 start + 52
    14  ???                                 0x0000000000000003 0x0 + 3
)
terminate called after throwing an instance of 'NSException'
Program received signal:  “SIGABRT”.
sharedlibrary apply-load-rules all

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

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

发布评论

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

评论(3

情释 2024-10-26 05:58:24

那是因为

- (id)initWithTitle:(NSString *)theTitle children:(NSMutableArray *)theChildren

当您像这样向类发送消息时,它是一个实例方法,

[IFParentNode initWithTitle: @"Some title" children: children]

您必须像这样进行调用:

[[IFParentNode alloc] initWithTitle: @"Some title" children: children]

如果您想向类发送消息,那么您必须将该方法声明为静态:

+ (id)initWithTitle:(NSString *)theTitle children:(NSMutableArray *)theChildren

Thats because

- (id)initWithTitle:(NSString *)theTitle children:(NSMutableArray *)theChildren

is an instance method while you're sending this message to the class like this

[IFParentNode initWithTitle: @"Some title" children: children]

You have to make call like this:

[[IFParentNode alloc] initWithTitle: @"Some title" children: children]

If you want to send message to class, then you have to declare that method as static:

+ (id)initWithTitle:(NSString *)theTitle children:(NSMutableArray *)theChildren
日暮斜阳 2024-10-26 05:58:24

虽然您的代码中似乎没有任何内容可能导致此崩溃,但您的代码确实包含严重的内存泄漏:

self.children = (theChildren == nil ? [NSMutableArray new] : theChildren);

虽然我看不到属性声明,但它看起来像是被声明为 (copy) 或 (retain) 。分配数组并分配给属性而不再次释放原始数组会导致内存泄漏。

While there doesn't seem to be anything in your code which can cause this crash, your code does contain a bad memory leak:

self.children = (theChildren == nil ? [NSMutableArray new] : theChildren);

Although I can't see the property declaration, it looks like it's declared as (copy) or (retain). Allocating an array and assigning to the property without releasing the original array again causes the memory to leak.

救赎№ 2024-10-26 05:58:24

代码没问题。这是对表列的旧绑定,在与尝试充当同一个表的数据源的代码发生冲突之前没有引起问题。

The code was ok. It was an old binding to a tablecolumn that hadn't caused problems before that was conflicting with the codes attempt to act as a datasource for the same table.

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