我见过的最奇怪的事情.. 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 尼斯湖水怪浮出水面?!