NSXtensions 0.71

NSXtensions 0.71

测试已测试
语言语言 Obj-CObjective C
许可 MIT
发布上次发布2015年3月

Anthony Shoumikhin 维护。



  • Anthony Shoumikhin

   

NSXtensions

一组标准 Cocoa 类的有用类别。CocoaPods 中也有。

欢迎提交拉取请求! :)

使用方法

只需将以下行添加到您的预编译头

#import <NSXtensions.h>

然后您将增强以下 Cocoa 类(按字母顺序):

详细信息

MacroX.h

一些常用样板代码的预编译定义。

  • 用“TODO”或“FIXME”前缀标记一行代码的编译器警告
TODO(message)
FIXME(message)
  • 创建并显示一个 UIAlertView,并给定值
SHOW_ALERT(title, message, delegate, cancelButtonTitle, ...)

例子

UIAlertView *alert = SHOW_ALERT(@"Title", @"And message", nil, @"OK", @"Other");

[alert dismissWithClickedButtonIndex:0 animated:YES];
  • 快速实现给定类型和名称的只读静态属性。
SYNTHESIZE_STATIC_PROPERTY(_type_, _name_, ...)

例子

SYNTHESIZE_STATIC_PROPERTY(NSString *, bundleID,
{
  NSString *bundleID = [[NSBundle mainBundle] bundleIdentifier];

  return bundleID;
})
  • 为给定类生成样板代码以支持单例设计模式
SYNTHESIZE_SINGLETON_FOR_CLASS(classname)

例子

@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 presented view controllers
- (void)swipeToIndex:(NSUInteger)index;

UIView

//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;

UIWebView

//load HTML content with specific font
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL font:(UIFont *)font;

//clear all saved cookies
- (void)clearCookies;