Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Safe casting in Objective C

Tags:

objective-c

Is there anything like C++s safe casts in Objective-C?

I know that they are in Objective C++, but I am unsure about possible side effects. Using Objective C++ may slow compilation time - are there any other reasons not to use it?

like image 540
Casebash Avatar asked Feb 03 '10 05:02

Casebash


1 Answers

Objective-c does have C++ safe casts. Alternatively, we can use runtime reflection:

id myOb=[someObject getObject];
NSAssert([myOb isKindOfClass:[MyClass class]], @"Return value is not of type MyClass as expected.");
MyClass * newOb= (MyClass *)myOb;

References:

Cocoa with Love:

like image 55
Casebash Avatar answered Nov 15 '22 18:11

Casebash