Нераспознанный селектор отправлен в класс - PullRequest
1 голос
/ 28 февраля 2011

Используя пример, который я нашел в другом месте, который объединил два массива в общий вид, используя 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

Ответы [ 3 ]

1 голос
/ 28 февраля 2011

Хотя в вашем коде, похоже, нет ничего, что могло бы вызвать этот сбой, ваш код содержит серьезную утечку памяти:

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

Хотя я не вижу объявления свойства,похоже, что он объявлен как (копия) или (сохранить).Выделение массива и присвоение свойству без повторного освобождения исходного массива приводит к утечке памяти.

1 голос
/ 28 февраля 2011

Это потому, что

- (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
0 голосов
/ 28 февраля 2011

Код был в порядке.Это была старая привязка к столбцу таблицы, которая не вызывала проблем до того, как это вступило в конфликт с попыткой кодов выступить в качестве источника данных для той же таблицы.

...