如何选择从给定file.txt的阵列(列)? (stringByAppendingPathComponent)x code [英] how to chose an array( column) from a given file.txt? (stringByAppendingPathComponent) X-code

查看:135
本文介绍了如何选择从给定file.txt的阵列(列)? (stringByAppendingPathComponent)x code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从包含数字阵列给定文件挑一个数组(列),例如:

16 2012-08-30:27:15.010 SMA -0.818863 0.286575 0.206177 -0.108286 -0.231033 -0.303317

2012-09-02 16:27:15.010 SMA -0.818863 0.286575 0.206177 -0.108286 -0.231033 -0.303317

16 2012-08-30:27:15.017 SMA -1.024597 0.380875 0.456131 -0.282619 -0.123060 -0.048027

2012-09-02 16:27:15.017 SMA -1.024597 0.380875 0.456131 -0.282619 -0.123060 -0.048027

16 2012-08-30:27:15.023 SMA -0.754196 0.417053 1.165237 -0.010996 -0.078193 0.594972

2012-09-02 16:27:15.023 SMA -0.754196 0.417053 1.165237 -0.010996 -0.078193 0.594972

16 2012-08-30:27:15.030 SMA -0.674835 0.486038 1.487640 0.061529 -0.008288 0.825637

2012-09-02 16:27:15.030 SMA -0.674835 0.486038 1.487640 0.061529 -0.008288 0.825637

票数是我的code,但它没有给出:

hier is my code, but its giving nothing :

NSString* filePath = @"/Users/bahmadios/Documents/Xcode/selectArray/selectArray/Bus.txt";//file path...
NSString* fileRoot = [[NSBundle mainBundle]
                      pathForResource:filePath ofType:@"txt"];

// read everything from text
NSString* fileContents =
[NSString stringWithContentsOfFile:fileRoot
                          encoding:NSUTF8StringEncoding error:nil];

NSLog(@"%@", fileContents);

// first, separate by new line
NSArray* allLinedStrings =
[fileContents componentsSeparatedByCharactersInSet:
 [NSCharacterSet newlineCharacterSet]];
 NSLog(@"%@", allLinedStrings);
// then break down even further
NSString* strsInOneLine =
[allLinedStrings objectAtIndex:0];
NSLog(@"%@", strsInOneLine);

// choose whatever input identity you have decided. in this case ;

NSArray *array = [ strsInOneLine  componentsSeparatedByString:@" "];
if (array.count > 3) {
    NSLog(@"---- %@", array[3]);

和我得到:

2014-05-20 23:26:52.226 selectArray[5734:907] ---- (null)
2014-05-20 23:26:52.227 selectArray[5734:907] (null)
2014-05-20 23:26:52.228 selectArray[5734:907] (null)
2014-05-20 23:26:52.228 selectArray[5734:907] (null)
2014-05-20 23:26:52.229 selectArray[5734:907] ---- (null)

有人可以看到什么是错的?

can somebody see what is wrong ?

感谢

推荐答案

我不知道你需要创建行 strsInOneLine 。对我来说,以下工作

I'm not sure you need the line that creates strsInOneLine. The following works for me

NSString *filePath     = ...;
NSString *fileContents = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

NSArray *lines = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];

[lines enumerateObjectsUsingBlock:^(NSString *line, NSUInteger idx, BOOL *stop) {
  NSArray *components = [line componentsSeparatedByString:@" "];
    if (components.count > 3) {
      NSLog(@"---- %@", components[3]);
    }
}];

这篇关于如何选择从给定file.txt的阵列(列)? (stringByAppendingPathComponent)x code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆