1

我见过的最奇怪的事情.. NSLog 在基于奇怪事物的方法中失败了。这是代码:

-(void) testBoard {
BoardManager* bm = [BoardManager manager];
 Board* board = [bm boardForName:@"fourteen by eight"];
 NSLog(@"%@", board);
 NSLog(@"%@", board.coords);
    }

在 Board.m 中:

-(NSArray*) coords {
  if(!_coords.count && _definition) {
    NSArray* c = [Board decode:_definition];
    [self setCoords:c];
  }
  return _coords;
} 

    +(NSArray*) decode:(NSString*)encodedCoords {
 NSMutableArray* coords = [NSMutableArray array];
 NSArray* tokens = [encodedCoords componentsSeparatedByString:@","];
 int i = 0;
 NSString* dimStr = [tokens objectAtIndex:i++];
 int width = [dimStr substringToIndex:2].intValue;
 int height = [dimStr substringWithRange:NSMakeRange(2, 2)].intValue;
 int depth = [dimStr substringFromIndex:4].intValue;
 NSLog(@"w=%d h=%d d=%d", width, height, depth);

 NSString* b128;
 NSString* b2;

 for(int z=0; z<depth; z++) {
  for(int y=0; y<height; y++) {
   b128 = [tokens objectAtIndex:i++];
   NSLog(@"[%@]", b128);

   b2 = [Board base128to2:b128];
   NSLog(@"b2=%@",b2);

   for(int x=0; x<b2.length; x++) {
    if([b2 characterAtIndex:b2.length-1-x] == '1') {
     Coord* coord = [Coord x:width-1-x y:height-1-y z:z];
     [coords addObject:coord];
    }
   }
  }
 }
 return coords;
    }

现在发生的情况是,decode: 中的任何 NSLog 语句或从 decode: 调用的方法都不会记录到控制台。但是,调用 decode: 之前和之后的 NSLog 工作,并且 NSLog 周围的其余代码执行得很好。我发现我可以通过注释掉 [coords addObject:coord]; 来让所有 NSLog 语句正常工作。该语句出现它所影响的 NSLog 语句之后。我还发现我可以通过重新排列 testBoard 中的几行来让 NSLog 工作:像这样:

   -(void) testBoard
    {
 BoardManager* bm = [BoardManager manager];
 Board* board = [bm boardForName:@"fourteen by eight"];
 NSLog(@"%@", board);

 NSString* def = board.definition;
 NSArray* coords = [Board decode:def];

 NSLog(@"%@", coords);
    }

这看起来很奇怪..一个 xcode 尼斯湖水怪浮出水面?!

4

2 回答 2

0

你试过在调试器中运行吗?当你得到那个 if 语句时会发生什么

 if(!_coords.count && _definition)

你确定 _coords.count 是 0 并且 _definition 不是 nil

于 2010-11-09T09:10:26.193 回答
0

根据我的经验,NSLog 宏扩展可能会失败。大多数时候很明显为什么,有时不是。只需执行以下操作:

id objToLog = /* whatever */;
NSLog(@"%@", objToLog);

如果这种方法有效,您可以继续您的开发,也许您稍后会解决问题。

于 2010-11-09T11:54:26.293 回答