Handling class methods when subclassing in objective-c

When trying my first subclass in Objective-C, I came across the following warning that it can't seem to resolve. Calling decimalNumberWithMantissa gives a warning about "initializing from separate Objective-C type".

#import <Foundation/Foundation.h>

@interface NSDecimalNumberSub : NSDecimalNumber {
}
@end

@implementation NSDecimalNumberSub
@end

int main (int argc, char *argv[]) {
    NSDecimalNumberSub *ten = [NSDecimalNumberSub 
          decimalNumberWithMantissa:10
          exponent:0
          isNegative:NO];
}

      

Do I need to treat a class method differently with a subclass? Did I miss something? Any help would be appreciated.

+1


a source to share


1 answer


NSDecimalNumber defines a method decimalNumberWithMantissa:...

to return NSDecimalNumber, so you will fall back to an instance of the base class and not your custom subclass. You will need to create your own convenience method to return an instance of your subclass, or simply assign and initialize it in a different way.



If you write your own class, you can define a convenience method to return a type id

and then use it [[self alloc] init]

on instantiation to make your class subclass safe.

+3


a source







All Articles