Over the years I’ve travelled to China several times, and now I’m working with large group of developers in Suzhou, Hangzhou and Hefei. A few weeks ago I was able to visit again, and that’s gotten me back in the mood to study Chinese. It often helps me to write down things as I learn them, and some of my Chinese coworkers read this blog and might help set me straight as I wander through their language like a bull in a China shop (as it were….) It’s a bit off the trail for Cocoa development, so feel free to use the Subscribe2 link on the right to take this category off of your email subscription. Read more…
So say you had this code:
printf("%s", 1);
NSLog(@"%s", 1);
And you compiled with -Wformat. You might expect both of these lines to kick out a warning:
Format '%s' expects type 'char *', but argument 2 has type 'int'
You’d be particularly misled when you went and looked at the definition of NSLog():
FOUNDATION_EXPORT void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
Why look there, doesn’t that look like it should provide format type checking? Oh how foolish. Neither gcc nor clang can actually handle that __NSString__ specifier in a robust way. So the first line above will give a useful warning, but the second one will silently compile and later crash. Exciting, I know. You have been warned.
-Wformat-nonliteral and -Wformat-security do catch dangerous calls like NSLog(foo), so __NSString__ isn’t a complete loss, but it’s a shame we can’t get type checking here.
There’s a good discussion of this at NSLog(…) improper format specifier affects other variables?
Prashant P. wrote me recently asking some questions that I thought I’d answer here. First, can iPhone development be full time job, and second, will it help him get into a “mainstream job” in Java, .NET, etc. As he notes, most of the iPhone development he’s seen has been a part-time rather than full-time job; a side-line rather than a career. Read more…