joan joan - 1 year ago 123
Objective-C Question

Entry point of an app in Objective-C

I started a new iOS project in Xcode. File-new-SingleViewApp

Then I selected the following files

enter image description here

and moved to the trash.
Then I removed the following keys from the Info.plist file:
enter image description here

Then I added a C file named main.c
The content of the main.c file is:

int main(int argc, char * argv[]) {


And surpirsingly this works, the app compiles and run and writes "hello" to console. My question is: Was not main.m (and not main.c) supossed to be the entry point of an app?

Answer Source

There is no rule about the filename or its suffix: only about the function name main. Your file can be written in C or Objective-C, so its suffix can be .c or .m. An Objective-C program, after all, is a C program; Objective-C is C.

However, notice that your app is now unable to use Cocoa for anything. It is, in effect, not an app. It never calls UIApplicationMain. It just runs its one line and quits. So it's kind of silly.

A more productive way to get started on what you're doing would be to use the macOS Command Line Tool template. Observe that you can choose to write it in C! What you have constructed is very like a C command-line tool (except that there are no iOS command-line tools).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download