RHBCastingObjC 0.1.6

RHBCastingObjC 0.1.6

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Aug 2016

Maintained by Lazar Otasevic.



Usage

This mini library will make your code safer and more readable when you deal with casting. This kind of library is not needed for Swift, because Swift will force you to write safe and readable casting code out of the box.

Example1

So, instead of doing it standard way, like:

if ([self.collection isKindOfClass:[NSArray class]]) {
    NSArray *array = (NSArray *)self.collection;
    /// ... some other code

This pod enables you to do the following:

NSArray *array = [NSArray rhb_dynamicCast:self.collection];
if (array) {
    /// ... some other code

Note that self.collection is actually duplicated code in the standard approach, and it may have performance impacts if its created on the fly.

Method rhb_dynamicCast: uses isKindOfClass: internally. There is another method named rhb_strictDynamicCast: that uses isMemberOfClass: internally.

Example2

To make extra security checks during development (asserts) then one should use the following:

NSArray *array = [NSArray rhb_verifyCast:self.collection];

instead of standard way:

NSArray *array = (NSArray *)self.collection;

First way will do the assertion and check if the cast is valid in debug mode to prevent errors during development. Both ways will have the same effect in release mode.

Installation

RHBCastingObjC is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "RHBCastingObjC"

Author

Lazar Otasevic, [email protected]

License

RHBCastingObjC is available under the MIT license. See the LICENSE file for more info.