Objective-C Subclass Question

I have a class called "Level", which is a subclass of NSObject.

Then I have a class called Level_1_1, which is a subclass of the level.

Is it allowed to print as

Level* aLevel = [Level_1_1 alloc];

instead

Level_1_1* theLevel = [Level_1_1 alloc];

? :)

I try this and I don't have any warnings, just wondering if this is good to do?

+3
source share
1 answer

It is allowed; but there are two things you should keep in mind:

  • Never, never call +allocwithout binding it with -initor -initWith*unless you are fooling yourself at runtime.
  • , , , Level_1_1. , Level , , ; .

:

SomeClass *aSomeclass = [[MySomeClass alloc] init];

(, MySomeClass SomeClass). , : , , NSString [[NSString alloc] init], :

// This is an NSConstString masquerading as an NSString
NSString *aString = @"This is an NSConstString";
// This probably gives you an NSConcreteString, or something of the kind
NSString *anotherString = [NSString stringWithString:aString];

. .

NSArray *someString = @"Not An Array!";
NSLog(@"%s", [someString UTF8String]);  // this works, but creates
                                        // a compile-time warning
NSLog(@"%u", [someString count]);       // this creates a runtime error,
                                        // but none while compiling

- , , NSArray. , Objective-C 2.0 ( .).

+6

Source: https://habr.com/ru/post/1741051/


All Articles