XCode:检查分割字符串值并将其分配给文本字段

发布于 2024-12-07 13:29:36 字数 861 浏览 1 评论 0原文

所以我一直在尝试对此进行测试;基本上我有一个名为 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++;
        }
    }

So i have been trying to test this out; basically i have a text file included named rawData.txt, it looks like this:

    060315512 Name Lastname
    050273616 Name LastName

i wanted to split the lines and then split each individual line and check the first part (with 9 digits) but it seems to not work at all (my window closes) is there any problem with this code?

    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++;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

香草可樂 2024-12-14 13:29:36

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

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

你应该写:

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

Yes if you want to compare 2 string you should use isEqualToString, because == compares the pointer value of the variables. So this is wrong:

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

You should write:

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

百变从容 2024-12-14 13:29:36

如果您检查控制台日志,您可能会看到类似“stringValue 发送到确实响应的对象(NSString)”(或那些效果)的内容。 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];
        }
    }
}

If you check your console log, you probably see something like "stringValue sent to object (NSString) that does respond" (or those effects). line is an array of strings, so [[line objectAtIndex:0] stringValue] is trying to call -[NSString stringValue] which does not exist.

You mean something more like this:

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];
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文