Hong Xiaoyao IOs (1): preparing for the sailing notes

Source: Internet
Author: User

I did flash development. Due to ANE and other development issues, I decided to invest in iOS development.

 

IOS development aims at ANE and interest.

 

Document preparation: the English version is also prepared for temporary translation problems.

Device: Apple air iPhone 4S ipad2 each.

 

 

 

 

Objective-C basics 1-6

After chapter 1-6 of the Basic tutorial of ective-C, the notes for some things are not a complete tutorial, but are similar to the notes and different and unfamiliar things.

Everything stems from the constant law of Hello world, the oldest language structure, that is, the omnipotent program. The objective-C extension is. M (also mentioned in later books. mm is the c ++ style)

 

Objective-C code  
  1. # Import <Foundation/Foundation. h>
  2. Int main (INT argc, const char * argv []) {
  3. Nslog (@ "Hello objective-c ");
  4. Return (0 );
  5. }

 

The nslog method is an exclusive type of objective-C. All cocoa objects are prefixed with NS and used as a distinction. The input parameter @ "" indicates that the string is processed as an nsstring. The rest of the program is similar to that of C. # The meaning of import is self-evident.

Boolean Value

The Boolean variables in objective-C are slightly different. The parameter type is bool and the value is YES/NO, where yes is 1 and no is 0, which occupies 8 digits.

Note that if a value such as int and short is assigned to bool,Only low-level bytes will play a role, This is especially important, because it means that it is not traditionally considered, non-zero is true

Objective-C has a unique syntax:[Object operations]This will always be seen later

About OOP in objective-C

@ Interface

 

 

Objective-C code  
  1. @ Interface circle: nsobject
  2. {
  3. Shapecolor color;
  4. Shaperect bounds;
  5. }
  6. -(Void) setfillcolor: (shapecolor) color;
  7. -(Void) setbounds: (shaperect) bounds;
  8. -(Void) draw;
  9. @ End

 

The above is a standard @ interface definition. It should be well understood that circle contains two variables: color, bounds, and three methods.

Pay special attention toStatement

(Void) indicates the method name and parameter after the return type

The draw method does not include any parameters:

Method Declaration for multiple parameters

-(Void) settire: (tire *) tire atindex :( INT) index; (the second parameter here looks a bit strange, but in the process of calling the method later, atindex will be used)

The objective-C method call uses something called an infix.

Let's take a look at the syntax for calling no-argument, 1-argument, and multi-argument respectively.

 

Objective-C code  
  1. [Circle draw];
  2. [Circle setfillcolor: kredcolor];
  3. [Car settire: Tire atindex: 2]; <span> </span>

 

@ Implementation

The implementation is the specific implementation of the content stated by @ interface.

 

Objective-C code  
  1. @ Implementation
  2. -(Void) setfillcolor :( shapecolor C)
  3. {
  4. Color = C;
  5. }
  6. @ End

 

The color = C here is actually equivalent to self. Color = C (here I want to make an analogy, the current understanding of self is equivalent to the this pointer in Java)

With @ interface and @ implementation, how can we define a class (object )?Instantiate an objectWhat about

 

Objective-C code  
  1. Id shapes [3];
  2. Shapes [0] = [circle new];
  3. [Shpaes [0] setbounds: rect];
  4. ...

 

Here, we can see a new thing Id, which is a pointer to an object. So far, I have seen this usage, but I don't know much about it. I simply understood it as an index.

Then we can see the method [circle new] of the new object, and then call the method of the object to set specific parameters.

Inheritance

 

Objective-C code  
  1. @ Interface circle: Shape

 

The method is simple. Objective-C is different from Java and C # in terms of inheritance rules,Multi-inheritance is not allowedHowever, since there are interfaces (or Protocols), it is naturally the same as Java. It is not difficult to achieve the same purpose. Similarly, subclass can call the method of the parent class through Super, such[Super setcolor: C];

In Ojbective-CThe null value is nil.

Next, let's take a look at how a car is automatically built.

 

Objective-C code  
  1. @ Implementation car
  2. -(ID) Init
  3. {
  4. If (Self = [Super init]) {
  5. Engine = [engine new];
  6. ...
  7. }
  8. Return (Self );
  9. }

 

This section cannot be understood. It is mentioned in the comments in the book that if the superclass can complete the one-time initialization required, you need to call [Super init]. The return value ID of the init method describes the object that times initializes. Assigning the result of [Super init] to self is a standard practice of objective-C.

The getter/setter method is the same in objective-C, but you need to pay attention to the naming rules.

 

Objective-C code  
  1. -(Engine *) engine;
  2. -(Void) setengine: (engine *) engine;

 

The setxxx method is still used in the set method,The get method directly uses xxxInstead of getxxx to avoid confusion

Split

AverageAdd the @ Interface part to. h.File

 

Objective-C code  
  1. # Import <Cocoa/cocoa. h>
  2. @ Interface tire: nsobject
  3. @ End

 

Place other parts in. M and remember to introduce the header file in. M.

# Import "tire. H"

Import <> indicates the system header file "" indicates the local header file.

The keyword @ Class tells the compiler that this is a class and must be passed through pointers.

 

Objective-C code  
  1. @ Class tire
  2. @ Class Engine
  3. ...
  4. Engine * Engine

Note thatAvailable only when the pointer is directed to another classTo reduce the compiler burden.Inheritance is not availableBecause he needs to know the information about the super class.

 

 

Recommended OC video tutorial: http://www.youku.com/playlist_show/id_4545842.html

 

 

 

 

 

 

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.