Xcode 8 不显示整个 NSLog 输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39412917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode 8 Does not display the whole NSLog output
提问by Pointblaster
After upgrading to Xcode 8 GM today i noticed that NSLog isn't printing the whole log-message to the console. This is especially noticeable when working against an API that downloads a lot of information, like a REST API download all the products from a database, it only shows the first 30 keys on the first product, the rest of the information is clipped...
今天升级到 Xcode 8 GM 后,我注意到 NSLog 没有将整个日志消息打印到控制台。这在针对下载大量信息的 API 工作时尤其明显,例如 REST API 从数据库下载所有产品,它只显示第一个产品的前 30 个键,其余信息被剪辑......
I'm printing arrays and dictionaries, if that makes any difference.
我正在打印数组和字典,如果这有什么不同的话。
NSDictionary *allProducts = responseFromAPI;
NSLog(@"All products:%@", allProducts);
Have anyone else noticed this? And does anybody know how to fix this?
有没有其他人注意到这一点?有谁知道如何解决这个问题?
采纳答案by Pointblaster
As @Lion described in his comment the easiest possible way is to use printf instead. It does not work exactly like NSLog but it shows what you want.
正如@Lion 在他的评论中所描述的,最简单的方法是改用 printf。它不像 NSLog 那样工作,但它显示了你想要的。
NSDictionary *allProducts = responseFromAPI;
NSString * string = [NSString stringWithFormat: @"%@", allProducts];
printf("%s", [string UTF8String]);
or shorter:
或更短:
NSDictionary *allProducts = responseFromAPI;
printf("%s", [NSString stringWithFormat: @"%@", allProducts].UTF8String);
A tip is to place a "\n" at the beginning or end of the printf format so it will separate the outputs and not put all in a single line. Something like this:
一个提示是在 printf 格式的开头或结尾放置一个“\n”,这样它将把输出分开,而不是把所有的都放在一行中。像这样的东西:
printf("%s\n", string.UTF8String);
If you don't like writing printf instead every time you can use a #define to redirect the code to an printf like this (code from @xfdai):
如果您不喜欢每次使用 #define 将代码重定向到这样的 printf 时都编写 printf(来自@xfdai 的代码):
#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
Hopefully this is just a bug from Apple that will get fixed soon, until then we can use this.
希望这只是 Apple 的一个错误,很快就会得到修复,在那之前我们可以使用它。
回答by Andy Vene
You can use this method. Split every 800 chars. Or can be set. NSLOG
I think truncate every 1000 chars. If string is less than 800 will use a simple NSLog
. This is useful for Json long strings and uses the console. printf
uses Xcode debug window not the console.
您可以使用此方法。每 800 个字符拆分一次。或者可以设置。NSLOG
我认为每 1000 个字符截断一次。如果字符串小于 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);
}
}