Ad space (banner)
๐ŸŽObjective-C Lessons
Lesson 55 / 68

Receiving command-line arguments

This lesson covers receiving command-line arguments in Objective-C, with the aim of passing values in from outside at run time.

You receive command-line arguments through main's parameters, as in int main(int argc, const char * argv[]). The C-derived form is used unchanged in Objective-C.

The sample code checks that enough arguments arrived with argc > 2 and displays argv[1] as the name and argv[2] as the age. Note that they arrive as C-style strings (char *).

A common early stumble is the difference between a C string and an NSString. Command-line arguments arrive as C strings, so you convert with [NSString stringWithUTF8String:] when you need one.

In professional work, this extends to simple automation tools that branch on the values passed in at run time.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Objective-C directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)