Search code examples
objective-cvariablesimplementationinstances

code problem - beginner


Ok here is my code so far:

@implementation PtyView

- (id)initWithFrame:(NSRect)frame;
{
    if (self = [super initWithFrame: frame])
    {
        [self setFont:[NSFont fontWithName:@"Courier" size:0.0]];
        [self startTask];
    }
    return self;
}

- (void)keyDown:(NSEvent *)event
{
    const char * typein = [[event characters] UTF8String];
    [masterHandle
     writeData:[NSData dataWithBytes:typein length:strlen(typein)]];
}
...
@end

the problem is that I want to trigger "startTask" from another implementation but, if I just "startTask" it won't display the output because I didn't use initWithFrame.

How would I do this?

Thanks, Elijah


Solution

  • If you want to call startTask from somewhere else without first creating an instance of PtyView then startTask must be a static method, not an instance method.

    Put this in your @interface:

    + (void)startTask;
    

    Put this in your @implementation

    + (void)startTask
    {
        // Code goes here
    }
    

    Put this when you want to call it:

    [PtyView startTask];
    

    Notes: The + means it's a static method. You cannot access instance variables from a static method.