Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.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中为文件设置任意属性吗?_Ios_File - Fatal编程技术网

我可以在iOS中为文件设置任意属性吗?

我可以在iOS中为文件设置任意属性吗?,ios,file,Ios,File,我正在实现一些管理简单文件(如文本文件)的代码,我希望在文件中添加一些信息,如位置、标记等,而不在文本文件上写入额外的文本 我认为最简单的方法是在文件属性中添加额外的信息,但我不确定这是否可行 我一直在使用NSURL的setResourceValue:forKey:error:,但似乎只有NSURL的预定义键才真正写入文件的属性。因此: NSURL *fileURL; //suppose this points to something NSError *error; id resourceVa

我正在实现一些管理简单文件(如文本文件)的代码,我希望在文件中添加一些信息,如位置、标记等,而不在文本文件上写入额外的文本

我认为最简单的方法是在文件属性中添加额外的信息,但我不确定这是否可行

我一直在使用NSURL的
setResourceValue:forKey:error:
,但似乎只有NSURL的预定义键才真正写入文件的属性。因此:

NSURL *fileURL; //suppose this points to something
NSError *error;
id resourceValue;

[fileURL setResourceValue:@"recorded string" forKey:@"testingkey" error:&error];
[fileURL getResourceValue:&resourceValue forKey:@"testingkey" error:&error];
在上面的代码中,两个函数
setResourceValue:forKey:error:
getResourceValue:forKey:error:
返回
YES
表示它们成功,但
resourceValue
的最终值为零,这可能意味着实际上没有记录该值

那么,有没有办法在iOS中设置我自己的文件属性?如果没有,那么在不修改文件本身的情况下,将额外信息吸收到文件中的好(简单)方法是什么


非常感谢

这是对这个问题的补充:

本文详细分析了如何解决这一问题,即既不需要改变原始文件的内容,又需要在文件中写入一些信息

您可以这样使用:

#include <sys/xattr.h>

    if (!&NSURLIsExcludedFromBackupKey) {
        // iOS <= 5.0.1
        const char *filePath = [[URL path] fileSystemRepresentation];
        const char *name = "com.apple.MobileBackup";
        u_int8_t value = 1;
        int result = setxattr(filePath, name, &value, sizeof(value), 0, 0);
    } else {
        // iOS >= 5.1
        NSError *error = nil;
        [URL setResourceValue:@YES
                       forKey:NSURLIsExcludedFromBackupKey
                        error:&error];
    }
#包括
如果(!&NSURLISEXCLUDED FROM BACKUPKEY){
//iOS=5.1
n错误*错误=nil;
[URL setResourceValue:@是
forKey:nsurlisecludedfrombackupkey
错误:&错误];
}

这就是您所需要的:谢谢!这确实是我所需要的一切。@Marcelo设置后,我将从这段代码中获得输出。