Arrays 如何从给定的file.txt文件中选择数组(列)?(stringByAppendingPathComponent)Xcode

Arrays 如何从给定的file.txt文件中选择数组(列)?(stringByAppendingPathComponent)Xcode,arrays,xcode,nsarray,Arrays,Xcode,Nsarray,我试图从包含数字数组的给定文件中选择一个数组(列),例如: 2012-09-02 16:27:15.010 SMA-0.818863 0.286575 0.206177-0.108286-0.231033-0.303317 2012-09-02 16:27:15.017 SMA-1.025597 0.380875 0.456131-0.282619-0.123060-0.048027 2012-09-02 16:27:15.023 SMA-0.754196 0.417053 1.165237-0

我试图从包含数字数组的给定文件中选择一个数组(列),例如:

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

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

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

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

这是我的代码,但它没有给出任何信息:

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)

怎么了?

我不确定您是否需要创建
strsineline
的行。以下是我的作品

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]);
    }
}];

记录
路径
数组
的值,以查看您使用的是什么。@PhillipMills这是一个好主意,谢谢,我更改了代码,现在我得到了“null,请参见上面