I am working on an example from a book that I got and it doesnt seem to be working I am getting the warning Incomplete implementation. When I run the program I get an error singal “EXC_BAD_ACCESS”. The warning is in the .m file at the line return [NSString stringWithFormat:@"Name:... Does anyone know what I am doing wrong?
my .m file
#import "RadioStation.h"
@implementation RadioStation
+ (double)minAMFrequency {
return 520.0;
}
+ (double)maxAMFrequency {
return 1610.0;
}
+ (double)minFMFrequency {
return 88.3;
}
+ (double)maxFMFrequency {
return 107.9;
}
- (id)initWithName:(NSString *)newName atFrequency:(double)newFreq atBand:(char)newBand {
self = [super init];
if (self != nil) {
name = [newName retain];
frequency = newFreq;
band = newBand;
}
return self;
}
- (NSString *)description {
return [NSString stringWithFormat:@"Name: %@, Frequency: %.1f Band: %@", name, frequency, band];
}
- (void)dealloc {
[name release];
[super dealloc];
}
@end
My .h file
#import <Cocoa/Cocoa.h>
@interface RadioStation : NSObject {
NSString *name;
double frequency;
char band;
}
+ (double)minAMFrequency;
+ (double)maxAMFrequency;
+ (double)minFMFrequency;
+ (double)maxFMFrequency;
-(id)initWithName:(NSString*)name
atFrequency:(double)freq
atBand:(char)ban;
-(NSString *)name;
-(void)setName:(NSString *)newName;
-(double)frequency;
-(void)setFrequency:(double)newFrequency;
-(char)band;
-(void)setBand:(char)newBand;
@end
radiosimulation.m file:
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
// insert code here...
NSMutableDictionary* stations = [[NSMutableDictionary alloc] init];
RadioStation* newStation;
newStation = [[RadioStation alloc] initWithName:@"Star 94"
atFrequency:94.1
atBand:'F'];
[stations setObject:newStation forKey:@"WSTR"];
[newStation release];
NSLog(@"%@", [stations objectForKey:@"WSTR"]);
newStation = [[RadioStation alloc] initWithName:@"Rocky 99"
atFrequency:94.1
atBand:'F'];
[stations setObject:newStation forKey:@"WKFR"];
[newStation release];
NSLog(@"%@", [stations objectForKey:@"WKFR"]);
[stations release];
[pool drain];
return 0;
You are declaring the following property accessor/mutators (getter/setters) but are not implementing them in your .m file.
You need to implement all 6 of these methods in the .m file if you want to remove the warning about incomplete implementation.
You are effectively saying in the .h file that this is what your object is going to do, then not doing it in the .m. It won’t generate an error, as objective-c messaging means that the message will be handed up to NSObject to deal with, which will also not have any matching implementation, and the messages will just be silently ignored. I don’t like the way that this is only shown as a warning – but there you go.
That said, I wouldn’t create the properties like this (there are neater ways of doing this in objective-c using @property), I would remove those method declarations in the .h and replace them with:
These property declarations go in the same place as method declarations.
and then add the following to the .m file:
This will avoid having to write all that boilerplate accessor/mutator code that you are currently missing. Again, these go in the same region of the code as method implementations. Effectively the compiler is going to create name and setName methods automatically.
This code is untested – but should point you in the right direction for tidying up the incomplete implementation. It may fix your access error too – but that may require more detailed look at a stack trace.
Another point I’m not sure the code as written even needs to use get/set methods or properties. You might try removing the method declarations from the .h and see if it works. It seems that all the accesses to name, frequency and band are all from within the object.