Search code examples
iosnslogafnetworkingtestflight

Semantic issue: missing '[' at start of message send expression


I get a weird semantic issue:

missing '[' at start of message send expression

and a parse issue:

Expected ']'

in NSLog line of AFURLConnectionOperation.m:

 @catch(NSException *e) { caughtException = e; }
 if(caughtException) {
   NSLog(NSLocalizedString(@"Unhandled exception on %@ networking thread: %@, userInfo: %@", nil), NSStringFromClass([self class]), caughtException, [caughtException userInfo]); 
 }
 [exceptionPool drain];

after I added

#define NSLog(__FORMAT__, ...) TFLog((@"%s [Line %d] " __FORMAT__), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)

to my project's pre-compile file: Proj-Prefix.pch

How can I fix this error?
I searched but without any workaround except comment out the NSLog line..

Thanks in advance!


EDIT:

NSLog(@"%@", [NSString stringWithFormat:NSLocalizedString(@"Unhandled exception on %@ networking thread: %@, userInfo: %@", nil), NSStringFromClass([self class]), caughtException, [caughtException userInfo]]);

and

NSLog(@"Unhandled exception on %@ networking thread: %@, userInfo: %@", NSStringFromClass([self class]), caughtException, [caughtException userInfo]);

are okay.

But why the original one does not? :?


Solution

  • Think about the macro expansion. In your macro, you're trying to use string literal concatenation:

    (@"%s [Line %d] " __FORMAT__)
    

    But the value of the __FORMAT__ parameter is NSLocalizedString(@"Unhandled exception on %@ networking thread: %@, userInfo: %@", nil), which is not a string literal. The expansion looks like this:

    (@"%s [Line %d] " NSLocalizedString(@"Unhandled exception on %@ networking thread: %@, userInfo: %@", nil))
    

    Clearly that's erroneous syntax. The error is made extra-inscrutable because NSLocalizedString is itself a macro (defined in NSBundle.h), so the full expansion looks like this:

    (@"%s [Line %d] " [[NSBundle mainBundle] localizedStringForKey:(@"Unhandled exception on %@ networking thread: %@, userInfo: %@") value:@"" table:nil])
    

    By the way, you should not use __FORMAT__ as your macro parameter name. All identifiers that begin with two underscores are reserved. (Also reserved are all identifiers that begin with an underscore followed by a capital letter.)