DRYUtilities 2.1.0

DRYUtilities 2.1.0

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最新发布2016年10月

Mike SeghersJoris DuboisJens Goeman维护。



DRYUtilities 旨在为 iOS 开发中常用的某些功能提供服务。它允许您快速开始,而无需在不同的项目中重复编写相同的功能。使用 DRYUtilities 将使您的代码比之前更加遵循 DRY 原则。

使用

要运行示例项目,请先克隆仓库,然后在 Example 目录中运行 pod install 命令。该项目包含 DRYUtilities 中提供的多数工具的示例用法。

启动应用后,您将看到一个表格,其中包含每个示例的行。点击它以查看示例如何操作,并阅读以下说明。

配置工具

这些工具提供了一种轻松获取从不同类型的资源文件中读取的属性的功能。

示例用法:ExampleConfigurationViewController

DRYResourceReader

DRYResourceReader 提供一个基于指定键从资源读取属性的操作。首先,您需要创建一个 DRYResourceReader。DRYUtilities 包含一个 DRYPlistResourceReader,它可以读取 .plist 文件。它还提供了一种提供备用资源的能力。如果使用原始资源文件无法解析属性,将使用此备用资源。

//without using fallback
id <DRYResourceReader> reader = [[DRYPlistResourceReader alloc] initWithPlistNamed:@"Configuration"];

//using fallback
id <DRYResourceReader> fallback = [[DRYJsonResourceReader alloc] initWithJsonNamed:@"Fallback"];
id <DRYResourceReader> reader = [[DRYPlistResourceReader alloc] initWithPlistNamed:@"Configuration" fallbackResourceReader:@"DefaultConfiguration"];

DRYBaseDynamicPropertyProvider

使用 DRYBaseDynamicPropertyProvider,您可以创建动态属性以从您的资源文件中检索值。通过在资源文件中使用的键扩展 DRYBaseDynamicPropertyProvider

@interface ExampleDynamicPropertyProvider : DRYBaseDynamicPropertyProvider

@property (nonatomic, readonly) NSString *firstProperty;

@end

@implementation ExampleDynamicPropertyProvider 
@dynamic firstProperty;
@end

从现在开始,您可以针对资源文件中的属性进行定位

id<DRYResourceReader> reader = ...;
ExampleDynamicPropertyProvider *propertyProvider = [[ExampleDynamicPropertyProvider alloc] initWithResourceReader:reader];
NSLog(@"Property value: %@", propertyProvider.firstProperty);

Foundation 工具

Foundation 工具为不同的 Foundation 类提供实用方法。

NSString DRYHex

示例用法:ExampleDRYNSStringHexViewController

dryStringFromHexWithData: 将返回一个表示 NSData 对象的十六进制字符串。

char bytes[3] = {0, 1, 2};
NSData *data = [NSData dataWithBytes:bytes length:3];
NSString *hexString = [NSString dryStringWithHexFromData:data];

NSLog(@"Hex string: %@", hexString); //Hex string: 000102

NSString DRYTemplateReplacement

示例用法:ExampleDRYNSStringTemplateReplacementViewController

dryStringByReplacingTemplatesWithValuesFromDictionary: 可以用于替换字符串中的占位符。它将返回一个新字符串,其中包含了替换占位符后的结果。

NSString *text = @"Hello {name}, you live in {country}";
NSDictionary *values = @{@"name" : @"Jos", @"country": @"Belgium"};
NSString *resolvedText = [text dryStringByReplacingTemplatesWithValuesFromDictionary:values];

   NSLog(@"Resolved text: %@", resolvedText); //Resolved text: Hello Jos, you live in Belgium

如果您想要使用不同的模板符号,可以使用以下内容:

NSString *text = @"Hello __name//, you live in __country//";
NSDictionary *values = @{@"name" : @"Jos", @"country": @"Belgium"};
NSString *resolvedText = [text dryStringByReplacingTemplatesWithValuesFromDictionary:values withTemplatePrefix:@"__" templateSuffix:@"//"];
NSLog(@"Resolved text: %@", resolvedText); //Resolved text: Hello Jos, you live in Belgium

NSString DRYUtil

dryIsNotBlank 做您想象中的事情。如果字符串不是空的,它将返回 YES;如果字符串是空的,它将返回 NO

NSLog(@"Is not blank: %d", [@"test" dryIsNotBlank]); //Is not blank: 1 
NSLog(@"Is not blank: %d", [@"" dryIsNotBlank]); //Is not blank: 0 
NSLog(@"Is not blank: %d", [@" " dryIsNotBlank]); //Is not blank: 0
NSLog(@"Is not blank: %d", [nil dryIsNotBlank]); //Is not blank: 0
NSLog(@"Is not blank: %d", [@"\t\n" dryIsNotBlank]); //Is not blank: 0

NSObject/NSNull DRYNSNullSupport

示例用法:ExampleDRYNSNullSupportViewController

dryValueOrNil 可以用于将 [NSNull null] 值转换为 nil。这在例如将包含 NSNull 值的 NSDictionary 保存到 Core Data 对象时非常有用。

NSString *string = [[NSNull null] dryValueOrNil]; // string = nil
NSString *string = [@"value" dryValueOrNil]; // string = @"value"

您也可以检查一个值是否为 [NSNull null]。如果值为 nil 或其他不是 [NSNull null] 的内容,则它将返回 NO

NSLog(@"Result: %d", [@"" dryIsNSNull]); //Result: 0
NSLog(@"Result: %d", [[NSNull null] dryIsNSNull]); //Result: 1

[NSNull dryNullOrValue:] 可以用于获取值或 [NSNull null](如果值为 nil)。

UIKit 工具

UIColor DRYUtil

示例用法:ExampleDRYUIColorDRYUtilViewController

dryColorFromRGBHexString: 提供了根据颜色的 RGB 十六进制表示创建 UIColor 对象的能力。

UIColor *redColor = [UIColor dryColorFromRGBHexString:@"#FF0000"];

UIImage ColorPixel

ColorPixel 分类提供根据单个颜色创建 UIImage 实例的能力。它将创建一个填充了提供颜色的 1px 的 UIImage。

UIImage *redImage = [UIImage dryImageWithColor:[UIColor redColor]];

您也可以创建不同大小的 UIImage。但是,这会消耗更多资源,因此在使用此方法时请小心。

UIImage *redImage = [UIImage dryImageWithColor:[UIColor redColor] size:CGSizeMake(10, 10)];

UIView DRYLoadFromNib

示例用法:ExampleDRYLoadFromNibController

+ (instancetype)dryViewWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundleOrNil owner:(id)owner tag:(NSInteger)tag;

上述方法是最详细的方法之一。可以使用它根据 .xib 文件创建 UIView 实例。用 nibName 指定应该使用哪个 .xib 文件。.xib 文件可以位于另一个包中,这可以通过 nibName 参数设置。tag 参数表示应基于 tag 的值加载哪个视图。如果没有提供,它将只返回具有与类相同的 instancetype 的 .xib 文件中的第一个视图。

安全工具

DRYSecurityCredentials 表示一个用户名和密码对,可以通过 DRYSecurityStoreWrapper 保存到安全存储中。此类的实例不应在内存中保持时间超过绝对必要的时间!

DRYSecurityCredentials *securityCredentials = [[DRYSecurityCredentials alloc] initWithUserName:@"superAwesomeUsername" andPassword:@"superSecurePassword"];

DRYSecurityStoreWrapper 是一个包装类,提供了根据 identifier 识别的用户名/密码对的访问权限。它允许您使用 iOS 提供的 Security 库来存储此用户名/密码对。

DRYSecurityStoreWrapper *securityStoreWrapper = [[DRYSecurityStoreWrapper alloc] initWithIdentifier:@"remotingCredentials"];
[securityStoreWrapper persistWithCredentials:securityCredentials];

CoreData 工具

DRYCoreDataTableViewController 提供了一个由 NSFetchedResultsController 支持的表视图控制器。当数据发生变化时,控制器将更新其表视图。

创建一个 DRYCoreDataTableViewController 实例,并添加一个 NSFetchedResultsController 以开始工作。您还可以告诉控制器是否使用区域索引。这可以通过设置正确的 needsSectionIndex 值来完成。如果设置为 YES,则 NSFetchedResultsControllersectionIndexTitles 被用于显示区域标题。

要求

安装

如果您只想安装特定的类集,可以安装以下子规范

pod "DRYUtilities/Configuration"
pod "DRYUtilities/CoreData"
pod "DRYUtilities/Foundation"
pod "DRYUtilities/Networking"
pod "DRYUtilities/Security"
pod "DRYUtilities/UIKit"

作者

Mike Seghers,[email protected] Joris Dubois,[email protected] Willem Van Pelt,[email protected]

许可

DRYUtilities 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。