ABUtils 是一个提供预编写功能的库。通过减少重复代码的需要,它使编写更干净的代码变得更容易,同时更容易开始项目而无需重写代码。随着功能需求的增加,功能将被添加。
要运行示例项目,首先克隆仓库,然后从示例目录运行 pod install
。
ABUtils 通过 CocoaPods 提供。要安装它,只需在 Podfile 中添加以下行
pod "ABUtils"
Print 可以用来替代 NSLog。通过 CFShow 到控制台打印。同时,在产品中不会进行打印,只有在调试模式下应用时才会打印。Print 有两种方法
/// Print object to console using CFShow, includes a tag which will preceed the object in the print (ie. "Tag: Object")
+ (void) print: (id) object tag: (NSString *) tag;
/// Print a string to console using CFShow
+ (void) printString: (NSString *) string;
以下宏可以用来轻松确定用户设备的 iOS 版本
/// Returns true if the device's iOS is version 5.0 or later
IS_OS_5_OR_LATER
/// Returns true if the device's iOS is version 6.0 or later
IS_OS_6_OR_LATER
/// Returns true if the device's iOS is version 7.0 or later
IS_OS_7_OR_LATER
/// Returns true if the device's iOS is version 8.0 or later
IS_OS_8_OR_LATER
/// Returns true if the device's iOS is version 9.0 or later
IS_OS_9_OR_LATER
/// Returns true if the device's iOS is version 10.0 or later
IS_OS_10_OR_LATER
'notNull' 函数有助于确定一个对象是否有效,即其值不是null或nil。在 Swift 中,您可以使用 '!' 确定变量是否为 nil。这在 Objective-C 中不可用,因此可以使用此函数替代。如果您想确定变量是否为 null 或 nil,它有一个姊妹函数 'isNull'。
/// Returns true if the object is not null or nil, otherwise returns false
+ (BOOL)notNull:(id)object;
/// Returns true if the object is null or nil, otherwise returns false
+ (BOOL)isNull:(id)object;
与 'notNull' 函数类似,'notNil' 在确定对象是否为 nil 时很有用。在 Swift 中,您可以使用 '!' 来确定变量是否为 nil。这在 Objective-C 中不可用,因此可以使用此函数替代。如果您想确定变量是否为 nil,它有一个姊妹函数 'isNil'。
/// Returns true if the object is not nil, returns true if the object is null
+ (BOOL)notNil:(id)object;
/// Returns true if the object is nil, returns false if the object is null
+ (BOOL)isNil:(id)object;
《notBlank》函数用于判断字符串是否为空。此函数可用于判断用户是否填写了文本字段。
/// Returns true if the object is not just spaces or blank, otherwise returns false
+ (BOOL)notBlank: (NSString *) text;
要确定电子邮件地址是否有效格式,可以使用《isValidEmail》函数。此函数使用正则表达式来确保电子邮件地址格式正确,并且只包含有效的电子邮件地址字符。
/// Determines if email is valid format
+ (BOOL)isValidEmail: (NSString *)string;
最后的条件函数是《boolValue》。此函数用于从NSString或NSNumber中解析出布尔值。
/// Determines bool value for a NSString or NSNumber
+ (BOOL) boolValue: (id) value;
有几个函数用于修改和清理字符串
/// Removes characters from the string that are not an upper or lowercase letter
+ (NSString *)removeSpecialCharacters: (NSString *) text;
/// Trims white space from the ends of a string, such as ' ' and '\n'
+ (NSString *)trimWhiteSpace: (NSString *) text;
/// Trims multispace from a string, and turns '\n\n' into '\n' as well as ' ' into ' '
+ (NSString *)trimMultiSpace: (NSString *) text;
/// Trims white space and removes extra new lines from string, and replaces instances of "\n\n" with "\n" and " " with " "
+ (NSString *)trimWhiteAndMultiSpace: (NSString *) text;
/// Removes all spaces and new lines from a string
+ (NSString *)removeSpaces: (NSString *) text;
《timeZone》函数可以快速返回用户时区,并以字符串格式返回
/// Determines the timezone of the user, and returns a string value of that time zone
+ (NSString *)timeZone;
《ordinalSuffixFromInt》函数返回整数的正确序列后缀(如st, rd, nd)。《ordinalNumberString》函数通过提供序列后缀并将其附加到数字上,简化了上述函数,从而返回完整的字符串
/// Returns the ordinal suffix for a number (ie. th, st, nd)
+ (NSString *)ordinalSuffixFromInt:(NSInteger)number;
/// Returns the number in string format with its proper ordinal suffix (ie. 5th, 1st, 2nd)
+ (NSString *)ordinalNumberString:(NSInteger)number;
《endOfDay》函数返回一个表示提供日期晚上11:59的NSDate。《endOfTomorrow》函数与上一个函数类似,但提供的是明天的日期结束时间。
// Returns the NSDate for the end of date received
+ (NSDate *)endOfDay:(NSDate *)date;
// Returns the NSDate for the end of tomorrow
+ (NSDate *)endOfTomorrow;
《differenceMet》函数用于判断从现在到提供的日期之间是否已过去指定天数。
/// Determines if the time difference between now and the date received is more or equal to the number of days received
+ (BOOL)differenceMet: (NSDate *) time days: (int) days;
以下函数从数字返回字符串格式
/// Returns decimal string for the number, with commas
- (NSString *) decimalNumber: (NSNumber *)value;
/// Returns int in comma format
+ (NSString *) commaFormat: (int)value;
/// Accepts number and returns it in comma format
+ (NSString *) commaFormatNumber: (NSNumber *)value;
《colorWithHexString》函数是一个非常实用的函数。目前,还没有简单的方法可以使用十六进制字符串来声明UIColor。此函数接受6位十六进制字符串(不要使用快捷十六进制字符串)并返回相应的UIColor。
/// Returns a UIColor for a hex string value
+ (UIColor *)colorWithHexString:(NSString*)hex;
以下函数提供一种简单的方法来确定设备的型号
/// Returns the platform type of the user's phone (ie. "iPhone 5S")
+ (NSString *)platformType;
/// Returns the screen size of the device (iPhone only) (ie. "iPhone Plus size")
+ (NSString *)modelTypeString;
/// Determines model type for screen size (ie. iPhone4Size)
+ (ModelSizeType)modelTypeSize;
以下函数提供编辑和编码媒体的工具
/// Send image, and returns same image scaled to 1080px and rotated accordingly
+ (UIImage *)scaleAndRotateImage:(UIImage *)image;
/// Generated a black and white image from the image received
+ (UIImage *) generateBlackAndWhiteImage: (UIImage *) image;
/// Generates an image with sepia tone from the image receieved
+ (UIImage *) generateSepiaImage: (UIImage *) image;
/// Generates a saturated image from the image received
+ (UIImage *) generateSaturatedImage: (UIImage *) image;
/// Encodes image to Base 64 for Backend
+ (NSString *)encodeToBase64String:(UIImage *)image;
/// Encodes video to Base 64 for Backend
+ (NSString *)encodeVideoToBase64String:(NSURL *)videoURL;
‘reorient’: 接受一个方向改变和大小,并返回必要的 CGAffineTransform 来接收请求的结果。当与 AVAssetExporters 和自定义 AVPlayers 一起工作时,这个函数非常有用。任何需要使用 AVAssetExporter 来裁剪视频的人都会了解这个函数在导出 AVAsset 时达到正确的视频方向有多么有用。
/// Provides the orientation transformation for reorienting videos
+ (CGAffineTransform) reorient: (OrientationType)orientation size: (CGSize)size;
Andrew Boryk,[email protected]
ABUtils 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。