为什么这段代码会出现这种奇怪的行为?目标-c
我有一个解析小文本文件的方法(下面的代码是简化版本):
- (void)parseFile:(NSString *)aFile
{
NSDate *date;
NSNumber *number;
NSString *desc;
NSString *txt = [NSString stringWithContentsOfFile:aFile encoding:NSUTF8StringEncoding error:nil];
for (NSString *line in [txt componentsSeparatedByString:@"\n"]) {
if ([linesubstring isEqual:@"mydate"]) {
date = [dateFormat dateFromString:strDate];
}
if ([linesubstring isEqual:@"mynumber"]) {
number = [numberFormat numberFromString:strValue];
}
if ([linesubstring isEqual:@"mydesc"]) {
desc = [line substringWithRange:NSMakeRange(0, 10)];
}
if (!date && !number && !desc) {
...do something...
}
}
}
第一个问题是 date
变量正在填充 aFile
参数的内容。当它通过第一个 if/check 时,它只假设它是正确的值。
那么为什么呢?我认为 date
可能是一个保留字并交换了它,但具有相同的行为。
第二个问题是最后一个 if (带有嵌套的 if )。调试代码,我可以看到 xcode 将其显示为“超出范围”,但是 !number
失败(xcode 认为它是有效的)...
我尝试了其他组合,例如 [number isNotEqualTo:[NSNull null]]
(这会引发错误 EXC_BAD_ACCESS),但没有成功。
请问有人可以给一些提示吗?我是可可/objective-c 的新手。我来自 java...
TIA,
鲍勃
I have a method (the code below is a simplified version) that parses small text files:
- (void)parseFile:(NSString *)aFile
{
NSDate *date;
NSNumber *number;
NSString *desc;
NSString *txt = [NSString stringWithContentsOfFile:aFile encoding:NSUTF8StringEncoding error:nil];
for (NSString *line in [txt componentsSeparatedByString:@"\n"]) {
if ([linesubstring isEqual:@"mydate"]) {
date = [dateFormat dateFromString:strDate];
}
if ([linesubstring isEqual:@"mynumber"]) {
number = [numberFormat numberFromString:strValue];
}
if ([linesubstring isEqual:@"mydesc"]) {
desc = [line substringWithRange:NSMakeRange(0, 10)];
}
if (!date && !number && !desc) {
...do something...
}
}
}
The first problem is that date
variable is being filled with the content of aFile
parameter. It only assumes it's correct value, when the passes through the fist if/check.
So why? I though that date
could be a reserved word and exchanged it, but with the same behavior.
The second problem is with the last if (with the nested ones). Debuging the code, i can see that xcode shows it as "out of scope", but !number
fails (xcode thinks that it's valid)...
I tried other combinations, like [number isNotEqualTo:[NSNull null]]
(this one throws an error EXC_BAD_ACCESS), without success.
Please, could anybody give some hints? I'm newbie with cocoa/objective-c. I'm coming from java...
TIA,
Bob
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您提供的代码有很多问题。我使用答案框是因为没有足够的空间来对此进行评论:
关于您的变量声明:
您已正确声明它们,但尚未初始化它们。事实上,它们可能指向任何随机垃圾。这意味着您在循环末尾的测试......
实际上可能总是执行,因为
date
、number
和desc
可能始终是非-零(我说可能因为它们实际上是未定义的,无论它们是零还是非零)。如果您打算确定它们是否已设置,请将每个变量初始化为 nil:并不总是需要初始化变量(例如,只要在读取之前对其进行写入,没有必要初始化它),但是有些人提倡初始化所有变量的想法,以防止出现这种未定义的行为(我通常会初始化所有变量,即使我无论如何都会覆盖初始化值)。
另外,还有一个名为
linesubstring
的变量,但它没有在代码中的任何地方声明,同样,strDate
、strValue
也没有在任何地方声明。重要的是要知道如何声明它们以及如何使用它们,因为它们可能同样指向垃圾。There's quite a few things wrong with the code you've provided. I'm using the answer box because there isn't enough room for this to be a comment:
With regards to your variable declarations:
You have correctly declared them, but you have not initialised them. As they are, they could be pointing to any random garbage. This means that your test at the end of the loop…
…may in fact always execute because
date
,number
anddesc
may always be non-zero (I say may because it is actually undefined whether they are zero or non-zero). Initialise each of them tonil
if you plan to determine whether they are set or not:It is not always necessary to initialise variables (for example, as long as you write to it before you read from it, it is not necessary to initialise it), however some people promote the idea of initialising all variables to prevent this undefined behaviour from surfacing (I typically initialise all variables even if I overwrite the initialised value anyway).
Also, there is a variable called
linesubstring
but it is not declared anywhere in the code, similarlystrDate
,strValue
are not declared anywhere either. It is important to know how these are declared and how these are used as they may similarly be pointing to garbage.