2010-11-09 76 views
1

我见过的最奇怪的事情.. NSLog在基于奇怪事物的方法中失败。下面是代码: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; 
    } 

现在是什么情况,没有解码中的NSLog语句:从所谓的解码或方法:将登录到控制台。但是,在调用decode:work之前和之后,NSLog的其余代码执行得很好。我发现我可以简单地通过注释[coords addObject:coord];来获得所有的NSLog语句。此声明在之后出现它正在影响的NSLog语句。我还发现,我能得到的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); 
    } 

这似乎相当bizarre..an Xcode中的尼斯湖水怪堆焊?

回答

0

您是否尝试过在调试器中运行?当你到那个if语句

if(!_coords.count && _definition) 

确定_coords.count为0,_definition不是零

0

以我的经验,会发生什么的NSLog宏扩展可能会失败。大部分时间很明显,为什么,有时不。 就做这样的事情:

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

如果这种方法有效,你可以用你的发展继续前进,也许你以后弄清楚问题出来了。