How to write defaults value from a file as bytes?

How to save content of a file into via defaults in such a way that they are handled as -data type?

Something like (just a naive idea, might not direct to the right approach):

% FILEDATA=... # read abc.xyz into a variable
% defaults write mydomain mykey -data "$FILEDATA" # save into user data

I. e., when I call defaults read on that key I get:

% defaults read mydomain mykey
{length = 400, bytes = 0xdcf2dbbb cb4343d7 ec4324ae 1234de18 ... 289f4567 ec4324ae }

Later in an obj-c app, the data are read using:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSData *data = [userDefaults dataForKey:@"mykey"];
Answered by DTS Engineer in 693424022

I’m not sure which bit of this is causing you problems so I did an end-to-end test.

When you write a -data value, the defaults command expect hex digits, so you set up FILEDATA like so:

% FILEDATA=`xxd -p test.txt`

The output from the read command is weird because defaults is relying on the description property and that changed a while back. I filed a bug about that at the time (r. 58531869) but it’s not resolved )-:

When I tweaked your app’s code to print the debugDescription property:

NSLog(@"%@", data.debugDescription);

I see the old school NSData format:

2021-11-01 11:52:20.551377+0000 Test693520[13739:1160063] <48656c6c 6f204372 75656c20 576f726c 64210a>

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

I’m not sure which bit of this is causing you problems so I did an end-to-end test.

When you write a -data value, the defaults command expect hex digits, so you set up FILEDATA like so:

% FILEDATA=`xxd -p test.txt`

The output from the read command is weird because defaults is relying on the description property and that changed a while back. I filed a bug about that at the time (r. 58531869) but it’s not resolved )-:

When I tweaked your app’s code to print the debugDescription property:

NSLog(@"%@", data.debugDescription);

I see the old school NSData format:

2021-11-01 11:52:20.551377+0000 Test693520[13739:1160063] <48656c6c 6f204372 75656c20 576f726c 64210a>

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

How to write defaults value from a file as bytes?
 
 
Q