1. minus sign (plus sign)
In C ++, we define a function as follows:
Public void test (bool flag)
{
Cout <"Hello world !! "<Endl;
}
In Objective C:
-(Void) test :( bool) Flag
{
Nslog (@ "Hello world !! ");
}
"-" Indicates the start of a function or message. It is actually a method and is easy to understand. (-) (function return type) function name: (parameter type) parameter name, which is slightly different when multiple parameters are used. The format is as follows:
(-) (Function return type) function name: (parameter type) parameter name secondpara :( parameter type) parameter name threedpara :( parameter type) parameter name ....
For C ++, it is public void test (string str1, string str2, string str3); it is generally called this. test ("hello", "world ","!!!! ");
For Objective C, calling is complicated, that is, [self test: @ "hello" secondpara: @ "world" threedpara :@"!!!! "];
The space between the instance and the function indicates the call relationship, and each parameter is separated by space, which is the same as the definition form, secondpara and threedpara can be changed as long as they are called. Note that self is equivalent to this pointer.
2. Brackets
In C ++, the function we call is generally this. Test ("Hello world !!! "); In Objective C, it is [self test: @" Hello world !!! "];
3. Ns **
This naming method is actually related to history, that is, neststep, a company created by Steve Jobs. You can also see other headers, such as CF, CA, CG, and UI. cf is about core Foundation, CA is about core animation, and CG is about core graphics, the UI is about the iPhone user interface ......
4. # Import
If you have learned Java, you will know that this is the meaning of import. In fact, it is equivalent to # include in C ++.
5. @ Interface
In C ++, we define a class:
Public class test: Public object
{
String m_str;
Int m_count;
Void printout (int count = 0)
{
If (COUNT = 0)
M_count = count;
Sprintf (m_str, "This % d coming in", m_count );
Cout <"m_str" <Endl;
}
}
Objective C is defined as follows:
Test. h file Definition
@ Interface Test: nsobject {
Nsstring str1;
Nsstring str2;
}
-(Void) Printout;
@ End
The test. M file is as follows:
# Import "test. H"
@ Implementation Test
-(Void) Init {
[Email protected] "Hello world !!! ";
[Email protected] "l am coming !! ";
}
-(Void) printout :{
Nslog :( str1 );
Nslog :( str2 );
}
@ End
5. function call
In C ++, call test mytest = new test (), mytest. printout ("Hello world !!! "); And then the last delete object.
In Objective C, we need to [[[test alloc] init: @ "Hello World"] Release release];
6. Others
1). In Objective C, ID is a special variable type, which can represent all types. It can be used when the variable type is unknown,
2). For nsarray, it is an array that can store different data types, various data types, such as floating point, string, and image.
3) In Objective C, yes is equivalent to true, and no is equivalent to false.
4 ). iboutlet and ibaction do not play much role in syntax. If you want to see this control object in interface builder, add iboutlet before definition, you can see the outlet of this object in Ib. If you want to control an object in interface builder to execute certain actions, add ibaction before the method ).