Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.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/5/objective-c/23.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_Objective C_Xcode - Fatal编程技术网

Ios 分开一根线

Ios 分开一根线,ios,objective-c,xcode,Ios,Objective C,Xcode,我如何使它能够将从文件中读取的字符串拆分为单独的字符串,或者使它只读取某一行,就像我希望它只读取文本文件的第一行并将其存储为字符串一样。下面是我用来读取文本文件的代码 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0];

我如何使它能够将从文件中读取的字符串拆分为单独的字符串,或者使它只读取某一行,就像我希望它只读取文本文件的第一行并将其存储为字符串一样。下面是我用来读取文本文件的代码

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pday11 = [documentsDirectory stringByAppendingPathComponent:@"day11.txt"];
    NSString *stringFromFileday11 = [NSString stringWithContentsOfFile:pday11 encoding:NSUTF8StringEncoding error:nil];
//If i used this to read the file how could i split up the "stringFromFileday11" into different strings where every line is a string from the file

读一个字符串并将其拆分

NSString *str = [NSString stringWithContentsOfFile:<YOUR FILEPATH> encoding:NSUTF8StringEncoding error:nil];
NSArray *array = [str componentsSeparatedByString:@"\n"];

你为什么要把这些字符串分开?最简单的解决方案是将它们合并为一个。为什么不合适呢?你真的想在很长一段时间内附加到文件中吗?我解决了问题的第一部分。我需要拆分字符串,因为它的部分用于if station中,可能与我如何将数组拆分为单独的字符串重复。你可以使用for循环,也可以使用while循环。编辑:添加示例。我希望这能有所帮助。@user2259486:您的字符串实际上是由换行符分隔的吗?对不起,没有错,我以为由字符串分隔的组件是在新行上分隔的。非常感谢,它解决了这么多问题!
NSString *firstline = [array objectAtIndex:0];