首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSLog不工作

NSLog不工作
EN

Stack Overflow用户
提问于 2010-11-09 07:58:35
回答 2查看 1.7K关注 0票数 1

我见过的最奇怪的事..。NSLog在一种基于奇异事物的方法中失败了。以下是代码:

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

在Board.m:

代码语言:javascript
复制
-(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: work之前和之后,NSLog和NSLog周围的其余代码执行得很好。我发现我可以通过注释掉所有的NSLog语句,addObject:coord;。此语句出现在它正在影响的NSLog语句之后。我还发现,通过重新安排testBoard中的几行代码,我可以让NSLog工作:如下所示:

代码语言:javascript
复制
   -(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);
    }

这似乎是相当bizarre..an xcode尼斯湖怪物出现?!

EN

回答 2

Stack Overflow用户

发布于 2010-11-09 09:10:26

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

代码语言:javascript
复制
 if(!_coords.count && _definition)

你确定_coords.count是0而_definition不是零吗?

票数 0
EN

Stack Overflow用户

发布于 2010-11-09 11:54:26

根据我的经验,NSLog宏扩展可能失败。大多数情况下,原因是显而易见的,有时不是。就像这样做:

代码语言:javascript
复制
id objToLog = /* whatever */;
NSLog(@"%@", objToLog);

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4131509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档