今天升级到Xcode 8 GM后,我注意到NSLog在控制台上没有打印完整的日志信息。当使用下载大量信息的API(例如REST API从数据库下载所有产品)时,尤其明显,它只显示第一个产品的前30个键,其余信息被截断了...
我正在打印数组和字典,如果这有任何区别。
NSDictionary *allProducts = responseFromAPI;
NSLog(@"All products:%@", allProducts);
有其他人也注意到这个问题了吗? 有人知道如何修复吗?
今天升级到Xcode 8 GM后,我注意到NSLog在控制台上没有打印完整的日志信息。当使用下载大量信息的API(例如REST API从数据库下载所有产品)时,尤其明显,它只显示第一个产品的前30个键,其余信息被截断了...
我正在打印数组和字典,如果这有任何区别。
NSDictionary *allProducts = responseFromAPI;
NSLog(@"All products:%@", allProducts);
有其他人也注意到这个问题了吗? 有人知道如何修复吗?
正如@Lion在他的评论中所描述的那样,最简单的方法是使用printf。它的工作方式不完全像NSLog,但可以显示你想要的内容。
NSDictionary *allProducts = responseFromAPI;
NSString * string = [NSString stringWithFormat: @"%@", allProducts];
printf("%s", [string UTF8String]);
或更简短:NSDictionary *allProducts = responseFromAPI;
printf("%s", [NSString stringWithFormat: @"%@", allProducts].UTF8String);
一个技巧是在printf格式的开头或结尾放置"\n",这样它将分隔输出而不是将所有内容放在一行。类似这样:
printf("%s\n", string.UTF8String);
如果你不喜欢每次都写 printf,可以使用 #define 将代码重定向到像 printf 一样的函数(来自 @xfdai 的代码):
#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
希望这个是只是漏洞来自<苹果公司>那将会<被修正<尽快,在那之前我们可以使用这个。NSLOG
。如果字符串少于800,则使用简单的NSLog
。这对于Json长字符串并使用控制台非常有用。printf
使用Xcode调试窗口而不是控制台。-(void) JSLog:(NSString*)logString{
int stepLog = 800;
NSInteger strLen = [@([logString length]) integerValue];
NSInteger countInt = strLen / stepLog;
if (strLen > stepLog) {
for (int i=1; i <= countInt; i++) {
NSString *character = [logString substringWithRange:NSMakeRange((i*stepLog)-stepLog, stepLog)];
NSLog(@"%@", character);
}
NSString *character = [logString substringWithRange:NSMakeRange((countInt*stepLog), strLen-(countInt*stepLog))];
NSLog(@"%@", character);
} else {
NSLog(@"%@", logString);
}
}