1

使用我在其他地方找到的一个示例,该示例使用 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
4

3 回答 3

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
于 2011-02-27T21:42:27.230 回答
1

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

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

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

于 2011-02-27T21:47:09.617 回答
0

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

于 2011-02-28T18:30:44.683 回答