开发者

Why am I getting "incompatible pointer type"?

I am trying to create a custom object that simply inherits the NSString class and overrides the 'description' method.

When I compile, however, I am getting a warning:

Incompatible pointer types initializing 'OverrideTester *' with an expression of type 'NSString *'

Here is my code:

main.m

#import <Foundation/NSObject.h>
#import <Foundation/NSString.h>
#import <Foundation/NSAutoreleasePool.h>
#import "OverrideTester.h"

int main (int argc, char *argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSString *str = 开发者_如何学Python@"Programming is fun";
    OverrideTester *strOverride = @"Overriding is fun";

    NSLog (@"%@", str);
    NSLog (@"%@", strOverride);

    [pool drain];
    return 0;
}

OverrideTester.h

#import <Foundation/Foundation.h>

@interface OverrideTester : NSString
-(void) description;
@end

OverrideTester.m

#import "OverrideTester.h"

@implementation OverrideTester

-(void) description

{
    NSLog(@"DESCRIPTION!\n");
}

@end


NSString is part of a class cluster. You cannot just create arbitrary subclasses of it, and when you do, you can't assign constant strings to them (which are type NXConstantString). See Subclassing Notes in the NSString documentation. Generally you don't want to subclass NSString. There are better solutions for most problems.


  1. you are assigning an instance of NSString to your variable of type OverrideTester. If you want an instance of your class, you need to instantiate an instance of that class; type-casting will never change the class of an instance.

  2. description is defined as returning an NSString*:

    - (NSString *)description;

  3. Do not try to learn about subclassing and overriding methods by subclassing NSString (or any other class cluster). If you want to play with subclassing and such -- a very good idea when new to the language, assuredly -- then subclass NSObject, potentially multiple levels , and play there.


How do you mean to subclass NSObject, potentially multiple levels? Isn't it possible NSObject might have conflicting methods compared to other class clusters or just not have them available to override?

If your goal is to figure out how method overrides work (which I thought it was), then you'd be better off doing it entirely yourself.

I may have mis-read your question.

In any case, subclassing NSString is pretty much never done. There are very very few cases where it is useful. Overriding description in anything but custom classes specifically for debugging purposes is useful, yes. Calling description in production code should never be done.

Also, why would description return an NSString* in this code?

What would happen if something that expects an NSString* return value were to call your version that doesn't return anything?

A crash.


You are declaring a variable named strOverride of type pointer to OverrideTester. But to that variable, you are trying to assign a pointer to an NSString. You cannot assign a superclass to a variable of a subclass. Imagine a generic class TwoWheeled and a derived class Motorbike. A Motorbike can be treated like a TwoWheeled, but not the other way round as the Motorbike has features a normal TwoWheeled might not have like a motor.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜