开发者

'TileMap' may not respond to '+mapNamed:'

开发者 https://www.devze.com 2022-12-13 09:07 出处:网络
Here\'s an odd one. I have a class named TileMap with the following interface: @interface TileMap : NSObject

Here's an odd one. I have a class named TileMap with the following interface:

@interface TileMap : NSObject
{
    int *data;
    int tilesWide;
    int tilesHigh;
    NSString *imageName;
}

+ (id)mapNamed:(NSString *)filename;
- (id)initWithFile:(NSString *)filename;

@end

The implementation looks like this:

@implementation TileMap

+ (id)mapNamed:(NSString *)filename
{
    return [[self alloc] initWithFile:filename];
}

- (id)initWithFile:(NSString *)filename
{
    if (self = [super init])
    {
        // ...
    }
    return self;
}


@end

But when I add a call to [TileMap mapNamed:@"map.plist"]; to my application Xcode warns:

'TileMap' may not respond to '+mapNamed:'

The application compiles fine and calls to NSLog within TileMap-initWithFile: are logged. I noticed that Xcode's syntax coloring was off for this class and method so I tried renaming both the class and the method separately. The only combination that eliminated the warning and syntax coloring issues was to rename both the class and the method.

Am I colliding with s开发者_Go百科ome undocumented framework class or method? Find in Documentation doesn't reveal anything. Find in Project only reveals the call, interface definition and the implementation. I'm stumped (not that it takes much). Is there a way around this without munging my existing naming conventions?


Did you #import the TileMap.h header? Did you save your TileMap.h header?


Turns out my project directory ended up with two TileMap.h and TileMap.m files—visible from the Finder but not in Xcode. One, a complete interface and implementation, in my root project directory. The other just a bare NSObject subclass in my framework subdirectory. Not sure how that happened. Deleting the latter resolved the problem. Thanks for the help just the same Dave.


Shaun,

besides the problem you asked about, you also have a memory leak in +mapNamed:. The following line returns a non-autoreleased object with a retain count of +1, which basically gives ownership to the caller:

return [[self alloc] initWithFile:filename];

According to the Memory Management Programming Guide for Cocoa, you should return autoreleased objects from convenience methods, such as this:

return [[[self alloc] initWithFile:filename] autorelease];

If you have Snow Leopard and Xcode 3.2, you might want to try running the static analyzer to find mistakes such as this one by pressing Cmd+Shift+A.

0

精彩评论

暂无评论...
验证码 换一张
取 消