Anthony Kong
Anthony Kong

Reputation: 40624

Objective-C: Why this function name caused a warning?

This is the original header file:

@interface TestDataHelper : NSObject {



}
+(void) populateTestData:(NSManagedObjectContext*) managedObjectContext;

+(void) testPopulateTestData:(NSManagedObjectContext*) managedObjectContext;

@end

When I compile this file, I got this warning:

method definiton not found

for testPopulateTestData

When I ignore the warning and run the app in iphone simulator, I got a runtime exception:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[TestDataHelper testPopulateTestData:]: unrecognized selector sent to class 0x104d8'

Rename the method to 'test' alone seems to solve the problem

What is special about testXXX method name?

EDIT: implementation is there and done. Renaming the method name (in both .h and .m) removes the warning, and the final app works.

EDIT 2:

Here is the implementation of the function test (originally named as testPopulatedTestData):

+(void) test:(NSManagedObjectContext*) managedObjectContext {

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Stock" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [request setSortDescriptors:sortDescriptors];
    [sortDescriptors release];
    [sortDescriptor release];

    NSError *error = nil;
    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];

    NSEnumerator *e = [mutableFetchResults objectEnumerator];
    id object;
    while (object = [e nextObject]) {
        // do something with object
        Stock* fc = (Stock*) object; 
        NSLog(@"get a fc %s", [[fc name] description]);
    }

}

Upvotes: 0

Views: 217

Answers (1)

Lily Ballard
Lily Ballard

Reputation: 185671

The error message is telling you that you've declared a method in your header file that you never implemented anywhere. The runtime error is telling you the same thing - you've sent the selector testPopulateTestData to the class TestDataHelper but it was never implemented.

Upvotes: 1

Related Questions