Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating method in implementation without defining in header
    text
    copied!<p>How can I create a method in the @implementation of a class without defining it in the @interface?</p> <p>For example, I have a constructor that does some initialisation and then reads data from a file. I want to factor out the file-reading code into a separate method that I then call from within the constructor. I don't want to define this method in the header because it's private only to this @implementation context.</p> <p>Is this possible?</p> <p>Here's my example. I have a little program that read's a Todo task-list from a file.</p> <p>Here is the @interface:</p> <pre><code>@interface TDTaskList : NSObject { NSString* name; // The name of this list. NSMutableArray* tasks; // The set of tasks in this list. } -(id)initListOfName:(NSString*)aName; -(NSArray*)loadListWithName:(NSString*)aName; @end </code></pre> <p>And here is part of the @implementation:</p> <pre><code>-(id)initListOfName:(NSString*)aName { if (self = [super init]) { name = aName; NSArray* aTasks = [self loadListWithName:aName]; tasks = [NSMutableArray arrayWithArray:aTasks]; } return self; } -(NSArray*)loadListWithName:(NSString*)aName { // TODO This is a STUB till i figure out how to read/write from a file ... TDTask* task1 = [[TDTask alloc] initWithLabel:@"Get the Milk."]; TDTask* task2 = [[TDTask alloc] initWithLabel:@"Do some homework."]; return [NSArray arrayWithObjects:task1, task2, nil]; } </code></pre> <p>What I want to do is to <strong>not</strong> have to define the following in the interface:</p> <pre><code>-(NSArray*)loadListWithName:(NSString*)aName; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload