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

时间:2011-02-27 21:33:55

标签: objective-c arrays xcode macos nsoutlineview

使用我在其他地方找到的一个例子,它将两个数组合成一个使用6个文件(一个代表h / m,一个父h / m和一个子h / m - 3个类)的outlineview,我推荐了一下这个项目并将它们组合成一个标题和一个主体(以避免额外的文件,因为项目预计会变得更大。我不知道这是否允许?)。该项目正在成功构建,但是当它崩溃时我得到以下错误,并且无法弄清楚要在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

3 个答案:

答案 0 :(得分:1)

那是因为

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

答案 1 :(得分:1)

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

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

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

答案 2 :(得分:0)

代码还可以。这是一个旧表绑定到表列之前没有出现问题,之前与代码尝试充当同一个表的数据源的代码冲突。