Objective-C syntax basics in iOS development

Source: Internet
Author: User

Based on the experience of many netizens and the experience of iOS development, the author summarizes the iOS development technology for later users to learn, I also hope that I can gain a deep understanding of iOS development.

1. Sources of objective-C and C

Objective-C was born in 1980s and was invented by Brad Cox to combine popular and portable C languages with elegant Smalltalk languages. Objective-C is an extension set of the C language. It is based on the C language and adds some subtle but significant features to the language.

Apple's iPhone platform uses objective-C for native language development. The objective-C kernel is C language and some features of OOP are implemented based on C language. Objective-C is an extension of C language, which is very similar to better-C, the predecessor of C ++, the new version of objective-c implements a garbage collection mechanism similar to Java. However, the iPhone platform does not support garbage collection based on iPhone platform resource restrictions.

 

Ii. first glance at objective-C

1. Composition of cocoa

Apple uses technologies such as cocoa, carbon, QuickTime, and OpenGL as a framework set to provide cocoa components: (1) foundation framework (there are many useful low-level data-oriented classes and data structures ); (2) Application Kit (also called appkit) framework (including all user interface objects and advanced classes, such as NS ......), There is also a suite supporting frameworks, including core animation and core image.

2. nslog is equivalent to printf ()

Nslog (@ "Hello objective-c ");

// Note: @ is one of the features added by objective-C based on the Standard C language. The double quotation mark string is preceded by @, which indicates that the referenced string should be processed as the nsstring element of cocoa.

Nslog (@ "are % d and % d different? % @ ", 5, 5, boolstring (aretheydifferent ));

// Note % @: This format is used when nslog is used to output any object value.

3. bool uses 8-bit storage. Yes is defined as 1. No is defined as 0. If it is greater than 1, yes is not set. It is different from standard C.

If an integer value longer than 1 byte is accidentally assigned to bool, only eight lower bits are intercepted.

In obejective-C, 1 is not equal to 1. Do not compare bool values with yes.

 

3. Weird objective-C syntax structure

The reason why I am talking about it here is that it is the most common feature of other programming languages (Java, C ++, and other languages:

1. minus sign (or plus sign)

Every day, we will see "+" or "-" before the method name in iOS development. What are these, so strange?

First, we need to discard the inertial thinking of learning object-oriented languages. In objective-C, there is no concept of public and private. You can think it is all public. It only has class methods and instance methods, the plus sign indicates the class method. The class method can be called directly without creating an instance of the class. The minus sign indicates the instance method. You must create an Instance Object of the class to call this method.

 

For example, in C #/Java, a method may be written as follows:

Private void Hello (bool ishello)

{

// Ooxx

}

 

Written in objective-C:

-(Void) Hello :( bool) ishello

{

// Ooxx

}

If you have programming experience, it is not difficult to understand this!

 

2. Brackets

Brackets can be thought of as how to call the method you just wrote. In objective-C, we usually say "message ".

For example, you can write in C # as follows:

This. Hello (true );

 

In objective-C, we need to write it:

[Self Hello: Yes];

 

 

 

3. Ns ****

In iOS development, objects starting with ns are often encountered. This should begin with the historical resentment of the helper. Steve Jobs and John scullery complained that they had been squeezed out of apple in the past and made a company calledNEXTSTEP, which is a complete set of development kits that some scientists like, And now Mac OS usesNEXTSTEP function library.

These developmentNEXTSTEP people use all classes in the function library in a more narcissistic manner.NEXTSThe abbreviation of TEP is naming by the header, that isNS. Common examples include:

 

NSLog

NSString

NSInteger

NSURL

NSImage

...

You will often see that some of the teaching will use:

Nslog (@ "% d", Myint );

This statement is mainly used in the console for tracking. You will see the value of Myint in the console (open the dbg window when running in xcode ).

You can also see other classes with names, such as CF, CA, CG, and ui, such

Cfstringtokenizer

Calayer, which indicates the layer of core Animation

Cgpoint indicates a vertex.

Uiimage: The image in the iPhone

 

Cf is about core Foundation, CA is about core animation, CG is about core graphics, and UI is about iPhone user interface ...... There are many other things you can find yourself.

 

Iv. Objective-C common syntax description

1. the header file is referenced in the form of # import "file name" or # import <File Name> to ensure that each header file is contained only once;

 

Class 2 Declaration starts with @ Interface Class Name: inheritance class and ends with @ end. Class implementation starts with @ implementation class name and ends with @ end;

3. Add a minus sign (-) before the method name, and add a plus sign (+) before the method name );

The call format of the four methods is [class name class method], and the call format of the member method is [Instance name instance method]. This mode is called the message mechanism in objc, an [object message] sends a message to an object. The result is that the object calls the corresponding instance method defined in this class;

5. The following is a simple example to illustrate the above Syntax:

 

Print class. h file (Declaration file)

 

 

# Import <Foundation/Foundation. h>

 

@ Interface print: nsobject {// all objective-C classes inherit from nsobject

// Member attributes

Nsstring * Caption;

Nsstring * photographer;

}

// Attribute synthesis is introduced in objective-C 2.0, which is equivalent to the previous get/set method.

@ Property (nonatomic, copy) nsstring * Caption;

@ Property (nonatomic, copy) nsstring * photographer;

 

// Class Method

+ (Nsstring *) printname;

@ End

 

 

 

Print class. M file (implementation file)

 

# Import "print. H"

 

@ Implementation print

@ Synthesize photographer;

@ Synthesize Caption;

 

// Class Method

+ (Nsstring *) printname

{

Return (@ "print result ");

}

 

@ End

 

 

 

PrintClass usage

 

# Import "print. H"

 

Int main (INT argc, const char * argv [])

{

NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init];

Return nsapplicationmain (argc, (const char **) argv );

// Class method call

Nslog (@ "classname =/" % @/"", [Print printname]);

 

// Initialization

Print * P = [[Print alloc] init];

// Setter call

[P setcaption: @ "mycaption"];

[P setphotographer: @ "myphotographer"];

 

// Call the accessor

Nslog (@ "caption =/" % @/"", [p caption]);

Nslog (@ "photographer =/" % @/"", [p Photographer]);

[Pool drain];

 

Return 0;

}

 

 

 

Run this code and the result is as follows:

 

11:12:13. 715 demo [1471: 903] classname = "print result"

11:12:13. 718 demo [1471: 903] caption = "mycaption"

11:12:13. 718 demo [1471: 903] photographer = "myphotographer"

 

I will write it here today. I will write a blog for the first time.Objective-C memory management.

Related Article

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.