How to Program Cocoa on an iPhone
The Cocoa language is a derivative of the C++ language you use to program for iPhone apps. You must learn the language to create iPhone apps and distribute them in the Apple App Store. To get started, you set up the "Hello World" app, which is the standard programming tutorial that gets you started with a new language and a new programming platform.
Instructions
-
-
1
Open the XCode app on your desktop and create a new, blank project. The XCode app creates a blank template with a main source code file and the project structure to start a new iPhone app.
-
2
Import the Cocoa headers. Copy and paste the following code to the top of the main source code file:
#import <Cocoa/Cocoa.h>
-
-
3
Set up the function that creates the app screen and sets up the object that displays your "Hello World" text. The following functions set up the screen, which prepares the app to display to the user:
@interface World:NSObject
{
}
-world;
@end@implementation World
- world
{
NSLog(@"Hello World!");
return 0;
}
@endAt this point, you only have a stored "Hello World" variable and a screen that you use to display the data. However, no data is displayed at this point. It only stores the data for future use in your app.
-
4
Call the "main" function to display the code to the user. The Cocoa's "main" function runs first in any application. You use the function to set up the basics of your app. In this example, you use it to display the content you set up in the "world" function. Add the following code to the source code file:
int main(int argc, char *argv[])
{
id hello;hello=[[World alloc] init];
[hello world];
return 0;
} -
5
Click the "Project" menu item and select "New Project." Select "Target" and select "Cocoa." Click "Finish" to set up the app and complete it for debugging.
-
6
Click "Run" to run the project in a debugger. The app runs and displays the "Hello World" text on your desktop, so you know there are no errors in the code.
-
1