Getting started with IOS development

Source: Internet
Author: User

Today's content is class, method definition and implementation

I. Class Definition

# Import <Cocoa/Cocoa. h> // 1

@ InterfaceStudent: NSObject // 2

{

Int age;

NSString * name; // 3

}

@ Property (nonatomic, retain) NSString * name; // 4

@ Property (nonatomic, retain) int age;

 

-(Id) initName: (NSString *) aName andAge: (int) aAge; // 5

-(Id) inttName: (NSString *) aName; // 6

-(Void) sayHello;

 

@ End // 1

Note:

  1. # Import tells the Preprocessor to include the header file content in this file. The import in. OC can ensure that the header file will only be included once.

  2. @ Interface Keyword: declare a student class. @ End statement.

Colon: indicates that the parent class is inherited.

NSObject is the memory management used by most objects. It is equivalent to the initialization framework, reflection, and type operations.

NS is short for NextSTEP, indicating that this function comes from the Cocoa toolkit.

  1. Declare global variables, as in C.

  2. Property keywords: Set the attributes of member variables (including read/write, assign, retain, copy, and nonatomic support for multithreading ).

  3. Declare a method in the format of-(Return Value) method keyword 1: (parameter type) parameter name method keyword 2: (parameter type) parameter name ...... (When reading a method, you can first find the method keyword to determine the parameter ).

-The minus sign is the instance method, and the plus sign is the class method.

 

 

II. Implementation of Classes

# Import "Student/Student. m"

# Synthesizename; // 1

# Synthesizeage;

 

@ ImplementationStudent // 2

 

-(Id) initName: (NSString *) aName andAge: (int) aAge // Implementation Method

{

If (self = [super init]) // 3

{

Self. name = aName;

Self. age = aAge;

}

Return self;

}

-(Id) initName: (NSString *) aName // 4

{

Self = [self initName: aName andAge: 0];

Return self;

}

-(Void) sayHello

{

NSLog ("% @", Hello World); // 5

}

@ End

Note:

  1. # Synthesize Keyword: automatically generates an access method for the member variable according to the @ property setting, so that the point operator can be used to conveniently access the member variable.

  2. @ Implementation indicates the implementation of the class @ end

  3. Self Keyword: similar to this in java, It is a hidden parameter that points to the class of the currently called method.

Super Keyword: Call the method of the parent class.

Self = [superinit] This is not to judge whether self and [superinit] are equal, but whether the initialization is successful. [Super init]: If the parent class is initialized successfully, pass = to self, so that self becomes a non-empty object, which is not false (non-no ).

 

3. The concept of calling an existing Initialization Method in another initialization method is called designated initializer.

4. nslog is the standard output in OC. When the date, time, and application name are appended to the output. When nslog () is used to output the value of any object, % @ format is used. When this specifier is used, the object uses a method named description to provide its own nslog () format.

% I bool value.

 

3. Object generation and method call

 

# Import <Foundation/Foundation. h>

# Import "student/student. H"

 

Int main (INT argc, const char * argv []) // 1

{

NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init]; // 2

Student * S1 = [[STUDENT alloc] initname: @ "zhangsan"

Andage: 15]; // 3

[S1 sayhello]; // 4

[S1 realsources]

[Pool drain] //

Return 0;

}

Note:

  1. Int argc, const char * argv []: argv [0] accepts the program name. Subsequent parameter command lines follow user input parameters, argc records several parameters input by the user in the command line.

  2. Create an automatic release pool

  3. Create an object format class name * object name = [[class alloc] init initialization data];

Alloc is a function that returns a pointer to N consecutive character storage units. The alloc function caller can use this pointer to store character sequences, here, we create a memory space for the object and initialize all the data to 0.

Init is a function that assigns values to member variables in the class. There is no difference with the general method, but init is added at the beginning.

4. The method call format [Object Name method name] is similar to the object name. method name;

 

 

PS:

 

  1. BOOL: boolean type. 1 or YES is true. 0 or NO is false.

  2. @ "Indicates that the string after @ is treated as an NSString object.

  3. Id: This is a newly added data type of ObjC. It is a common object type and can store any type of methods.

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.