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/0/search/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
File 如何从文档目录中读取文件_File_Search_Ios Simulator - Fatal编程技术网

File 如何从文档目录中读取文件

File 如何从文档目录中读取文件,file,search,ios-simulator,File,Search,Ios Simulator,我必须从documents目录中读取名为myname.plist的文件…..我想将文件内容存储到string….类似的操作应该是: /* Find the documents directory. */ NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentsDirectory, NSUserDomainMask, YES/*expand tilde*/); NSAssert([paths count] &

我必须从documents目录中读取名为myname.plist的文件…..我想将文件内容存储到string….

类似的操作应该是:

/* Find the documents directory. */
NSArray *paths = NSSearchPathForDirectoriesInDomains(
        NSDocumentsDirectory, NSUserDomainMask, YES/*expand tilde*/);
NSAssert([paths count] > 0,
         @"%s: No user document directory found!", __func__);
NSString *documentsDir = [paths objectAtIndex:0U];

/* Build the file path. */
NSString *name = [@"myname" stringByAppendingPathExtension:"plist"];
NSString *path = [documentsDir stringByAppendingPathComponent:name];

/* Read in the file content. */
NSError *error = nil;
NSStringEncoding enc;
NSString *content = [NSString
                     stringWithContentsOfFile:path
                     usedEncoding:&enc
                     error:&error];
if (!content) {
    NSLog(@"%s: Error reading content of \"%@\": %@", __func__, path, error);
}

这样做应该可以:

/* Find the documents directory. */
NSArray *paths = NSSearchPathForDirectoriesInDomains(
        NSDocumentsDirectory, NSUserDomainMask, YES/*expand tilde*/);
NSAssert([paths count] > 0,
         @"%s: No user document directory found!", __func__);
NSString *documentsDir = [paths objectAtIndex:0U];

/* Build the file path. */
NSString *name = [@"myname" stringByAppendingPathExtension:"plist"];
NSString *path = [documentsDir stringByAppendingPathComponent:name];

/* Read in the file content. */
NSError *error = nil;
NSStringEncoding enc;
NSString *content = [NSString
                     stringWithContentsOfFile:path
                     usedEncoding:&enc
                     error:&error];
if (!content) {
    NSLog(@"%s: Error reading content of \"%@\": %@", __func__, path, error);
}