OC is a kind of object-oriented thought compared with C language. OC fully compatible with C language
Most of the classes in OC are preceded by the prefix NS, which comes from the Jobs NeXTSTEP team, which retains all the basic syntax when acquired by Apple and merges into the foundation
OC language with. m for file suffix
Absolute majority class in OC inherits from NSObject OC is single inheritance (can use protocol to achieve multi-inheritance effect)
One, the keyword @ formatted as an object
@ "Hello, world!" As a String object
@[@ "123", @ "234"]; Array
@selector () Selector
@property Properties
ID denotes any type
Second, class
declares that the person class inherits from NSObject
@interface Person:nsobject
{//member variable (shadow with struct), global variable (it is a global variable)
Nsinteger age;
NSString *name;
}
Realize
@implementation person
@end
In many cases we will rewrite the Init method, for example:
-(Instancetype) init
{
self = [super init];
if (self) {
Do something, set a default value preset condition
Age = 20;//is automatically set to 0 if it does not exist
Name = @ "Zhangsan"; The method of assigning constant value
/*
Age = 0;
name = nil;
Char *s = NULL
*/
name = [[NSString alloc]initwithformat:@ "Zhangsan"];//is typically assigned in this way
NSLog (@ "%ld", age);
NSLog (@ "%@", name);
}
return self;
}
Third, object initialization
Person *per = [[Person alloc]init];
Person *per = [[The person new];//is the same as the above effect
The memory allocated object OC is automatically allocated memory and then initializes the allocated memory to the desired object Alloc allocate memory init initialization
Note: Person *per1 = per; It's just a change of name or assignment.
Four, object output
objects are output with%@
NSLog (@ "%@", per);
V. Methods
Format:
Caller return value method name (with parameter with colon) parameter type formal parameter name
-(void) run: (int) distance;
Call:
[Object Method Name]
[Per run];
"Introduction to OC-Classes and objects"