KeyNode is a powerful library for working with key-value pairs in Objective-C. It provides an easy-to-use API for storing, retrieving, and manipulating data using keys. With KeyNode, you can quickly and efficiently manage your data, whether it’s stored in memory or persisted to disk.
Installation
To install KeyNode in your project, simply add it to your Podfile
:
// In your Podfile pod 'KeyNode'
Then, run the following command:
pod install
Usage
Using KeyNode is straightforward. First, import the KeyNode header file:
#import <KeyNode/KeyNode.h>
Then, you can use KeyNode to manage your data:
Creating a Key-Value Pair
To create a key-value pair, use the KNNode
class in conjunction with a KNNodeKey
and a KNNodeValue
:
KNNodeKey *key = [[KNNodeKey alloc] initWithName:@"myKey"]; KNNodeValue *value = [[KNNodeValue alloc] initWithObject:@"myValue"]; KNNode *node = [[KNNode alloc] initWithKey:key value:value];
Alternatively, you can use the convenient factory method:
KNNode *node = [KNNode nodeWithKey:@"myKey" value:@"myValue"];
Storing and Retrieving Data
To store a key-value pair, use the +[KNNodeStorage storeNode:]
method:
[KNNodeStorage storeNode:node];
To retrieve a key-value pair, use the +[KNNodeStorage nodeForKey:]
method:
KNNode *storedNode = [KNNodeStorage nodeForKey:key];
Manipulating Data
You can update the value of a key-value pair using the setValue:
method:
[storedNode setValue:@"newValue"];
You can also remove a key-value pair using the removeNode:
method:
[KNNodeStorage removeNode:storedNode];
Checking for Existence
To check if a key-value pair exists, use the +[KNNodeStorage nodeExistsForKey:]
method:
BOOL exists = [KNNodeStorage nodeExistsForKey:key];
Example
Here’s an example that demonstrates basic usage of KeyNode:
#import <KeyNode/KeyNode.h> - (void)example { // Create a key-value pair KNNodeKey *key = [[KNNodeKey alloc] initWithName:@"myKey"]; KNNodeValue *value = [[KNNodeValue alloc] initWithObject:@"myValue"]; KNNode *node = [[KNNode alloc] initWithKey:key value:value]; // Store the pair [KNNodeStorage storeNode:node]; // Retrieve the pair KNNode *storedNode = [KNNodeStorage nodeForKey:key]; NSLog(@"%@", storedNode.value.object); // Update the value [storedNode setValue:@"newValue"]; // Check if the pair still exists BOOL exists = [KNNodeStorage nodeExistsForKey:key]; NSLog(@"%@", exists ? @"YES" : @"NO"); // Remove the pair [KNNodeStorage removeNode:storedNode]; // Check if the pair exists after removal exists = [KNNodeStorage nodeExistsForKey:key]; NSLog(@"%@", exists ? @"YES" : @"NO"); }