Tag Archives: Objective-C

Getting started with Objective-C?

If you want to get started with Objective-C, and you have some background in C/C++, read this neat post I randomly stumbled upon: "Why Objective-C is cool". It's a pretty nice description and hopefully removes a lot of WTFs from a newbie.

Then, read CocoaLab's free e-book "Become an Xcoder".

Finally, this is the longest route, but the one I went with: watch all 18 hours + extras of CS193P iPhone Application Development 2009 (and then go watch some of the 2010 lessons, too). It takes a long time to watch, but it's worth it.

Testing for presence of Apple platform in C/C++/ObjC code

Are we running on an Apple platform?
#ifdef __APPLE__
Prerequisite for other tests
#ifdef __APPLE__
// let Apple define 
// various TARGET_OS_ 
// constants
// not on Apple platform
#define TARGET_OS_MAC 0
Are we running on Mac OS X?
Are we running on an iOS device?
// updated on Oct13 2010, previous method was flawed. sorry everyone!

Fixing "warning: missing sentinel in function call"

I got it for the folowing line:

execl("/bin/sh", "sh", "-c", sessioncommand.c_str(), 0);

Quite easy to fix:

execl("/bin/sh", "sh", "-c", sessioncommand.c_str(), (char*)0);


Alright. Since I'm getting "lots" of hits on this, let's see if we can improve this post. (June 28th 2010, more examples added July 22nd 2010, style but not content updated April 19 2014)

Generally, you're missing a sentinel if you don't add a "NULL" in C/C++, or a "nil" in Objective-C. In above example in C, I presume the compiler did not notice the zero (a valid sentinel) and threw a warning. That's what this post was mostly about.

However, it could also happen if you really did forget to add the sentinel. What is the purpose of this "sentinel"? When iterating through a varlist**, the function needs to know where to stop. In printf() and scanf(), the format string specifies this number of arguments. Functions could also accept the number of arguments as one of the arguments. Third option is this — specifying a sentinel, such as NULL or nil, as something that will stop further iteration.

Examples of sentinels in C:

execl("/bin/sh", "sh", "-c", sessioncommand.c_str(), NULL);

and in Objective-C:

NSArray *names = [NSArray arrayWithObjects: @"Ivan", @"Ana", @"Marko", @"Petar", nil];

** Iterating through a varlist – which is how you iterate through arguments in variadic functions, those with variable number of arguments