Objective-C speed (2)

Source: Internet
Author: User
Tags types of functions
Custom class. On Object-c, a class is composed of a public header file. h and A private implementation file. m. the header file defines the class interface, starting with @ interface and ending with @ end as follows: # import <Foundation/Foundation. h>
@ Interface Member: NSObject // The class name is Member, which inherits NSObject
{
// Instance variable
NSString * name; // member name
Int points; // points purchased by Members
}
// Method declaration
-(NSString *) name; // obtain member name Information
-(Void) setName :( NSString *) value; // set the member name
-(Int) points: // obtain Point Information
-(Void) setPoints :( int) points; // sets Member points
-(BOOL) hasPoint: // determines whether the member has points.
-(Void) buyCourse :( int) courseID; // a member purchases a course
@ End in the implementation file, we mainly implement two types of functions: one is the get/set method, and the other is the specific operations, such as purchasing courses. To implement a file, first import the header file and then @ implementation class. The program is as follows. This article mainly describes the knowledge points of implementing the Program syntax format, super, self and so on. # Import "Member. h"
@ Implmentation Member
-(Id) init {// initialization method, optional, provided as needed, defined in the parent class.
If (self = [super init]) {// call the initialization method in the parent class: self is similar to this in Java

Name = @ "anonymous ";
Points = 0;
}
Return self;
}

-(Int) points {// get method, return the attribute value

Return points
}

-(Void) setPoints :( int) value {// set method, set the attribute value
Points = value;
}

-(NSString *) name {// return name

Return name;

}

-(Void) setName :( NSString *) newName {// Set Name

If (name! = NewName) {// if there are two different objects
[Name release]; // release the old object

Name = [newName copy]; // get the new object. obj-c is passed by reference instead of by value.
// The application count for name is 1

}
}

-(BOOL) hasPoints {// do you have any points?

Return ([self points]> 0); // self is similar to this in java,

}

-(Void) buyCourse :( int) courseId {// purchase Course

If ([self hasPoints]) {
...
} Else {
NSLog (@ "points required ");
}
}

-(Void) dealloc {
[Name release]; // release the memory occupied by name
[Super dealloc]; // use super to call dealloc in the parent class
}
@ End: a gray object can define multiple init methods, such as-(id) init;
-(Id) initWithName :( NSString *) name;
-(Id) initWithName :( NSString *) name points :( int) points; If there are multiple init methods, the general method calls the specific method, for example:-(id) init {
Return [self initWithName: @ "NO Name"]
}
-(Id) initWithName :( NSString *) name {
Return [self initWithName: name points: 0];
} If you have applied for memory for some variables, you need to release them in the dealloc method, but instead of directly calling dealloc to release them, you can use release. in the application, you call your own class to complete some operations on Member * member = nil;
// Alloc allocates memory. If you call alloc, you must consider releasing it.
Member = [[Member alloc] init]; // init is used to set the initial value.
[Member setName: @ "king"];
[Member setPoints: 50];
[Member buyCourse: 18];
[Member release]; // use release to release internal management of the memory occupied by objects: similar to C, Objective-C needs to use alloc to apply for memory. The difference is that C directly calls the free function to release the memory, while Objective-C does not directly call dealloc to release the memory. The entire Objective-C uses Object reference, and each object has a reference counter. When the counter is 0, the system calls dealloc to release the inner. Objective-C provides the autorelease attribute, allowing the system to automatically release the memory occupied by objects. When you use the alloc (or copy) method to create an object, the counter value is 1, the retain method is called 1, and the release method is called 1. When the counter is 0, the system automatically calls the dealloc method to release objects in the memory, for example, after Member * member = [[Member alloc] init]; // after execution, the counter is 1 [member retain]; // after execution, the counter is 2
[Member release]; // after execution, the counter object is 1
[Member release]; // the counter is 0 after execution, and the system automatically calls the dealloc Method
// After the object is released, the application will abort if any method of the object is called.
To prevent exceptions from being aborted. You can add a sentence after the last release:
Member = nil;
Set the object to nil. After that, any method that calls the member will return nil, rather than abort the exception:
Analysis:
-(Void) setName :( NSString *) newName {

If (name! = NewName ){
[Name release];
Name = [newName copy]; // The Reference Counter of name is 1
}
} 4.5.2 automatic release pool (autorelease) If you create a string in the Code and if you need to return this string, you need to use autorelease instead of release; -(NSString *) welcome {

NSString * result;
Result = [[NSString alloc] initWithFormat: @ "Welcome % @ login", name];
// [Result release]; // cannot be released; otherwise, the caller cannot obtain new string information.
[Result autorelease]; // use autorelease.
Return result;
} With autorelease, the object is put into the automatic release pool. The system automatically tracks the usage of each object and releases all objects in the pool when the pool is released automatically, autorelease is not a system garbage collection function. The obj-c garbage collection function on the IPhone operating system. The following are the basic principles of Memory Management: 1. If you use the alloc (or copy) method to create an object or use retain to keep an object, you must release it yourself. 2. In most cases, the number of statements applying for memory should be equal to the number of statements releasing memory. 3. Try to use less memory. 4.6 conceptually, the obj-c class is similar to the java/C ++ class. You must specify the class name, attributes, and methods. Ojb-c provides the @ Property and @ synthesize flags, which are used to identify attributes in the class and allow the system to automatically generate a set/get Method Instance in Member. h.
-(NSString *) name; // obtain member name Information
-(Void) setName :( NSString *) value; // set the member name
-(Void) points: // obtain Point Information
-(Void) setPoints :( int) points; // use @ property to mark Member points: @ property (copy) NSString * name; // set copy (release old object, copy new object) @ property (int) points; in Member. in m, we originally compiled the following code:-(int) points {// get method, return the attribute value

Return points
}

-(Void) setPoints :( int) value {// set method, set the attribute value
Points = value;
}

-(NSString *) name {// return name

Return name;

}

-(Void) setName :( NSString *) newName {// Set Name

If (name! = NewName) {// if there are two different objects
[Name release]; // release the old object

Name = [newName copy]; // get the new object. obj-c is passed by reference instead of by value.
// The application count for name is 1

}
} Now you can change it to @ synthesize points; @ synthesize name; in the above statement, @ sythesize automatically generates the get/set method.

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.