AboutObjective-CA small concept is the content to be introduced in this article, mainly to understandObjective-CTo learn the details. Let's take a look at the details.
1. Definition class: In the. h file,
- @ Interface Class Name: parent class name
- {
- Member variables;
- }
1. member functions;
- @end;
In the. m file, @ implementation class name member function definition;
- @end;
2. member function definition:-(Return Value Type) function name: (parameter type) parameter ,...
- -(void) setColor:(NSString*)newColor
- {
- Color=newColor;
- }
- -(void) setNumber:(int)newNumber
- {
- Number=newNumber;
- }
3. instantiation class:
- myCar=[Car new];
- [myCar setColor:red];
- [myCar setNumber:Num];
4. @ class command:
When you want to use another defined class in a class, use @ class. If you want to use the method, # import this class.
5. Memory Management:
The iphone operating system does not support garbage collection.
Generally, alloc + init is used to create an object, and new is also available;
Alloc, copy, retain, release, autorelease;
6. Foundation framework:
- @ Import <Foundation/Foundation. h>
- NSString class, NSMutableString class;
- NSArray class, NSMutableArray class;
- NSDictionary class, NSMutableDictionary class;
7. accessors:
When declaring a class variable, you can make it an attribute. @ Property int Number;
In the. m file, add @ synthesize Number to automatically generate the setter and getter methods;
In this way, you can use the dot operator to access the member variable, car. Number = 27;
8. Create an object:
- Id object name = [class name alloc];
- [Object Name init];
- Boat * boat = [[Boat allco] init];
- Boat * boat = [[Boat alloc] initWithNum: 123
- Color: @ "red"];
In general, write as follows during init:
- -Init
- {
- If (self = [super init]) {
- // Initialization
- Return self;
- }
When parameters exist,
- -(id) initWithNum:(int) Num
- Color:(NSString*) Color;
9. variable scope:
If there is no scope restriction, the default value is protected;
10. @ try exception handling:
- @try{}
- @catch(NSException *exception){NSLog(@"Catch exception name :%@; reason:%@;",
- [exception name],[exception reason]);}
- @finally{}
11. Categories and protocols:
Sometimes you need to add some methods to a defined class without rewriting the class. You can use a category ).
If you have defined a Circle class, in the CircleCal. h file,
- @import "Circle.h"
- @interface Circle(CircleCal)
- -(float) AreaCal;
- -(float) LenCal;
- @end;
In the CircleCal. m file,
- #import "CircleCal.h"
- @implementation Circle(CircleCal)
- -(float) AreaCal{}
- -(float) LenCal{}
- @end;
In this case, the new defined function can be called in the main function;
Ps: the category cannot add instance variables to the class. The category can overwrite the methods in the class to be extended, but avoid them as much as possible. When a category is used to expand a class, it will also affect the subclass of this class.
The Protocol is a list of methods. It declares a series of methods without implementation. The Protocol is not the class itself, there is no parent class, and member variables cannot be defined.
- @protocol Calculating
- -(void) CalculateArea;
- @end;
- @interface Rectangle:NSObject <Calculating>{}
When defining a member function in Rectangle, you must also define the method in Calculating to call it.
@ Protocol has two modifiers: @ optional and @ required,
The method modified by @ optional can be disabled in the class using this protocol, but the method modified by @ required must be implemented. The default value is @ required.
Summary: I have thoroughly analyzed the content about the Objective-C small concept. I hope this article will help you!