NSError *error;
NSString *textFileContents = [NSString
stringWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:@”myTextFile”
ofType:@”txt”]
encoding:NSUTF8StringEncoding
error: & error];
// If there are no results, something went wrong
if (textFileContents == nil) {
// an error occurred
NSLog(@”Error reading text file. %@”, [error localizedFailureReason]);
}
NSArray *lines = [textFileContents componentsSeparatedByString:@” ”];
NSLog(@”Number of lines in the file:%d”, [lines count] );
本文介绍了一种使用Objective-C从应用程序资源中加载特定文本文件的方法,并解析文件内容为字符串数组的过程。文章通过示例代码展示了如何处理文件读取错误及如何将文件内容按行分割。
1万+

被折叠的 条评论
为什么被折叠?



