1, from a simple routine to see the basic syntax:
The following code is generated by the Osx-application-command line tool:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
@autoreleasepool {
Insert code here ...
NSLog (@ "Hello, world!");
NSLog (@ "Hello, objective-c");
}
return 0;
}
1>, file name: FIRST_OSX_EG.M This is an OC-specific extension, and the C language extension is generally used with. C, library files with the. h;c++ extension generally used. cpp;
The OC extension is used. m (from the word message) but this is just an extension of the Description section, the extension of the declaration section is also the. h, and the file name corresponds to. m is the same name, and if the extension is. mm It can be considered C + + code (c + + is compatible with C).
2>, #import相当于C语言中的 the uses in the #include,delphi language, represents a reference to a library file or other cell. Although OC is also compatible with # include, but #import is much better than # include, No matter how many times you have #import references in a file, the compiler refers to a specific file only once.
#import之后所用的库文件, the use of double quotation marks or angle brackets is allowed in syntax, but the general system defaults to double quotes as the native file,
The system header file is followed by the angle brackets.
The 3>,<foundation/foundation.h> represents a reference to the Foundation.h file in the Foundation framework. Use the spline + single point to view the corresponding cell file. In fact, when referencing the system library, the general header
Files are a collection of hundreds of dozens of of header files. #import可以预编译头文件, making the quote faster. For example, the corresponding framework file for Foundation.h can be viewed in the following path:
/system/library/frameworks/foundation.framework/headers/(later versions of the Headers directory are changed to the resources directory).
The 4>,nslog function, where NS represents the base Cocoa library in OSX, is originally referenced in the Next-step schema, and its meaning and usage are basically equivalent to printf () in C, but its parameters require a cocoa library
Formatted string. There are a number of functions such as NS named, such as:
Nsarray: Used to define an array
Nsdateformat: Formatted date
Nsthread: Defines a thread.
Nsspeechsynthesizer: Defines a sound.
5>, using @ "XX" to denote a string, where the meaning of @ means that the string is a nsstring string in a cocoa library, rather than a traditional C-language string. As a cocoa-style string, its function
is much more powerful than the traditional C-language string. For example, you can format various types of data with formatted characters (for example,%d) using the stringWithFormat method.
6>, flower +~ can be easily switched over multiple projects.
2, commonly used OC object types:
1>,uiview: base class for all visual controls. Each object that is based on this class is a container.
2>,uilabel: Label class
3>,uitextfield: Text Input class
4>,uibutton: Button class
3, commonly used OC syntax:
1> Program Flow Control
A>, select structure:
if (bool expression)
{
An expression;
}
Else
{
An expression;
}
B>, cyclic structure:
int i;
for (i=0; i<=99;i++)
{
NSLog (@ "%d\n", I);
}
Or
while (bool-expression)
{Loop body}; This requires control of the bool expression in the loop body.
C>, branching structure:
switch (expression)
{
Case Branch 1 value: expression;
Break
Case branch 2 value: expression;
Break
...
}
' 2>, function definition: (note that in obj-c, it is not allowed to define a child function directly inside a function.) The scope of each function is automatically
Top to bottom, you can use a declaration to function globally, or to other cell interfaces, and the function allows the name of the parameter to be the same as the function name.
If a function's return value type is an object of one class, the function should use a pointer type, for example:
NSString *showmsg (int x1)
{
return ([NSString stringwithformat:@ "%d", X1]);
}
If you call this function return value, you can use the%@ format string for example:
NSLog (@ "%@", ShowMsg (123456));
About the default main function definition: int main (int argc, const char *argv[])
Where ARGC is the number of parameters of the program, argv is a parameter array, where ARGV[0] is the program file itself path + file name (this is the same as Delphi).
3>, definition of data type (with pointers for the definition of an object)
A>, integer: int A;
B>, character type: char C;
C>, String type:
Char S[n]; This is equivalent to the N-length array, and the corresponding cocoa library is nsarray. Here s is a pointer, equivalent to the string object name.
Char s[]; You can define a variable-length array so that the strings in the obj-c are handled strictly by object, not as convenient as in Delphi.
You can use strlen (s) to return the length of a string.
Char *s[]; You can use this to define a multidimensional array, each of which s[i] corresponds to a string.
NSString str; However, because obj-c often use the cocoa library nsstring, most of the time, try to use this type, it has a lot of convenient methods.
D>,bool Type: The Boolean type of OC is not true and false, but yes and no. Its essence is a 8-bit signed character. When defined, 1 is yes,0 to No. When actually used, if the other symbols
As a bool type, then only the value of the lowest bit is judged, and the high value is ignored (note that this differs from the C language, not more than 0 is true).
The value of bool type can only be compared with no, if it is false, the difference is true. The bool type cannot be compared with Yes (although sometimes it is right to compare with Yes).
E> floating-point float
F>, constant keyword const This can be added before the definition type, for example: const char *argv[];
G>, the definition of the enumeration type:
typedef enum{Enumeration Constants list, comma separated} enumeration type name;
There is no specific intrinsic function can directly get the enumeration constant name, you can construct a function to implement.
A common enumeration definition technique, which allows you to define a starting amount and a terminating amount so that it is easy to iterate through.
H>, struct type definition:
typedef struct{struct ELEMENT definition} struct-body type name;
The value of the struct can be directly enclosed in curly braces: stru1={1, ' a '};
You can also use [struct variable name. Member name] to refer to each member for assignment.
5> a more complex number of cocoa defined useful data types.
A>nsrange:
typedef struct _nsrange{
unsigned int location;
unsigned int length;
} Nsrange;
Represents the starting position of a piece of data and the length of the data.
You can use the shortcut function Nsmakerange () to return a Nsrange
eg. Nsrange Range=nsmakerange (17,4);
B> Several geometry data types: Nspoint, Nssize,nsrect
corresponding shortcut function Nsmakepoint (), Nsmakesize (), Nsmakerect ()
typedef struct _nspoint{
float x;
Float y;
} Nspoint;
typedef struct _nssize{
float width;
float height;
}nssize;
typedef struct _nsrect{
Nspoint origin;
Nssize size;
}nsrect;
C> NSString, there is no need to consider 0 manipulation characters compared to the string in the C language with the character array.
6>, file type definition: *words; Words=fopen (Argv[1], "R");
The above two sentences can also be assigned directly in the definition: FILE *words=fopen (argv[1], "R");
Fgets (word, words); This function is used to read the contents of a text file, where word is an array to receive data, 100 read length, and words as the file type.
The return value is bool, and when the file is read, a newline character is encountered to automatically end the read, and the location flag of the file is placed at the beginning of the downward line.
You can use this statement to convert the contents of the read to a string: Word[strlen (Word) -1]= ' + '; A backslash 0 is an empty-action character used to denote the end of a string.
File standard path notation:/users/murphy/...
Each user has a root folder structure named after the user name, and the corresponding system folder path is as follows:
Public:/users/User name/public
Image:/users/User name/pictures
Document:/users/User name/documents
5. Use of classes and objects
The definition of the 1> class can be divided into two parts, one part is the declaration part, and the other part is the elaboration part:
The Declarations section is used to describe the definitions of individual members and methods, in the following format:
@interface Class Name: base class name//Here the base class name can use NSObject, or other base classes.
{
@public/@private/@protected//Obj-c There is no real private method. This is the dynamic nature of OBJ-C decision.
Member Definition area;
}
@public/@private/@protect
method definition area; Methods you can use a plus or minus sign to correspond to a class method or a member method. If no minus sign is added, it is a function prototype.
e.g.-(void) Setmember: (type) parameter;
@end
The declaration section of the '//class is mainly for its method, in the following format:
@implementation class Name
-(void) method name: (type) parameter
{
method implementation;
}
@end//class name after the Terminator, add the class name, is a good habit, can increase the readability of the program.
Note here that the return value of the method is also required for the type definition, must be added minus (type) in the form of the description, the parentheses cannot be omitted. Similarly, the parameter's
Type parentheses are also not omitted.
An undeclared method is allowed in @implementation, which can be seen as a class-private method (as with Delphi).
The parameter names in the @implementation section may not be the same as the @interface section. Note that the parameters used in the @implementation
It is best not to have the same name as the member variable of the @interface section, otherwise hide the instance variable and generate a warning message.
If you want to invoke a hidden instance variable, you can use the Self-> member name.
2> use syntax for classes and objects:
A> in Obj-c, the original base class for all objects is nsobject (this is equivalent to object in Delphi).
All of the member variables in the b>obj-c are defined by the name of the type before the name of the variable. And you can assign a value directly when you define it.
C>obj-c the infix character invocation method:
[Object Name Method name: parameter];
Here the method name and colon can be considered as one, and it tells the compiler that arguments will appear later. Adding a colon after a method without parameters will cause an error.
The public interface of the D> class is called the API (Application programming Interface)
E> in the method body, allow self to be used to access the object itself.
The F>nsobject class has a class method new, which is equivalent to Delphi's construction method. We can get an instance by calling this method.
[Class name New]; Of course, all class methods can be called with a method that sends a message to the class name.
G>id can be used to define a generic object variable, which can point to any object, and an array can be defined with an ID, and then each array element
All point to objects of different classes. Here is a question, why not directly use NSObject instead of ID, but I studied the next, found ID and Delphi
The object referenced in object is really different, and you can send various subclass messages directly to it without having to use as for class conversions.
Implementation of the 3> class inheritance:
A>obj-c deprecated the features of C + + inheritance, and it is not allowed to inherit more in Obj-c. (This is the same with Delphi).
B>obj-c at the time of declaration, there is no need to use virtual or dynamic as Delphi to declare virtual functions.
Just use the Super keyword in the elaboration section when the subclass overrides this method to complete the inheritance. Its call to the parent class part format is:
[Super Method name: parameter]; This completes the invocation of the parent virtual method, similar to the inherited in Delphi; But this statement is written in the overlay
Outside of the function.
The Super keyword continues to be found along the superclass chain until it finds a method of the parent class.
Composition of the 4> class:
The process of grouping several objects together and forming a new object is called compounding.
Each composite sub-object can be thought of as a property of a parent object, and each property can be performed with the getter and setter methods of the same name
Storage, which is generally paired, has the following format:
-(Sub-object class *) Sub-object name; The corresponding get keyword is omitted, unless the getter method requires a pointer parameter to add a get
-(void) Set sub-object name: (Sub-object type *) New sub-object name; The parameter here is best not to have the same name as the original object.
The object name followed by set, and the first letter is generally used to capitalize.
Note that when the setter method is used to set the property, the substituted data is actually a pointer to the property, not the actual value. Because the object reference of Obj-c
Mechanism, we do not need to consider the active release here, which is different from Delphi.
The self-construction of a property can be implemented using the INIT function, where the self-construction of a Property object can be implemented with the following statement in the definition attribute class:
Note that the Init method belongs to the internal method of the object and is not required to be declared in the @interface section.
@implementation the name of the parent object
-(ID) init
{
if (Self=[super init]) {
Property name =[attribute class name new];
}
return (self);
}//init
...
@end
This will form the self-construction of the property, but if the property has a corresponding setter method, it is not necessary to construct, but the use of the need to actively assign value.
5> header file splitting, pointing, importing, and inheriting:
When splitting a composite class with a. m file and an. h file, you need to point to its compound attribute class with the @class class name in the header file of the composite class.
In the elaboration file of the compound class, it is necessary not only to import its corresponding header file with #import, but also to import the header files of all the corresponding attribute classes.
When a derived class splits a file, the header file of the derived class is only required to import the header file of the parent class (the system header files already in the parent class can be completely ignored)
6> Object-oriented design significance:
A> in object-oriented and process-oriented design, the biggest difference is the change of its function modification and deletion. Object-oriented to avoid modification to the whole
Processing function, and does not need to modify the basic structure, only need to add a class basic can adapt to change.
B> process-oriented design is the function of the first, the second data, which causes the program changes often affect the interface, and object-oriented design, can directly the original
The part of the function modification is cleverly moved to the object's method, we just need to focus on the object of this data is good, does not affect the function and interface.
C> when there is a large number of duplicate code in the same class, it is not a good design, we can change the inheritance structure of its classes, so that these duplicated code in the middle
Implemented in an inherited class, this method of moving and simplifying code is called refactoring.
D> You can use inheritance when you create a new class that has a relationship with an existing class is a; When you create a class that has a relationship with an existing class, it needs to be composited.
7> Object-oriented terminology:
A> Super class: Superclass
B> Parent class: ParentClass, same as Super class.
C> sub-class (derived class): Subclass
D> Child class: ChildClass, same subclass.
6, commonly used Xcode operation skills
1> Flower +[and flower +] You can move the selected code block to the left or right
The 2>esc key closes and opens the auto-complete selection box.
3>control+/can automatically jump on placeholders as they are entered.
4> Flower +control+s can create a snapshot.
5> Flower +shift+o quickly open a unit file.
The 6>control+f/b are the forward cursor and the back-move cursor, respectively.
7>CONTROL+P/N is the move cursor up and down cursor respectively.
8>CONTROL+A/E are moving the cursor to the beginning and end of the line, respectively.
9>control+t Drag the character before the cursor one position.
10>control+d Delete the character to the right of the cursor (equivalent to the DEL Key in Windows).
11>control+k deletes a row. Actually, this is the part that deletes the current line cursor.
12>control+l adjusts the window so that the current cursor is centered on the window.
13> Flower +y Run the program in debug mode.
14> flower +option+p/o/i/t are debugging to continue, skip, jump in, jump out.
Basic knowledge of Xcode and OBJ-C