开发者

Safe casting in Objective C

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

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


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:


You can turn on compiler flags to warn you in cases like this. This particular mistake would be caught by the -Wconversion flag.


Which feature of C++ do you think will help you cast a 64-bit long to a 32-bit int?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜