为什么这段代码会出现这种奇怪的行为?目标-c

发布于 2024-10-07 10:30:34 字数 1214 浏览 8 评论 0原文

我有一个解析小文本文件的方法(下面的代码是简化版本):

- (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 技术交流群。

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

发布评论

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

评论(1

赏烟花じ飞满天 2024-10-14 10:30:34

您提供的代码有很多问题。我使用答案框是因为没有足够的空间来对此进行评论:

关于您的变量声明:

NSDate *date;
NSNumber *number;
NSString *desc;

您已正确声明它们,但尚未初始化它们。事实上,它们可能指向任何随机垃圾。这意味着您在循环末尾的测试......

if (!date && !number && !desc) {
   ...do something...
}

实际上可能总是执行,因为 datenumberdesc 可能始终是非-零(我说可能因为它们实际上是未定义的,无论它们是零还是非零)。如果您打算确定它们是否已设置,请将每个变量初始化为 nil:

NSDate *date = nil;
NSNumber *number = nil;
NSString *desc = nil;

并不总是需要初始化变量(例如,只要在读取之前对其进行写入,没有必要初始化它),但是有些人提倡初始化所有变量的想法,以防止出现这种未定义的行为(我通常会初始化所有变量,即使我无论如何都会覆盖初始化值)。

另外,还有一个名为 linesubstring 的变量,但它没有在代码中的任何地方声明,同样,strDatestrValue 也没有在任何地方声明。重要的是要知道如何声明它们以及如何使用它们,因为它们可能同样指向垃圾。

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:

NSDate *date;
NSNumber *number;
NSString *desc;

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…

if (!date && !number && !desc) {
   ...do something...
}

…may in fact always execute because date, number and desc may always be non-zero (I say may because it is actually undefined whether they are zero or non-zero). Initialise each of them to nil if you plan to determine whether they are set or not:

NSDate *date = nil;
NSNumber *number = nil;
NSString *desc = nil;

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, similarly strDate, 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.

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