Xcode 8 Does not display the whole NSLog output
Asked Answered
H

2

19

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...

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?

Hertahertberg answered 9/9, 2016 at 13:39 Comment(7)
I am having the same issue and I can't figure it out! I'm not printing dicts, this is mine - used to print entire string, now only prints out 1028 chars every time: NSLog(@"Data:%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);Tryma
Did this also start after upgrade to xCode 8?Hertahertberg
yup only started after xcode 8 GM. I didn't notice it in the last beta.Tryma
@Tryma & Pointblaster It may help you https://mcmap.net/q/668079/-ios10-nslog-is-limited-to-1024-chars-strings/39538500#39538500Balch
As the other question ^ offered, you can fake NSLog out with printf but that doesn’t help much in Swift (okay, one could write a bunch of code to bridge between swift and obj-c… but I’d rather a different solution)Dilapidation
Me too having the same issue, But I don't have a clue about how to solve this issue. I'm researching and will let u guys know on any updates.Caputto
Same here after upgrading Xcode.Kisner
H
13

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.

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("%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 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.

Hertahertberg answered 24/9, 2016 at 13:5 Comment(0)
E
0

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.

-(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);
        }

}
Eddings answered 18/9, 2017 at 15:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.