XCode:检查分割字符串值并将其分配给文本字段
所以我一直在尝试对此进行测试;基本上我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,如果你想比较 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"])
如果您检查控制台日志,您可能会看到类似“stringValue 发送到确实响应的对象(NSString)”(或那些效果)的内容。
line
是一个字符串数组,因此[[line objectAtIndex:0] stringValue]
正在尝试调用-[NSString stringValue]
,但它不会存在。你的意思更像是这样的:
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: