Search code examples
objective-cnsarraynslog

Method does not get run


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

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.