Latest 0.1.0
Homepage https://github.com/johnmckerrell/NSString-KeyPathReplace
License MIT
Dependencies RegexKitLite
Authors

NSString+KeyPathReplace

A simple NSString cateogory that gives you the ability to interpolate values
into a string taken from a given NSDictionary. It also allows you to specify
default values to be used if the dictionary does not contain a value for
a keypath. Default values can be provided inline with the keypath or in a
separate dictionary.

Note that keypaths don’t support arrays so everything must be structured as
a dictionary (or at least anything that you reference with a keypath).

Examples

Simple Example

    NSDictionary *dict = @{
                           @"value": @"World"
                           };
    NSString *result = [@"Hello {value}!" stringByReplacingKeyPathBindingsWithDictionaryValues:dict];
    // result should now contain "Hello World!"

Example with a hierarchy

    NSDictionary *dict = @{
                           @"planet": @"World",
                           @"person": @{
                                   @"firstname": @"Joe",
                                   @"lastname": @"Bloggs"
                                   }
                           };
    NSString *result = [@"Hello {planet}! Welcome {person.firstname} {person.lastname}." stringByReplacingKeyPathBindingsWithDictionaryValues:dict];
    // result should now contain "Hello World! Welcome Joe Bloggs."

Example with defaults

    NSDictionary *dict = nil;
    NSDictionary *defaultValues = @{
                                    @"value": @"World"
                                    };
    NSString *result = [@"Hello {value}! Welcome {person.name:friend}." stringByReplacingKeyPathBindingsWithDictionaryValues:dict
                                                                                 defaultValues:defaultValues];
    // result should now contain "Hello World! Welcome friend."

Latest podspec

{
    "name": "NSString-KeyPathReplace",
    "version": "0.1.0",
    "summary": "Simple Objective C NSString category for replacing key path references with values from a dictionary.",
    "description": "  A simple NSString cateogory that gives you the ability to interpolate valuesn  into a string taken from a given NSDictionary. It also allows you to specifyn  default values to be used if the dictionary does not contain a value for an  keypath. Default values can be provided inline with the keypath or in an  separate dictionary.n",
    "homepage": "https://github.com/johnmckerrell/NSString-KeyPathReplace",
    "license": "MIT",
    "authors": {
        "John McKerrell": "[email protected]"
    },
    "source": {
        "git": "https://github.com/johnmckerrell/NSString-KeyPathReplace.git",
        "tag": "0.1.0"
    },
    "source_files": "NSString-KeyPathReplace/*",
    "dependencies": {
        "RegexKitLite": [
            "~> 4.0"
        ]
    }
}

Pin It on Pinterest

Share This