1.C language oriented process, OC object-oriented
2. First OC Program
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
}
return 0;
}
1) Import introduces header file to prevent duplicate inclusion
2) Foundation/foundation.h file contains a lot of header files, introduced this is tantamount to the OC library files are cited.
3) @autoreleasepool
4) comparison between NSLog and printf
NSLog Wrap, printf does not
NSLog will output time and other project information, printf will not output debugging information
The argument to the NSLog function is a NSString object, and printf is a string constant pointer
Print the string with%@,nsstring *[email protected] "ANCC"; NSLog (@ "%@", str1);
[Email protected] "" represents a string
4. Documentation Comments
/**
*
**/
5. Enhanced for Loop
6. Definition of class
//
// main.m
// OC1
//
// Created by fanyafang on 15/10/30.
// Copyright © 2015 itcast. All rights reserved.
//
#import <Foundation / Foundation.h>
#pragma mark Person Class
@interface Person: NSObject
{
@public
int _age;
NSString * _name;
}
-(void) getAge;
@end
@implementation Person
-(void) getAge {
NSLog (@ "dddd");
}
@end
#pragma mark
int main (int argc, const char * argv []) {
@autoreleasepool {
Person * p = [Person new];
p-> _ age = 10;
NSLog (@ "% d", p-> _ age);
[P getage];
} return0;}
7. Understanding of Memory
Code area: Store class definition, load once
Heap Area: Store case variables, save the address of the Code area class
Stack area: Storing objects
Usage of 8.NSString:
1) How to create a string
NSString *[email protected] "This is a string!";
NSString *s=[nsstring New];
NSString *imgname=[nsstring stringwithformat:@ "xxxxxxxx%02d.jpg", i];//format Create string
NSString *s2=[[nsstring Alloc] initwithstring:s1];//Create a new string with an existing string
2) output A string
NSLog (@ "%@", asstring);
3) String length calculation method
Using strlen function in C language
Nsuinterger len=[s1 length];//nsuinterger unsigned long integer in OC
1 Men by 1 lengths
9.OC Multi-file development
The header file and the class implementation file are written separately, with the import containing
10. Learning of class methods
11. Anonymous Class
To invoke a class method using an anonymous class: [[Car new] stop];
[Car new] is equivalent to [[Car alloc]init]
The advantages of an anonymous class: You can simplify the code and make it easier to call the method; disadvantage: The anonymous object is instantiated only once with the member variable correctly.
The writing of the 12.get-set method
13. Relationships between objects:
Combination (grape) dependency (one object's method parameter is another object) association (owned, the member of one class is an object of another class)
No overloads in 13.OC, class methods cannot have the same name
Usage of 14.static
1) Extend the life cycle of a variable in a method
2) defined in the implementation class, defined in the interface class, only valid in the previous file
Use of 15.self
1) In an object method, call another object method
2) in a class method, call another class method
3) modifier variable-in the Set method, the shape participates in the instance variable name equal, takes the instance variable with self
16. Inheritance
1) Variables that cannot be defined in the same way as the parent class
2) Single inheritance, does not support inheriting multiple classes
3) Support for multi-layer inheritance subclasses can also be inherited
17. Case Variable Modifiers
Public: Publicly available, also accessible in other classes
Protected: Protected type, accessible only in the current class and subclass
Private: cannot be accessed directly
Variables and methods that are not declared in the. h file are defined only in. m files and cannot be inherited and used
18.%@ the address of the Print object, the default calling object's description method, you can override this method
19. Polymorphism: The ability of different objects to respond to the same name method in their own way is called polymorphic
Parent class pointer to object of child class
20. The nature of the class is the class object
1) class C1=[d class]; Class C1=[D1 class]; if D and D1 are objects of the same class, that C1 and C2 point to the same address
2) Acquire the method, obtain class C1=[d class by the case object, and obtain the class C1=[dog class by its name;
3) Use of class object instead of class name new object
21.SEL [email protected] (test)//manually package the test method into SEL type
[P performselector:s1];//sends the SEL message to the test method, which is equivalent to [P test];
22. The essence of point syntax is to invoke getter, setter method, not access member variable
Object. Property =...;//replaced by the Set method
... = object. property;//Replace with Get method
Use of [email protected]
Written in the declaration file, equivalent to defining a get, set method declaration
#import <Foundation/Foundation.h> @interface Animal : NSObject
{ int _age;
}
@property int age; //-(void)setAge:(int)age; //-(int)age; @end
Use of [email protected]
Code written in the implementation file equivalent to the comment section
#import "Animal.h"
@implementation Animal
@synthesize age;
//-(void) setAge: (int) age {
// self-> age = age; // @ synthesize age will automatically create a new age instance variable
//}
//-(int) age {
// return self-> age;
//}
@end
@synthesize age=_age;//Specifies the case variable name and does not manipulate the default variable
Equivalent:
//-(void)setAge:(int)age{
// _age=age;
//}
//-(int)age{
// return _age;
//}
After xcode4.4, you can use only @property instead of @systhesize, and you do not have to define an underscore variable
The action is a case variable with an underscore, and if the current class does not have an underscore case variable, then the system will generate it for us. Note that the variable is private (relative, hidden) subclasses do not see
@property enhanced, override the Get, set method in the. m file, but cannot override it at the same time, overriding only one
25. Dynamic type and static type
Dynamic type: A pointer to the parent class that points to the object of the child class
Static type:
The use of 26.id, ID is a universal pointer, can point to any object
Both NSObject and ID can point to any object
NSObject object is compile-time checked (requires coercion of type conversions)
ID does not need to force type conversion, ID can be used directly
After the compiler sees the ID, it is considered a dynamic type, not checked
ID Obj=[animal new];
27: Dynamic Binding
Introspection
Dynamic Type detection:
1) Type of judgment
1)) Determines whether an object is an instance object of a class, or a case object of a subclass
BOOL iskind = [ANI Iskindofclass:[animal class]]; (Objects and classes)
2)) To determine whether this class is a case, whether or not it is a subclass of this class
Ismemberof (objects and classes)
3)) Determine if the case is a subclass
Issubclassofclass
BOOL iskind = [Dog issubclassofclass:[animal class]];(classes and classes)
2) method Response detection
1)) Determines whether the object responds to the specified method:
Respondstoselector: Sel of the method
2)) Determines whether a class can respond to a specified method
Instanceresponsetoselector: Sel of the method
3) Response method
No reference:Performselector:sel
There is one parameter: Performselector:sel withobject:@ "parameter"
There are two parameters: Performselector:sel withobject:@ "parameter" withobject:@ "Parameter 2"
28. Constructor method Init, is an object method that returns an object-the object that invokes the Init method
29. Overriding the construction method
-(instancetype)init{
self = [super init];
if(self){
_age=10;
}
return self;
}
OBJECTIVE-C Foundation