Skip to main content
←API Reference ←NSDK ←NSDKUtils

withNSDKStrings


Declaration​

static func withNSDKStrings<Result>(_ body: ((String?) -> ARDK_String) throws -> Result) rethrows -> Result

Summary​

Safely manages multiple C string conversions with automatic memory cleanup.
This function provides a convenient way to work with multiple C strings in a single scope
while ensuring proper memory management. It automatically allocates memory for C strings
and cleans up all allocated memory when the scope exits.
Use this function when you need to have multiple C strings in a single scope and
the number of nested scopes gets too messy. Otherwise, prefer Swift's built-in
String.withCString for simpler cases.

Returns​

The result of executing the body closure

Throws​

  • Any error thrown by the body closure

Example​

let result = NSDKUtils.withNSDKStrings { createString in
let string1 = createString("Hello")
let string2 = createString("World")
let string3 = createString(nil) // Creates empty string
// Use the NSDK strings with C API calls
return someCFunction(string1, string2, string3)
}
// Memory is automatically cleaned up here

Memory Management​

The function automatically:

  • Allocates memory for each string using strdup

  • Tracks all allocated pointers

  • Frees all memory when the scope exits (even if an error is thrown)

  • Handles nil and empty strings gracefully

    Parameters​

NameTypeSummary
body ((String?) -> ARDK_String) throws -> Result
A closure that receives a function for creating NSDK strings