Object-c Study Notes (1)

Source: Internet
Author: User

I want to do iOS development. I learned for a while before training. So I should take a new note to record my learning experience.

Objective-C is an extension set based on the C language. Of course, you must first familiarize yourself with the C language and then study objective-C. I learned it based on objective-C basics. I am a beginner. If you have different opinions or are wrong, please correct me and make a smile.


Construct an objective-C program


We need to download xcode as the Basic Editor. After downloading and installing xcode, we will create a new xcode project. Then we will select



Select Foundation in type, select next, and save the project by step.


Main. M contains the source code of the program.


Hello World Program:

In C language, we can output the hello World sentence like this.

#include <stdio.h>int main () {   printf ("Hello World !");   return 0;}

You can enter the following in main:

#import <Foundation/Foundation.h>int main (int argc,const char *argv[]) {   NSLog (@"Hello World !");   return 0;}
Click Run in the upper left corner to display the running result below,


Code Analysis:

Xcode uses the. m extension to indicate that the object uses objective-C code, which should be processed by the object-C compiler. If it is based on the C language, it is not difficult to understand that it ends with the declaration statement of the main () function and the Return Statement. Here

#import <Foundation/Foundation.h>

The statement tells the compiler to find the header file of foundation. h In the foundation framework.

So what is the framework? The so-called framework is a collection of header files, libraries, images, sounds, and other content in an independent unit. foundation is the content in the cocoa toolkit, it mainly deals with the features of the layer under the user interface, such as the data structure and communication mechanism.

The actual code of the only object-C statement we use is the nslog () function.

NSLog(@"Hello World !");

This line of code outputs Hello World to the console! Printf () statements are commonly used in C language. Nslog () accepts a string as its first parameter, which can contain format specifiers such as % d. This function accepts other parameters that match the format specifier. Of course, objective-C is an extension set of C language. You can still use the printf () function for output during output (nslog is recommended ).

@ What is the symbol?

@ Symbol is one of the features added by objective-c Based on C language. @ Indicates that the string in the quotation marks should be processed as the nsstring element of cocoa.

What is nsstring?

Nsstring is a string in cocoa. Nsstring support: String Length, string comparison, integer or floating point value conversion, etc.

What does NS mean?

Cocoa adds the NS prefix to all functions, constants, variables, and type names. This prefix tells us that this function is from cocoa rather than other toolkit.

In addition, for example, nsarray stores arrays and nsdateformatter sets different time formats. nsthread provides multithreading and nsspeechsynthesizer can hear speech.


Boolean Type:

In C, Boolean values include true and false, while objective-C values include yes and no.

The following is a simple example:

# Import <Foundation/Foundation. h>


BOOL areIntsDifferent (int thing1,int thing2) {   if (thing1== thing2) {      return (NO);   } else {      return (YES);   }}NSString *boolString (BOOL yesNo) {   if (yesNO == NO) {      return (@"NO");   } else {      return (@"YES");   }}int main (int argc, const char *argv[]) {   BOOL areTheyDifferent;   areTheyDifferent = areIntsDifferent (5,5);   NSLog(@"are %d and %d different ? %@",5,5,boolString(areTheyDifferent));   areTheyDifferent = areIntsDifferent (23,42);   NSLog(@"are %d and %d different ? %@",23,42,boolString(areTheyDifferent));   return (0);}

Code Analysis:

The first is the areintsdifferent () function,

BOOL areIntsDifferent (int thing1,int thing2) {   if (thing1== thing2) {      return (NO);   } else {      return (YES);   }}
Accept two Integer Parameters, return a bool value, compare according to thing1 and thing2, if the same return no, if different return YES

The second function is,

NSString *boolString (BOOL yesNo) {   if (yesNO == NO) {      return (@"NO");   } else {      return (@"YES");   }}

The main function of this function is to convert the bool value to an obvious string. The return value of boolstring () is a pointer to nsstring, which means that the function returns a cocoa string.

Main () function

The aretheydifferent variable saves the YES or NO returned by areintsdifferent.

What does "% @" in nslog mean?

A pointer is returned in nsstring (). In nslog, the % @ format specifier is written to notify nslog to accept appropriate parameters and use it as nsstring, and send it to the console using characters in the string.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.