Loading...
Searching...
No Matches
NCKeyValueStorage Class Reference

Interface for key-value storage operations, allowing storage and retrieval of various data types. More...

#include <com/navigine/idl/objc/NCKeyValueStorage.h>

Inherits NSObject.

Instance Methods

(BOOL) - contains:
 
(nonnull NSArray< NSString * > *) - getKeys
 
(int32_t) - getInt:defaultValue:
 
(int64_t) - getLong:defaultValue:
 
(BOOL) - getBool:defaultValue:
 
(float) - getFloat:defaultValue:
 
(double) - getDouble:defaultValue:
 
(nonnull NSString *) - getString:defaultValue:
 
(void) - putInt:value:
 
(void) - putLong:value:
 
(void) - putBool:value:
 
(void) - putFloat:value:
 
(void) - putDouble:value:
 
(void) - putString:value:
 
(void) - remove:
 
(void) - clear
 

Detailed Description

Interface for key-value storage operations, allowing storage and retrieval of various data types.

Referenced from StorageManager.

Definition at line 17 of file NCKeyValueStorage.h.

Method Documentation

◆ clear

- (void) clear

Removes all entries from the storage.

Swift code snippet:

// Clear all data
userStorage.clear()
print("Cleared all data from user storage")

Objective C code snippet:

// Clear all data
[self.userStorage clear];
NSLog(@"Cleared all data from user storage");

◆ contains:

- (BOOL) contains: (nonnull NSString *) key

Checks whether the key exists in the storage.

Parameters
keyKey to check.
Returns
true if a value is stored under the key (regardless of type).

Swift code snippet:

// Check if keys exist
let hasUserName: Bool = userStorage.contains(key: "user_name")
let hasNonExistent: Bool = userStorage.contains(key: "non_existent_key")
print("Contains 'user_name': \‍(hasUserName)")
print("Contains 'non_existent_key': \‍(hasNonExistent)")

Objective C code snippet:

// Check if keys exist
BOOL hasUserName = [self.userStorage contains:@"user_name"];
BOOL hasNonExistent = [self.userStorage contains:@"non_existent_key"];
NSLog(@"Contains 'user_name': %@", hasUserName ? @"YES" : @"NO");
NSLog(@"Contains 'non_existent_key': %@", hasNonExistent ? @"YES" : @"NO");

◆ getBool:defaultValue:

- (BOOL) getBool: (nonnull NSString *) key
defaultValue: (BOOL) defaultValue 

Gets a boolean value. Returns defaultValue if the key is missing or the stored type is not bool.

Parameters
keyLookup key.
defaultValueValue to return when not found or type mismatch.
Returns
Stored bool or defaultValue.

Swift code snippet:

// Retrieve boolean values with defaults
let isPremium: Bool = userStorage.getBool(key: "is_premium", defaultValue: false)
let notificationsEnabled: Bool = userStorage.getBool(key: "notifications_enabled", defaultValue: true)
let nonExistentBool: Bool = userStorage.getBool(key: "non_existent_bool", defaultValue: false)
print("Is premium: \‍(isPremium)")
print("Notifications enabled: \‍(notificationsEnabled)")
print("Non-existent bool: \‍(nonExistentBool)")

Objective C code snippet:

// Retrieve boolean values with defaults
BOOL isPremium = [self.userStorage getBool:@"is_premium" defaultValue:NO];
BOOL notificationsEnabled = [self.userStorage getBool:@"notifications_enabled" defaultValue:YES];
BOOL nonExistentBool = [self.userStorage getBool:@"non_existent_bool" defaultValue:NO];
NSLog(@"Is premium: %@", isPremium ? @"YES" : @"NO");
NSLog(@"Notifications enabled: %@", notificationsEnabled ? @"YES" : @"NO");
NSLog(@"Non-existent bool: %@", nonExistentBool ? @"YES" : @"NO");

◆ getDouble:defaultValue:

- (double) getDouble: (nonnull NSString *) key
defaultValue: (double) defaultValue 

Gets a 64-bit floating-point value. Returns defaultValue if the key is missing or the stored type is not double.

Parameters
keyLookup key.
defaultValueValue to return when not found or type mismatch.
Returns
Stored double or defaultValue.

Swift code snippet:

// Retrieve double values with defaults
let userLat: Double = userStorage.getDouble(key: "user_location_lat", defaultValue: 0.0)
let userLng: Double = userStorage.getDouble(key: "user_location_lng", defaultValue: 0.0)
let nonExistentDouble: Double = userStorage.getDouble(key: "non_existent_double", defaultValue: -1.0)
print("User location lat: \‍(userLat)")
print("User location lng: \‍(userLng)")
print("Non-existent double: \‍(nonExistentDouble)")

Objective C code snippet:

// Retrieve double values with defaults
double userLat = [self.userStorage getDouble:@"user_location_lat" defaultValue:0.0];
double userLng = [self.userStorage getDouble:@"user_location_lng" defaultValue:0.0];
double nonExistentDouble = [self.userStorage getDouble:@"non_existent_double" defaultValue:-1.0];
NSLog(@"User location lat: %f", userLat);
NSLog(@"User location lng: %f", userLng);
NSLog(@"Non-existent double: %f", nonExistentDouble);

◆ getFloat:defaultValue:

- (float) getFloat: (nonnull NSString *) key
defaultValue: (float) defaultValue 

Gets a 32-bit floating-point value. Returns defaultValue if the key is missing or the stored type is not float.

Parameters
keyLookup key.
defaultValueValue to return when not found or type mismatch.
Returns
Stored float or defaultValue.

Swift code snippet:

// Retrieve float values with defaults
let userRating: Float = userStorage.getFloat(key: "user_rating", defaultValue: 0.0)
let temperature: Float = userStorage.getFloat(key: "temperature", defaultValue: 0.0)
let nonExistentFloat: Float = userStorage.getFloat(key: "non_existent_float", defaultValue: -1.0)
print("User rating: \‍(userRating)")
print("Temperature: \‍(temperature)")
print("Non-existent float: \‍(nonExistentFloat)")

Objective C code snippet:

// Retrieve float values with defaults
float userRating = [self.userStorage getFloat:@"user_rating" defaultValue:0.0f];
float temperature = [self.userStorage getFloat:@"temperature" defaultValue:0.0f];
float nonExistentFloat = [self.userStorage getFloat:@"non_existent_float" defaultValue:-1.0f];
NSLog(@"User rating: %f", userRating);
NSLog(@"Temperature: %f", temperature);
NSLog(@"Non-existent float: %f", nonExistentFloat);

◆ getInt:defaultValue:

- (int32_t) getInt: (nonnull NSString *) key
defaultValue: (int32_t) defaultValue 

Gets a 32-bit integer value. Returns defaultValue if the key is missing or the stored type is not int.

Parameters
keyLookup key.
defaultValueValue to return when not found or type mismatch.
Returns
Stored int32 or defaultValue.

Swift code snippet:

// Retrieve integer values with defaults
let userAge: Int32 = userStorage.getInt(key: "user_age", defaultValue: 0)
let loginCount: Int32 = userStorage.getInt(key: "login_count", defaultValue: 0)
let nonExistentInt: Int32 = userStorage.getInt(key: "non_existent_int", defaultValue: -1)
print("User age: \‍(userAge)")
print("Login count: \‍(loginCount)")
print("Non-existent int: \‍(nonExistentInt)")

Objective C code snippet:

// Retrieve integer values with defaults
int32_t userAge = [self.userStorage getInt:@"user_age" defaultValue:0];
int32_t loginCount = [self.userStorage getInt:@"login_count" defaultValue:0];
int32_t nonExistentInt = [self.userStorage getInt:@"non_existent_int" defaultValue:-1];
NSLog(@"User age: %d", userAge);
NSLog(@"Login count: %d", loginCount);
NSLog(@"Non-existent int: %d", nonExistentInt);

◆ getKeys

- (nonnull NSArray< NSString * > *) getKeys

Returns all keys currently stored.

Returns
List of keys. Order is implementation-defined (not guaranteed).

Swift code snippet:

// Get all stored keys
let allKeys: [String] = userStorage.getKeys()
print("All stored keys: \‍(allKeys)")

Objective C code snippet:

// Get all stored keys
NSArray<NSString *> *allKeys = [self.userStorage getKeys];
NSLog(@"All stored keys: %@", allKeys);

◆ getLong:defaultValue:

- (int64_t) getLong: (nonnull NSString *) key
defaultValue: (int64_t) defaultValue 

Gets a 64-bit integer value. Returns defaultValue if the key is missing or the stored type is not long.

Parameters
keyLookup key.
defaultValueValue to return when not found or type mismatch.
Returns
Stored int64 or defaultValue.

Swift code snippet:

// Retrieve long values with defaults
let regTimestamp: Int64 = userStorage.getLong(key: "registration_timestamp", defaultValue: 0)
let lastLogin: Int64 = userStorage.getLong(key: "last_login_timestamp", defaultValue: 0)
let nonExistentLong: Int64 = userStorage.getLong(key: "non_existent_long", defaultValue: -1)
print("Registration timestamp: \‍(regTimestamp)")
print("Last login timestamp: \‍(lastLogin)")
print("Non-existent long: \‍(nonExistentLong)")

Objective C code snippet:

// Retrieve long values with defaults
int64_t regTimestamp = [self.userStorage getLong:@"registration_timestamp" defaultValue:0];
int64_t lastLogin = [self.userStorage getLong:@"last_login_timestamp" defaultValue:0];
int64_t nonExistentLong = [self.userStorage getLong:@"non_existent_long" defaultValue:-1];
NSLog(@"Registration timestamp: %lld", regTimestamp);
NSLog(@"Last login timestamp: %lld", lastLogin);
NSLog(@"Non-existent long: %lld", nonExistentLong);

◆ getString:defaultValue:

- (nonnull NSString *) getString: (nonnull NSString *) key
defaultValue: (nonnull NSString *) defaultValue 

Gets a string value. Returns defaultValue if the key is missing or the stored type is not string.

Parameters
keyLookup key.
defaultValueValue to return when not found or type mismatch.
Returns
Stored string or defaultValue.

Swift code snippet:

// Retrieve string values with defaults
let userName: String = userStorage.getString(key: "user_name", defaultValue: "Unknown")
let userEmail: String = userStorage.getString(key: "user_email", defaultValue: "")
let nonExistent: String = userStorage.getString(key: "non_existent_key", defaultValue: "default_value")
print("User name: \‍(userName)")
print("User email: \‍(userEmail)")
print("Non-existent key: \‍(nonExistent)")

Objective C code snippet:

// Retrieve string values with defaults
NSString *userName = [self.userStorage getString:@"user_name" defaultValue:@"Unknown"];
NSString *userEmail = [self.userStorage getString:@"user_email" defaultValue:@""];
NSString *nonExistent = [self.userStorage getString:@"non_existent_key" defaultValue:@"default_value"];
NSLog(@"User name: %@", userName);
NSLog(@"User email: %@", userEmail);
NSLog(@"Non-existent key: %@", nonExistent);

◆ putBool:value:

- (void) putBool: (nonnull NSString *) key
value: (BOOL) value 

Stores a boolean value under the key, replacing any existing value.

Parameters
keyKey to set.
valueValue to store.

Swift code snippet:

// Store boolean values
userStorage.putBool(key: "is_premium", value: true)
userStorage.putBool(key: "notifications_enabled", value: false)
print("Stored boolean values")

Objective C code snippet:

// Store boolean values
[self.userStorage putBool:@"is_premium" value:YES];
[self.userStorage putBool:@"notifications_enabled" value:NO];
NSLog(@"Stored boolean values");

◆ putDouble:value:

- (void) putDouble: (nonnull NSString *) key
value: (double) value 

Stores a 64-bit floating-point value under the key, replacing any existing value.

Parameters
keyKey to set.
valueValue to store.

Swift code snippet:

// Store double values
userStorage.putDouble(key: "user_location_lat", value: 55.7558)
userStorage.putDouble(key: "user_location_lng", value: 37.6176)
print("Stored double values")

Objective C code snippet:

// Store double values
[self.userStorage putDouble:@"user_location_lat" value:55.7558];
[self.userStorage putDouble:@"user_location_lng" value:37.6176];
NSLog(@"Stored double values");

◆ putFloat:value:

- (void) putFloat: (nonnull NSString *) key
value: (float) value 

Stores a 32-bit floating-point value under the key, replacing any existing value.

Parameters
keyKey to set.
valueValue to store.

Swift code snippet:

// Store float values
userStorage.putFloat(key: "user_rating", value: 4.5)
userStorage.putFloat(key: "temperature", value: 23.5)
print("Stored float values")

Objective C code snippet:

// Store float values
[self.userStorage putFloat:@"user_rating" value:4.5f];
[self.userStorage putFloat:@"temperature" value:23.5f];
NSLog(@"Stored float values");

◆ putInt:value:

- (void) putInt: (nonnull NSString *) key
value: (int32_t) value 

Stores a 32-bit integer value under the key, replacing any existing value.

Parameters
keyKey to set.
valueValue to store.

Swift code snippet:

// Store integer values
userStorage.putInt(key: "user_age", value: 25)
userStorage.putInt(key: "login_count", value: 42)
print("Stored integer values")

Objective C code snippet:

// Store integer values
[self.userStorage putInt:@"user_age" value:25];
[self.userStorage putInt:@"login_count" value:42];
NSLog(@"Stored integer values");

◆ putLong:value:

- (void) putLong: (nonnull NSString *) key
value: (int64_t) value 

Stores a 64-bit integer value under the key, replacing any existing value.

Parameters
keyKey to set.
valueValue to store.

Swift code snippet:

// Store long values
userStorage.putLong(key: "registration_timestamp", value: 1640995200000)
userStorage.putLong(key: "last_login_timestamp", value: Int64(Date().timeIntervalSince1970 * 1000))
print("Stored long values")

Objective C code snippet:

// Store long values
[self.userStorage putLong:@"registration_timestamp" value:1640995200000LL];
[self.userStorage putLong:@"last_login_timestamp" value:(long long)[[NSDate date] timeIntervalSince1970] * 1000];
NSLog(@"Stored long values");

◆ putString:value:

- (void) putString: (nonnull NSString *) key
value: (nonnull NSString *) value 

Stores a string value under the key, replacing any existing value.

Parameters
keyKey to set.
valueValue to store.

Swift code snippet:

// Store string values
userStorage.putString(key: "user_name", value: "John Doe")
userStorage.putString(key: "user_email", value: "john.doe@example.com")
print("Stored string values")

Objective C code snippet:

// Store string values
[self.userStorage putString:@"user_name" value:@"John Doe"];
[self.userStorage putString:@"user_email" value:@"john.doe@example.com"];
NSLog(@"Stored string values");

◆ remove:

- (void) remove: (nonnull NSString *) key

Removes a value by key. No-op if the key does not exist.

Parameters
keyKey to remove.

Swift code snippet:

// Remove specific keys
userStorage.remove(key: "user_age")
userStorage.remove(key: "non_existent_key") // No-op
print("Removed 'user_age' key")

Objective C code snippet:

// Remove specific keys
[self.userStorage remove:@"user_age"];
[self.userStorage remove:@"non_existent_key"]; // No-op
NSLog(@"Removed 'user_age' key");

The documentation for this class was generated from the following file: