TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Mar 2015 |
Maintained by Anthony Shoumikhin.
A collection of useful categories for standard Cocoa classes. Also available among CocoaPods.
Pull requests are welcome! :)
Simply add the following line to your precompiled header:
#import <NSXtensions.h>
And you'll boost the following Cocoa classes (in alphabetical order):
Precompile definitions for some commonly-used boilerplate code.
TODO(message)
FIXME(message)
SHOW_ALERT(title, message, delegate, cancelButtonTitle, ...)
Example:
UIAlertView *alert = SHOW_ALERT(@"Title", @"And message", nil, @"OK", @"Other"); [alert dismissWithClickedButtonIndex:0 animated:YES];
SYNTHESIZE_STATIC_PROPERTY(_type_, _name_, ...)
Example:
SYNTHESIZE_STATIC_PROPERTY(NSString *, bundleID, { NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier]; return bundleID; })
SYNTHESIZE_SINGLETON_FOR_CLASS(classname)
Example:
@implementation MyClass SYNTHESIZE_SINGLETON_FOR_CLASS(MyClass) + (instancetype)init { //custom initialization } @end void foo() { MyClass.shared.bar = @"Singleton"; }
//get the current zoom level
- (NSInteger)zoomLevel;
//change map's location and zoom level
- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(NSUInteger)zoomLevel animated:(BOOL)animated;
//helper methods whose names speak for themselves
+ (unsigned long long)millisecondsSince1970;
- (NSInteger)secondsAfterDate:(NSDate *)date;
- (NSInteger)minutesAfterDate:(NSDate *)date;
- (NSInteger)minutesBeforeDate:(NSDate *)date;
- (NSInteger)hoursAfterDate:(NSDate *)date;
- (NSInteger)hoursBeforeDate:(NSDate *)date;
- (NSInteger)daysAfterDate:(NSDate *)date;
- (NSInteger)daysBeforeDate:(NSDate *)date;
//merge two dictionaries
+ (NSDictionary *)dictionaryByMerging:(NSDictionary *)first with:(NSDictionary *)second;
- (NSDictionary *)mergeWith:(NSDictionary *)other;
//a friendly human-readable localized description
- (NSString *)friendlyLocalizedDescription;
//create an NSError object with given domain, code and userInfo with a friendly human-readable localized description for known domains and codes
+ (instancetype)friendlyErrorWithDomain:(NSString *)domain andCode:(NSInteger)code;
//pretty-formatted stack trace
- (NSArray *)backtrace;
//get standard directories paths and URLs
+ (NSURL *)documentsURL;
+ (NSString *)documentsPath;
+ (NSURL *)libraryURL;
+ (NSString *)libraryPath;
+ (NSURL *)cachesURL;
+ (NSString *)cachesPath;
//prevent syncing with iCloud
+ (BOOL)addSkipBackupAttributeToFile:(NSString *)path;
//check available disk space (Megabytes)
+ (double)availableDiskSpace;
//convert index path to a string like "1.2.3.4" and back
+ (NSIndexPath *)indexPathWithString:(NSString *)path;
- (NSString *)stringValue;
//convenience methonds
- (NSUInteger)firstIndex;
- (NSUInteger)lastIndex;
//create new object
- (NSManagedObject *)newObjectOfEntity:(NSString *)name;
//fetch objects with options, zero limit means unlimited
- (NSArray *)objectsOfEntity:(NSString *)name withPredicate:(NSPredicate *)predicate sortDescriptors:(NSArray *)sortDescriptors andFetchLimit:(NSUInteger)limit;
//delete multiple objects
- (void)deleteObjects:(NSArray *)objects;
//delete objects with options
- (void)deleteObjectsOfEntity:(NSString *)name withPredicate:(NSPredicate *)predicate andSortDescriptors:(NSArray *)sortDescriptors;
//a new array storing weak references to objects
+ (id)arrayUsingWeakReferences;
+ (id)arrayUsingWeakReferencesWithCapacity:(NSUInteger)capacity;
//randomly mix contents
- (void)shuffle;
//use as queue ADT
- (void)enqueue:(id)anObject;
- (id)dequeue;
//use as stack ADT
- (void)push:(id)anObject;
- (id)pop;
//add or change methods in run-time
+ (void)swizzleMethod:(SEL)originalMethod withMethod:(SEL)newMethod;
+ (void)appendMethod:(SEL)newMethod fromClass:(Class)aClass;
+ (void)replaceMethod:(SEL)aMethod fromClass:(Class)aClass;
//check whether an object or class implements or inherits a specified method up to and exluding a particular class in hierarchy
- (BOOL)respondsToSelector:(SEL)selector untilClass:(Class)stopClass;
- (BOOL)superRespondsToSelector:(SEL)selector;
- (BOOL)superRespondsToSelector:(SEL)selector untilClass:(Class)stopClass;
+ (BOOL)instancesRespondToSelector:(SEL)selector untilClass:(Class)stopClass;
//returns a dictionary with integer keys and all objects in set as values
- (NSDictionary *)indexedDictionary;
//first substring that matches a given regular expression
- (NSString *)substringWithRegularExpressionPattern:(NSString *)pattern options:(NSRegularExpressionOptions)options;
//short and smart way to deal with URL percent escapes
- (NSString *)URLEncoded;
- (NSString *)URLDecoded;
//hash of contents
- (NSString *)MD5;
- (NSString *)SHA256;
//helper to insert dashes in 32-chars length string to make it look like UUID
- (NSString *)likeUUID;
//get base host URL
- (NSURL *)hostURL;
//generate a new unique identifier
+ (NString *)makeUUID;
//display an alert and execute a completion block on dismiss
- (void)showWithCompletion:(void (^)(UIAlertView *alertView, NSInteger buttonIndex))completionBlock;
//application's bundle identifier
+ (NSString *)identifier;
//application's bundle version
+ (NSString *)version;
//application's frame regardless current orientation
+ (CGRect)frame;
//application's status bar height
+ (CGFloat)statusBarHeight;
//pretty-formatted backtrace of current point of execution
+ (NSArray *)backtrace;
//call, email or open ULR with an ability to quickly return back
+ (void)call:(NSString *)phoneNumber andShowReturn:(BOOL)shouldReturn;
+ (void)email:(NSString *)address andShowReturn:(BOOL)shouldReturn;
+ (void)openURL:(NSURL *)url andShowReturn:(BOOL)shouldReturn;
//set a title for all UIControl states at once
- (void)setTitleForAllStates:(NSString *)title;
//set an image for all UIControl states at once
- (void)setImageForAllStates:(UIImage *)image;
//convert "#RRGGBB" to UIColor
+ (UIColor *)colorWithHTMLColor:(NSString *)HTMLColor;
//globally unique device identifier (SHA256 of Wi-Fi MAC address before iOS 7.0, and identifier for vendor now)
+ (NSString *)uniqueIdentifier;
//hadrware device name
+ (NSString *)machineName;
//human readable device name
+ (NSString *)deviceName;
//check if a specific system version is supported
+ (BOOL)systemVersionIsAtLeast:(NSString *)version;
//get the screen resolution of the device
+ (UIResolution)resolution;
//how much of free memory remains in system (Megabytes)
+ (double)availableMemory;
//check the device type
+ (BOOL)isSimulator;
+ (BOOL)isPhone;
+ (BOOL)isPad;
+ (BOOL)isPod;
+ (BOOL)isAppleTV;
//check if the device is cracked
+ (BOOL)isJailbroken;
//get the MAC addresses of installed network interfaces (deprecated)
+ (NSString *)WiFiMACAddress;
+ (NSString *)CellularMACAddress;
//convenient way to download an image
- (id)initWithContentsOfURL:(NSURL *)URL;
+ (id)imageWithContentsOfURL:(NSURL *)URL;
//applies the blur effect to the image
- (UIImage *)applyBlurWithRadius:(CGFloat)blurRadius tintColor:(UIColor *)tintColor saturationDeltaFactor:(CGFloat)saturationDeltaFactor maskImage:(UIImage *)maskImage;
//returns a new image representing a cropped part of the original
- (UIImage *)cropToRect:(CGRect)rect;
//animates navigation action with custom CoreAnimation transitions
- (void)pushViewController:(UIViewController *)viewController withTransitionType:(NSString *)type;
- (UIViewController *)popViewControllerWithTransitionType:(NSString *)type;
- (NSArray *)popToRootViewControllerWithTransitionType:(NSString *)type;
- (NSArray *)popToViewController:(UIViewController *)viewController withTransitionType:(NSString *)type;
//methods to hide/show the tab bar similarly to the standard navigation bar behavior
@property (nonatomic, getter = isTabBarHidden) BOOL tabBarHidden;
- (void)setTabBarHidden:(BOOL)hidden animated:(BOOL)animated;
//animated swipe-like transition between presented view controllers
- (void)swipeToIndex:(NSUInteger)index;
//a convenient way to control UIView's frame
@property (nonatomic) CGFloat x;
@property (nonatomic) CGFloat y;
@property (nonatomic) CGPoint origin;
@property (nonatomic) CGFloat width;
@property (nonatomic) CGFloat height;
@property (nonatomic) CGSize size;
@property (nonatomic) CGFloat dx;
@property (nonatomic) CGFloat dy;
@property (nonatomic) CGVector bound;
//returns a parent view controller
- (UIViewController *)viewController;
//resigns the first responder, if found in this view hierarchy
- (BOOL)resignFirstResponderRecursively;
//find a superview of specific class
- (UIView *)superviewOfClass:(Class)aClass;
//move with animation
- (void)moveTo:(CGPoint)destination duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options;
//load HTML content with specific font
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL font:(UIFont *)font;
//clear all saved cookies
- (void)clearCookies;