sách gpt4 ai đã đi

iOS Làm thế nào để lưu trữ NSDictionary hoặc JSON trong xattr của một tệp?

In lại 作者:行者123 更新时间:2023-11-29 02:43:45 33 4
mua khóa gpt4 Nike

我正在使用 setxattr 命令查看 iOS 和 Mac 文件的扩展文件属性。据我了解,我可以在那里存储任意数据,最多 128kb。

如何像处理字典一样写入和读取扩展属性,而不是取消引用字符串指针?

到目前为止,我有这段尝试设置单个属性的代码。

NSString* filepath = [MyValueObject filepath]; 
const char *systemPath = [filepath fileSystemRepresentation];
const char *name = "special_value";
const char *value = "test string";

int result = setxattr(systemPath, name, &value, strlen(value), 0, 0);

如果我需要存储一小组值(例如 5 个键值对),我会考虑:

  1. 用我的属性创建一个 NSDictionary
  2. 将字典转换为 JSON 字符串
  3. 将字符串转换为字符指针
  4. 将字符串写入扩展属性
  5. 要读回属性,我会读回字符串指针
  6. 转换为 NSString
  7. 转换成JSON对象
  8. 创建一个字典回来
  9. 从字典中检索一个值

这看起来是正确的方法吗? 有没有更简单的方法将元数据存储在扩展属性中?也许 NSObject 上有一个类别可以处理 xattr 的指针操作?

1 Câu trả lời

I found a Cocoanetics/DTFoundation that allows reading/writing arbitrary strings to xattr:与其他帖子一起,我能够完成我想要的 - 编写/恢复字典

#import "Note+ExtendedAttribute.h"
#include


@implementation MyFile (ExtendedAttribute)

-(NSString*)dictionaryKey
{
return @"mydictionary";
}

-(BOOL)writeExtendedAttributeDictionary:(NSDictionary*)dictionary
{
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary
options:0
error:&error];
if (! jsonData) {
return NO;
}

NSString* jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];



const char *filepath = [[self filepath] fileSystemRepresentation];

const char *key = [[self dictionaryKey] UTF8String];
const char *value = [jsonString UTF8String];

int result = setxattr(filepath, key, value, strlen(value), 0, 0);

if(result != 0)
{
return NO;
}

return YES;
}

阅读:

-(NSMutableDictionary*)readExtendedAttributeDictionary
{
const char *attrName = [[self dictionaryKey] UTF8String];
const char *filePath = [[self filepath] fileSystemRepresentation];

// get size of needed buffer
int bufferLength = getxattr(filePath, attrName, NULL, 0, 0, 0);

if(bufferLength<=0)
{
return nil;
}

// make a buffer of sufficient length
char *buffer = malloc(bufferLength);

// now actually get the attribute string
getxattr(filePath, attrName, buffer, bufferLength, 0, 0);

// convert to NSString
NSString *retString = [[NSString alloc] initWithBytes:buffer length:bufferLength encoding:NSUTF8StringEncoding];

// release buffer
free(buffer);

NSData *data = [retString dataUsingEncoding:NSUTF8StringEncoding];
if(data == nil || data.length == 0)
{
return nil;
}

NSError *error = nil;
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

if([json isKindOfClass:[NSDictionary class]])
{
return [NSMutableDictionary dictionaryWithDictionary:json];
}

if(error)
{
return nil;
}


return json;
}

关于iOS 如何在文件的 xattr 中存储 NSDictionary 或 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25408028/

33 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com