Coder Social home page Coder Social logo

yuvakumar-phtn / dryutilities Goto Github PK

View Code? Open in Web Editor NEW

This project forked from appfoundry/dryutilities

0.0 2.0 0.0 121 KB

DRYUtilities provides common utilities used in projects by AppFoundry

License: MIT License

Ruby 2.43% Objective-C 97.57%

dryutilities's Introduction

DRYUtilities

CI Status Version License Platform

DRYUtilities is designed to offer some common functionality used in iOS development. It allows you to quickly get up and running without needing to write the same functionality over and over again on different projects. Using DRYUtilities will make your code even more DRY than it already is.

Usage

To run the example project, clone the repo, and run pod install from the Example directory first. This project contains example usages for most of the utilities provided in DRYUtilities.

Once you've started the app, you'll see a table containing a row for each example. Click on it to see the example in action and read about it below.

Configuration utilities

These utilities provide the functionality to easily gain access to properties read from different types of resource files.

Example usage: ExampleConfigurationViewController

DRYResourceReader

A DRYResourceReader exposes an operation to read a property from a resource based on a specified key. First of all you need to create a DRYResourceReader. DRYUtilities contains a DRYPlistResourceReader which can read .plist files. It also offers the ability to supply a fallback resource. This fallback resource will be used if the property can't be resolved using the original resource file.

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

Using the DRYBaseDynamicPropertyProvider you can create dynamic properties to retrieve the values from your resource files. Extend the DRYBaseDynamicPropertyProvider with properties matching the keys used in the resource file:

@interface ExampleDynamicPropertyProvider : DRYBaseDynamicPropertyProvider

@property (nonatomic, readonly) NSString *firstProperty;

@end

@implementation ExampleDynamicPropertyProvider 
@dynamic firstProperty;
@end

From now on you can target the properties from your resource file:

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

Foundation utilities

Foundation utilities provides utility methods on different Foundation classes.

NSString DRYHex

Example usage: ExampleDRYNSStringHexViewController

dryStringFromHexWithData: will return a hexadecimal string representing a NSData object.

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

Example usage: ExampleDRYNSStringTemplateReplacementViewController

dryStringByReplacingTemplatesWithValuesFromDictionary: can be used to replace placeholders in a string. It will return a new string containing the result after replacing the placeholders.

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

In order to use a different template notation you can use the following:

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 does what you think. It will return YES if the string isn't blank, it will return NO if the string is blank.

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

Example usage: ExampleDRYNSNullSupportViewController

dryValueOrNil can be used to convert an [NSNull null] value to nil. This can be useful for example when storing a NSDictionary with NSNull values to a Core Data object.

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

You can also test if a value is [NSNull null]. It wil return NO in case of nil or anything else besides [NSNull null]

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

[NSNull dryNullOrValue:] can be used to get the value or [NSNull null] if the value is nil.

UIKit utilities

UIColor DRYUtil

Example usage: ExampleDRYUIColorDRYUtilViewController

dryColorFromRGBHexString: provides the ability the create UIColor objects based on a RGB hex representation of a color.

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

UIImage ColorPixel

ColorPixel category provides the capability of crearing UIImage instances based on a single color. It will create a UIImage of 1px filled with the provided color.

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

There also the possibility to create a UIImage with a different size. However this consumes more resources, so be careful when using this approach.

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

UIView DRYLoadFromNib

Example usage: ExampleDRYLoadFromNibController

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

The method above is the most elaborate method provided. It can be used to create a UIView instance based on a .xib file. The nibNameis used to indicate which .xib file should be used. The .xib file can be located in another bundle, this can be set by the nibBundleOrNilargument. The tag argument indicates which view should be loaded based on the value of tag. If it is not provided it wille just return the first view in the .xib file which has the instancetype as class.

Security utilities

DRYSecurityCredentials represent a username and password pair that can be saved in secure storage through the DRYSecurityStoreWrapper. Instances of this class should not be kept in memory longer then absolutely necessary!

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

DRYSecurityStoreWrapper is a wrapper class providing access to a username/password pair identified with an identifier. It allows you to store this username/password pair using the Security library provided by iOS.

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

CoreData utilities

DRYCoreDataTableViewController provides a tableview controller backed by a NSFetchedResultsController. The controller will update its tableview when data changes.

Create an instance of DRYCoreDataTableViewController and add a NSFetchedResultsController to get started. You can also tell the controller whether to use section indexes or not. This can be done by setting the correct value for needsSectionIndex. If it is set to YES, the sectionIndexTitlesof the NSFetchedResultsController is used to display the section titles.

Requirements

Installation

DRYUtilities is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "DRYUtilities"

Author

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

License

DRYUtilities is available under the MIT license. See the LICENSE file for more info.

dryutilities's People

Contributors

mikeseghers avatar aceontech avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.