TestsTested | ✓ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Jan 2015 |
Maintained by Fabian Canas.
Objective-C is C, and C is perilous. Don't blindly cast objects. Stick to high-level language features, and write more readable code.
Do things like this:
NSMutableArray *mArray = [NSMutableArray safe_cast:array];
// `mArray` is nil if `array` is not a mutable array, or `array` if it is.
Or only call methods on collection members that can respond to the selector.
[array safe_makeObjectsSafelyPerformSelector:@selector(method)];
Or enumerate with a block on objects that are of a specific kind.
[array safe_enumerateObjectsOfKind:[MyObject class]
usingBlock:^(MyObject *obj, NSUInteger idx, BOOL *stop) {
[obj setNumber:@3];
}];
Or a protocol
[array safe_enumerateObjectsConformingToProtocol:@protocol(MyProtocol)
usingBlock:^(id<MyProtocol> *obj, NSUInteger idx, BOOL *stop) {
[obj setNumber:@3];
}];
SafeCast has quite a few methods like this, covering NSArray
, NSSet
, NSDictionary
, and NSOrderedSet
.
I doubt it would be a good idea to give coverage to collections like NSMapTable
or NSHashTable
, so that's not planned.
Well, first of all, it's really small. The documentation in the headers is much bigger than the code. And it's tested. You may not need it. But it has a lot of things going for it.
SafeCast is available as a CocoaPod.
pod 'SafeCast'
It definitely takes fewer lines of code. In my opinion it's easier to read and think about than the more verbose way of doing it safely. It keeps your type-safety checks up front and at a high level.
Maybe you know what you're doing. But do you always do the right thing? How many people are in your code base? And how well do they all know Objective-C's C underbelly? More and more developers are coming in having worked with higher-level or dynamic languages, and may not realize what's safe and what's not.
SafeCast wants to make it easier to safely cast ... to cast only when it's safe to do so.
Please send feedback, pull-requests, and coffee.