-
-
Save jkyin/4918b4ea43b3188f0e2dc1799b60ca8d to your computer and use it in GitHub Desktop.
Retrofitting containsString: on iOS 7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 80000 | |
@interface NSString (PSPDFModernizer) | |
// Added in iOS 8, retrofitted for iOS 7 | |
- (BOOL)containsString:(NSString *)aString; | |
@end | |
#endif | |
// PSPDFModernizer.m | |
#import "PSPDFModernizer.h" | |
#import <objc/runtime.h> | |
#if __IPHONE_OS_VERSION_MIN_REQUIRED < 80000 | |
@implementation NSString (PSPDFModernizerCreator) | |
+ (void)load { | |
@autoreleasepool { | |
[self pspdf_modernizeSelector:NSSelectorFromString(@"containsString:") withSelector:@selector(pspdf_containsString:)]; | |
} | |
} | |
+ (void)pspdf_modernizeSelector:(SEL)originalSelector withSelector:(SEL)newSelector { | |
if (![NSString instancesRespondToSelector:originalSelector]) { | |
Method newMethod = class_getInstanceMethod(self, newSelector); | |
class_addMethod(self, originalSelector, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)); | |
} | |
} | |
// containsString: has been added in iOS 8. We dynamically add this if we run on iOS 7. | |
- (BOOL)pspdf_containsString:(NSString *)aString { | |
return [self rangeOfString:aString].location != NSNotFound; | |
} | |
@end | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment