// First program example#import <Foundation/Foundation.h>int main (int argc, const char * argv[]) { @autoreleasepool { NSLog (@"Programming is fun!"); } return 0; }
Before explaining this program, we need to describe the steps for compiling and running. You can use xcode to compile and run the program, or you can use the OC compilation command on the terminal to compile and run the program. This section only describes how to use xcode to compile and run programs.
//////////////////////////////////////// //////////////////////////////////////// ////////////////
Note: xcode can be downloaded for free in app store. Or you can log on to the https://developer.apple.com where you can download to the latest version of xcode development tools and iOS SDK for free.
//////////////////////////////////////// //////////////////////////////////////// ////////////////
Xcode is a complex application that allows you to edit, compile, debug, and execute programs. If you want to develop some applications on Mac, you can learn how to use this powerful tool.
Start xcode (open for the first time, you may need to agree to the specific Protocol before using it). You can create a new xcode project on the startup interface.
Create a project and select the application type
On the left side, you will see the OS X column, select Application, select command line tool on the right side, and click Next.
Enter the application name, organization, and enterprise ID. Bundle identifier is used to create IOS apps, so we don't have to worry too much about what to fill in here. Select foundation for "type" and click "Next. Select the Directory and path for saving the project. Click Create to create a project.
Next, write the program. Select Main. m in the left-side pane.
The OC source file uses. m as its file extension. The meanings of other commonly used file extensions are as follows:
Extension |
Description |
. C |
C source file |
. CC. cpp |
C ++ source file |
. H |
Header file |
. M |
OC source file |
. Mm |
OC ++ source file |
. Pl |
Perl source file |
. O |
Compiled object files |
The content of Main. m is displayed on the right. This is a template automatically generated by xcode. The content is as follows:
//// main.m// Demo//// Created by Winson on 7/5/14.// Copyright (c) 2014 Winson. All rights reserved.//#import <Foundation/Foundation.h>int main(int argc, const char * argv[]){ @autoreleasepool { // insert code here... NSLog(@"Hello, World!"); } return 0;}
Next, compile and run the program. Before running, open the debug window of xcode.
The red rectangle area is the debugging window. Then, press the keys in the upper-left corner of xcode to compile and run the program.
//////////////////////////////////////// //////////////////////////////////////// ////////////////
If this is the first time you run xcode and run the program, a prompt box will pop up asking you to activate your Mac in development mode. Click Enable and enter the administrator password.
//////////////////////////////////////// //////////////////////////////////////// ////////////////
If your program has an error, xcode will display a red stop symbol next to the wrong code line, indicating that the program has a fatal error here and the program cannot run.
A yellow triangle is a warning identifier that appears next to a specified line of code. The program can run normally, but in general, this part of code must be detected and corrected.
Normal program running status: