Objective-C class, Objective-C

Source: Internet
Author: User

Objective-C class, Objective-C

Objective-C (OC) is the main programming language for OS X and iOS operating systems. The following describes how to create a class using OC:

Interface part: Student. h file implementation part: Student. m file

 

  

The class has been created. The following describes how to use the class:

Main function: main. m

 

The visibility of instance variables is divided into public, protected, and private ).

1 @ interface Student: NSObject {2 @ public // can be used outside the class. It has high visibility, destroys encapsulation, and is rarely used. 3 NSString * _ name; 4 @ protected // default. instance variables can only operate on 5 NSString * _ gender in the class and its rule class; 6 @ private // only 7 NSInteger _ age can be used in the class; 8}
 

In order not to break the encapsulation class, usually use protected (default) to create instance variables. to use instance variables in a class outside the class, you must set the setter and getter methods in the class.

// Student. hfile @ interface Student: NSObject {NSString * _ name; NSString * _ gender;} // Set name (Declaration)-(void) setName :( NSString *) name; // set gender (Declaration)-(void) setGender :( NSString *) gender; // get name (Declaration)-(NSString *) name; // get gender (Declaration) -(NSString *) gender; @ end // Student. m file @ implementation Student // Set name (implemented)-(void) setName :( NSString *) name {_ name = name;} // set gender (implemented)-(void) setGender :( NSString *) gender {_ gender = gender;} // get name (Implementation)-(NSString *) name {return _ name;} // obtain gender (Implementation) -(NSString *) gender {return _ gender;} @ end

In this way, you can define the configurator and accessors to use instance variables in the main function.

// Main. m main function # import <Foundation/Foundation. h> # import "Student. h "int main (int argc, const char * argv []) {@ autoreleasepool {// Student sends messages to alloc to memory, apply for memory // init student sends a message to init initialization instance variable Student * student = [[Student alloc] init]; // assign a value through the seter [student setName: @ "James"]; // obtain the NSString * str = [student name]; NSLog (@ "% @", str);} return 0 ;}

The properties in OC implement the getter and setter methods by default, simplify the program code to a certain extent, and enhance the security of instance variable access.

Attribute definition:

1 # import <Foundation/Foundation. h> 2 3 @ interface Student: NSObject4 @ property NSString * name; 5 // equivalent to the two methods (setter, getter) 6 //-(void) defined in @ interface) setName :( NSString *) name; 7 //-(NSString *) name; 8 @ end

Attribute implementation:

1 # import "Student. h "2 3 @ implementation Student 4 @ synthesize name = _ name; // equivalent to @ implementation (setter, getter) Method 5 //-(void) setName :( NSString *) name {6 // _ name = name; 7 //} 8 //-(NSString *) name {9 // return _ name; 10 //} 11 @ end

@ Synthesize name = _ name; it can be omitted. You can only define @ property NSString * name in the header file;

  

In OC, some keywords of setter are provided for attributes to control the implementation details of setter and getter, that is, attribute attributes.

Attributes are classified into three types:

Category 1: read/write control (readonly, readwrite, setter, getter)

1) readwrite: The default. compiler declares the setter and getter methods.
2) readonly: the compiler only declares the getter method.
3) setter specifies the setter method name to the compiler. The setter method is not used. Generally, do not modify the setter method name defined by the compiler.
4) The getter method specifies the getter method name for the compiler. getter is only used in BOOL-type attributes.

Category 1: original atomic subcontrol (nonatomic, atomic)

1) atonic atomicity. By default, it ensures thread security. It occupies a lot of system resources and reduces system performance. nonatonic is generally used.
2) nonatonic, non-atomic, does not guarantee thread security.

Category 3: semantic settings (assign, retain, and copy)

1) Basic data type of assign, default.
2) retain can only be of the object type.
3) copy can only be used for object types that follow the <NSCopying> protocol.

Usage:

1 #import <Foundation/Foundation.h>2 3 @interface CollegeStudent : NSObject4 @property (nonatomic,retain) NSString *name;5 @end

 

 

 

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.