Objective-C concepts to be remembered

Source: Internet
Author: User
Tags variable scope

1. Definition class: In. H files,

@ Interface Class Name: parent class name

{

Member variables;

}

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.