Skip to content

Instantly share code, notes, and snippets.

@advantis
Created May 15, 2013 07:30
Show Gist options
  • Save advantis/5582203 to your computer and use it in GitHub Desktop.
Save advantis/5582203 to your computer and use it in GitHub Desktop.
Rough analogue of C++ static_cast and dynamic_cast
//
// Copyright © 2012 Yuri Kotov
//
#import <Foundation/Foundation.h>
@interface NSObject (ADVCasting)
+ (instancetype) staticCast:(id)from;
+ (instancetype) dynamicCast:(id)from;
@end
//
// Copyright © 2012 Yuri Kotov
//
#import "NSObject+ADVCasting.h"
@implementation NSObject (ADVCasting)
+ (instancetype) staticCast:(id)from
{
return from;
}
+ (instancetype) dynamicCast:(id)from
{
return [from isKindOfClass:self] ? from : nil;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment