我想从调用方法的位置获得确切的行数。
我有一个方法,在命令中从不同的不同行称为多行。所以,我需要从Controller
中调用它。
我使用了Stack Overflow
中的以下代码,但这传递给我错误的行号:
NSString *sourceString = [[NSThread callStackSymbols] objectAtIndex:1];
// Example: 1 UIKit 0x00540c89 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1163
NSCharacterSet *separatorSet = [NSCharacterSet characterSetWithCharactersInString:@" -[]+?.,"];
NSMutableArray *array = [NSMutableArray arrayWithArray:[sourceString componentsSeparatedByCharactersInSet:separatorSet]];
[array removeObject:@""];
NSLog(@"Stack = %@", [array objectAtIndex:0]);
NSLog(@"Framework = %@", [array objectAtIndex:1]);
NSLog(@"Memory address = %@", [array objectAtIndex:2]);
NSLog(@"Class caller = %@", [array objectAtIndex:3]);
NSLog(@"Function caller = %@", [array objectAtIndex:4]);
NSLog(@"Line caller = %@", [array objectAtIndex:5]);
那么,如何从iOS中调用方法的位置获取确切的行数?
答案 0 :(得分:1)
NSLog((@"%s [Line %d] "), __PRETTY_FUNCTION__, __LINE__);