NSXtensions
A collection of useful extensions for standard Cocoa classes
Install / Use
/learn @shoumikhin/NSXtensionsREADME
NSXtensions
A collection of useful categories for standard Cocoa classes. Also available among CocoaPods.
Pull requests are welcome! :)
Usage
Simply add the following line to your precompiled header:
#import <NSXtensions.h>
And you'll boost the following Cocoa classes (in alphabetical order):
- MacroX
- MKMapView
- NSDate
- NSDictionary
- NSError
- NSException
- NSFileManager
- NSIndexPath
- NSManagedObjectContext
- NSMutableArray
- NSObject
- NSSet
- NSString
- NSURL
- NSUUID
- UIAlertView
- UIApplication
- UIButton
- UIDevice
- UIImage
- UINavigationController
- UITabBarController
- UIView
- UIWebView
Details
MacroX.h
Precompile definitions for some commonly-used boilerplate code.
- Mark a line of code with a "TODO" or "FIXME" prefixed compiler warning:
TODO(message)
FIXME(message)
- Create and show a UIAlertView with the given values:
SHOW_ALERT(title, message, delegate, cancelButtonTitle, ...)
Example:
UIAlertView *alert = SHOW_ALERT(@"Title", @"And message", nil, @"OK", @"Other");
[alert dismissWithClickedButtonIndex:0 animated:YES];
- Rapidly implement a read-only static property of a given type and name.
SYNTHESIZE_STATIC_PROPERTY(_type_, _name_, ...)
Example:
SYNTHESIZE_STATIC_PROPERTY(NSString *, bundleID, { NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];
return bundleID; })
- Synthesize boilerplate code for a given class to support the singleton design pattern:
SYNTHESIZE_SINGLETON_FOR_CLASS(classname)
Example:
@implementation MyClass SYNTHESIZE_SINGLETON_FOR_CLASS(MyClass) + (instancetype)init { //custom initialization } @end
void foo() { MyClass.shared.bar = @"Singleton"; }
MKMapView
//get the current zoom level
- (NSInteger)zoomLevel;
//change map's location and zoom level
- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(NSUInteger)zoomLevel animated:(BOOL)animated;
NSDate
//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;
NSDictionary
//merge two dictionaries
+ (NSDictionary *)dictionaryByMerging:(NSDictionary *)first with:(NSDictionary *)second;
- (NSDictionary *)mergeWith:(NSDictionary *)other;
NSError
//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;
NSException
//pretty-formatted stack trace
- (NSArray *)backtrace;
NSFileManager
//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;
NSIndexPath
//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;
NSManagedObjectContext
//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;
NSMutableArray
//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;
NSObject
//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;
NSSet
//returns a dictionary with integer keys and all objects in set as values
- (NSDictionary *)indexedDictionary;
NSString
//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;
NSURL
//get base host URL
- (NSURL *)hostURL;
NSUUID
//generate a new unique identifier
+ (NString *)makeUUID;
UIAlertView
//display an alert and execute a completion block on dismiss
- (void)showWithCompletion:(void (^)(UIAlertView *alertView, NSInteger buttonIndex))completionBlock;
UIApplication
//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;
UIButton
//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;
UIColor
//convert "#RRGGBB" to UIColor
+ (UIColor *)colorWithHTMLColor:(NSString *)HTMLColor;
UIDevice
//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;
UIImage
//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;
UINavigationController
//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;
UITabBarController
//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 pr
Related Skills
node-connect
333.7kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
82.0kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
333.7kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
82.0kCommit, push, and open a PR


