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
- # Import <Foundation/Foundation. h>
- Int main (INT argc, const char * argv []) {
- Nslog (@ "Hello objective-c ");
- Return (0 );
- }
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
- @ Interface circle: nsobject
- {
- Shapecolor color;
- Shaperect bounds;
- }
- -(Void) setfillcolor: (shapecolor) color;
- -(Void) setbounds: (shaperect) bounds;
- -(Void) draw;
- @ 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
- [Circle draw];
- [Circle setfillcolor: kredcolor];
- [Car settire: Tire atindex: 2]; <span> </span>
@ Implementation
The implementation is the specific implementation of the content stated by @ interface.
Objective-C code
- @ Implementation
- -(Void) setfillcolor :( shapecolor C)
- {
- Color = C;
- }
- @ 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
- Id shapes [3];
- Shapes [0] = [circle new];
- [Shpaes [0] setbounds: rect];
- ...
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
- @ 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
- @ Implementation car
- -(ID) Init
- {
- If (Self = [Super init]) {
- Engine = [engine new];
- ...
- }
- Return (Self );
- }
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
- -(Engine *) engine;
- -(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
- # Import <Cocoa/cocoa. h>
- @ Interface tire: nsobject
- @ 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
- @ Class tire
- @ Class Engine
- ...
- 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