Question

I am trying to run a non-void function returning an NSArray, but when I run it, there's not even a log line:

- (NSArray *) arrayFunction
{
    return myList;
}

This is how I call the function:

- (void) myMainFunction
{
    [self arrayFunction];
}

I also tried with NSLog and a void function instead of NSArray, but that won't show up either.

It is a NSView class.

Thanks for you help!

*EDIT: * Full Code: Implementation file:

#import "LogNavigator.h"

@implementation LogNavigator

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
    // Initialization code here.
    }

    [self myMainFunction];
    return self;
}

- (void)drawRect:(NSRect)dirtyRect
{
    // Drawing code here.
}

- (NSArray *) arrayFunction
{
    // # Get the list of .txt files, this part works correctly as expected in CodeRunner
    NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Desktop"];

    NSArray *directoryList = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil]
                          pathsMatchingExtensions:[NSArray arrayWithObjects:@"txt", nil]];
return directoryList;
}

- (void) myMainFunction
{
    [self arrayFunction];
}

@end
Était-ce utile?

La solution

If this custom NSView is created using Interface Builder then you should override awakeFromNib:

- (void)awakeFromNib {
    [self myMainFunction];
}

Note, however, that you are ignoring the return from arrayFunction, so it's feasible the compiler might omit the call entirely during an optmimized release build if it can determine no side effects of the call.

EDIT: Note that you need to set the NSView-derived class within the view of the window within MainMenu.xib, using IB, in order for this method to be triggered when the view is loaded.

Autres conseils

When objects are loaded from a nib (or a Storyboard) The normal initialiser is not called, instead initWithCoder is called.

When setting up objects from a nib, you should override initWithCoder and put your initialiser code in there instead.

If you need to access or set up other objects in the nib as part of initialisation, then awakeFromNib is a better method to override, because it is called after all the other nib objects have been loaded.

edit

When you added the view object to the nib, did you specify it's class as LogNavigator? This is something that people frequently forget to do.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top