Reputation: 5
Ok, so I’m very new to the programming world. I have learnt the basics of C as a foundation to Objective C. I have been learning Objective C and I’m starting to familiarise myself with the language but one burning issue still remains:
I’m struggling to get my head around why there are interface and implementation files. What’s the point of having a portion of it public? It made more sense to me having everything in one file when I was learning C. I think my problem lies in the fact that I can't visualise how everything would interact with each other for example in an app, so I tried to find some sort of diagram/layman explanation that would help me understand how the process works from a user clicking/pressing a button or inputting data and data returning to the user, but the search was fruitless.
To sum it up: What’s the point of interface and implementation files and why have interface “public” - does that mean to the user or something else?
Very confused. Thanks in advance for any help.
Upvotes: 0
Views: 55
Reputation: 1756
Headers, when done well, make it clear exactly what the class does because it lays out all the class can do. Having public and private methods encourages good design. When you create a class you want it to perform some task or another. It should have a few methods that represent the things it can do in the header file, then internally it should have any methods it needs, but other classes shouldn't call. For instance, you might have a class that processes and parses a couple types of files and returns objects of associated type. You header file might look like:
@class DGCFileTypeA, DGCFileTypeB;
@class DGCObjectTypeA, DGCObjectTypeB;
@interface DGCFileParser : NSObject
- (DGCObjectTypeA *)parseFileA:(DGCFileTypeA *)fileA;
- (DGCObjectTypeB *)parseFileB:(DGCFileTypeB *)fileB;
@end
This makes it so everything else you write that needs to parse files of those types can. You implementation file would look something like:
#import "DGCFileParser.h"
#import "DGCFileTypeA.h"
#import "DGCFileTypeB.h"
#import "DGCObjectTypeA.h"
#import "DGCObjectTypeB.h"
@implementation DGCFileParser
- (DGCObjectTypeA *)parseFileA:(DGCFileTypeA *)fileA {
NSArray *fileLines = [self fileLinesForFile:fileA];
/// Implementation details ///
return result;
}
- (DGCObjectTypeB *)parseFileB:(DGCFileTypeB *)fileB {
NSArray *fileLines = [self fileLinesForFile:fileB];
/// Implementation details (presumably different than above) ///
return result;
}
- (NSArray *)fileLinesForFile:(DGCFileType *)file {
/// Implementation details on reading lines from a file ///
}
@end
So now you have a method that you're using internally but didn't need to expose because no other class should need to use it since this is the class that parses files. In an app something would need to parse a file and it would pass it in to here and get whatever results it needed and that class wouldn't need to worry about how the sausage was made. It doesn't need to care that the lines were read into an array, for example, because all it cares about is the returned object.
Upvotes: 1