Checking the nil value returned by NSClassFromString

We can create a class with NSClassFromString:

Class class = NSClassFromString(@"someClass");
id object = [[class alloc] init];
[object test];

There is no problem.

NSClassFromStringwill return nilif the class specified by the string does not exist.

Fair enough, and call [object test];when the object is nil (since the class nil) will not cause any problems.

As an experiment, I changed @"someClass"to something else that does not exist. By registering a value class(when it NSClassFromStringreturns nil), it shows 0x0. And subsequently I tried replacing as a test:

id object = [[class alloc] init];

with the following in order:

id object = [[Nil alloc] init];

id object = [[nil alloc] init];

id object = [[NSNull alloc] init];

It just id object = [[NSNull alloc] init];does not cause compilation, but will crash when this line is reached:

[object test];

, , class nil nil, NSClassFromString nil Class class [[class alloc] init]; ? class nil .

, id object = [[Nil alloc] init]; id object = [[Nil alloc] init];.

'void *'.

+4
2

nil void*, id. void*.

NULL, Nil nil - void*, . nil id, :

char* foo = nil;

.

nil, :

[(id)nil description];
+5

Objective C nil, , nil, , , , , , .

: @ , nil - void *, id, . - [[(id)nil alloc] init] . , nil s, .

+3

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


All Articles