【问题标题】:XCode: Checking and assigning split string values to textFieldXCode:检查并将拆分字符串值分配给 textField
【发布时间】:2011-10-02 22:03:24
【问题描述】:

所以我一直在尝试对此进行测试;基本上我有一个名为 rawData.txt 的文本文件,它看起来像这样:

    060315512 Name Lastname
    050273616 Name LastName

我想拆分行,然后拆分每个单独的行并检查第一部分(9 位数字),但它似乎根本不起作用(我的窗口关闭)这段代码有什么问题吗?

    NSString *path = [[NSBundle mainBundle] pathForResource: @"rawData" ofType:@"txt"];
    if (path)
    {
        NSString *textFile = [NSString stringWithContentsOfFile:path]; 
        NSArray *lines = [textFile componentsSeparatedByString:(@"\n")];
        NSArray *line;
        int i = 0;
        while (i < [lines count])
        {
            line = [[lines objectAtIndex:i] componentsSeparatedByString:(@" ")];
            if ([[line objectAtIndex:0] stringValue] == @"060315512")
            {
                idText.text = [[line objectAtIndex: 0] stringValue];    
            }
            i++;
        }
    }

【问题讨论】:

    标签: xcode split textfield assign


    【解决方案1】:

    是的,如果你想比较两个字符串,你应该使用 isEqualToString,因为 == 比较变量的指针值。所以这是错误的:

    if ([[line objectAtIndex:0] stringValue] == @"060315512")

    你应该写:

    if ([[[line objectAtIndex:0] stringValue] isEqualToString:@"060315512"])

    【讨论】:

      【解决方案2】:

      如果你检查你的控制台日志,你可能会看到类似“stringValue sent to object (NSString) that does response”(或那些效果)。 line 是一个字符串数组,所以[[line objectAtIndex:0] stringValue] 试图调用不存在的-[NSString stringValue]

      你的意思更像是这样的:

      NSString *path = [[NSBundle mainBundle] pathForResource: @"rawData" ofType:@"txt"];
      if (path)
      {
          NSString *textFile = [NSString stringWithContentsOfFile:path]; 
          NSArray *lines = [textFile componentsSeparatedByString:@"\n"];
          for (NSString *line in lines)
          {
              NSArray *columns = [line componentsSeparatedByString:@" "];
              if ([[columns objectAtIndex:0] isEqualToString:@"060315512"])
              {
                  idText.text = [columns objectAtIndex:0];
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-20
        • 2013-05-09
        • 1970-01-01
        • 2011-07-13
        • 2019-05-04
        • 2023-03-09
        相关资源
        最近更新 更多