Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS如何在文件中存储NSDictionary或JSON';这是什么?_Ios_Objective C_Json_File_Xattr - Fatal编程技术网

iOS如何在文件中存储NSDictionary或JSON';这是什么?

iOS如何在文件中存储NSDictionary或JSON';这是什么?,ios,objective-c,json,file,xattr,Ios,Objective C,Json,File,Xattr,我正在使用setxattr命令查看iOS和Mac文件的扩展文件属性。据我所知,我可以在那里存储任意数据,最高可达128kb 如何像处理字典一样编写和读取扩展属性,而不是取消对字符串指针的引用? 到目前为止,我有一段代码试图设置单个属性 NSString* filepath = [MyValueObject filepath]; const char *systemPath = [filepath fileSystemRepresentation]; const char *name = "sp

我正在使用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个键值对),我会考虑:

  • 使用我的属性创建NSDictionary
  • 将字典转换为JSON字符串
  • 将字符串转换为字符指针
  • 将字符串写入扩展属性
  • 要读回属性,我需要读回字符串指针
  • 转换为NSString
  • 转换为JSON对象
  • 创建一个字典
  • 从字典中检索值

  • 这似乎是正确的方法吗是否有更简单的方法将元数据存储在扩展属性中?NSObject上可能有一个类别处理xattr的指针操作?

    将字典转换为二进制plist并写入,反之亦然:)

    写:

  • 创建dict
  • 制作二进制plist
  • 写下来
  • --

    阅读:

  • 读二进制plist
  • 用它编一本字典
  • 与其他帖子一起,我能够完成我想要的——编写/恢复字典

    #import "Note+ExtendedAttribute.h"
    #include <sys/xattr.h>
    
    
    @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;
    }
    
    #导入“Note+ExtendedAttribute.h”
    #包括
    @实现MyFile(ExtendedAttribute)
    -(NSString*)字典键
    {
    返回@“mydictionary”;
    }
    -(BOOL)WriteExtendedAtDistributed字典:(NSDictionary*)字典
    {
    n错误*错误;
    NSData*jsonData=[NSJSONSerialization dataWithJSONObject:dictionary
    选项:0
    错误:&错误];
    if(!jsonData){
    返回否;
    }
    NSString*jsonString=[[NSString alloc]initWithData:jsonData编码:NSUTF8StringEncoding];
    const char*filepath=[[self filepath]fileSystemRepresentation];
    const char*key=[[self dictionaryKey]UTF8String];
    常量字符*值=[jsonString UTF8String];
    int result=setxattr(文件路径、键、值、strlen(值)、0、0);
    如果(结果!=0)
    {
    返回否;
    }
    返回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;
    }
    
    -(NSMutableDictionary*)readExtendedAttributeDictionary
    {
    const char*attrName=[[self dictionaryKey]UTF8String];
    const char*filePath=[[self filePath]fileSystemRepresentation];
    //获取所需缓冲区的大小
    int bufferLength=getxattr(filePath,attrName,NULL,0,0,0);
    
    如果(BufferLength)但是为什么它需要json格式,您是否要将它发送回服务器。确切地说,当我尝试将文件转换为NSData时,扩展属性不会被发送过来,所以我只想获取字典并将其与post请求一起发送。