Objective-c (Grammar one)

Source: Internet
Author: User

Dot syntax

The essence of point syntax is method invocation

Scope of member variables

@public : The member variables of an object can be accessed directly from anywhere

@private : can only be accessed directly in the object method of the current class (default is @private in @implementation)

Subclass if you want to access access by Setter Getter method

@protected : can be accessed directly in the object methods of the current class and its subclasses

(Default is @protected in @interface)

@package : You can directly access the member variables of an object as long as you are in the same frame

Generally not used, the system comes directly with the frame

member variables with the same name cannot be declared in the @interface and @implementation

You can also write member variables in @implementation, without declaring them, and you can write the set get method directly

@implementation Class:nsobject

{

int _a;//is private by default

}

-(void) SetA: (int) A

_a = A;

-(int) A

{

return _a;

}

@end

@property can automatically generate a declaration of the setter and getter for a member variable

Cases

@interface Class:nsobject

{

int _a;

Double _b;

}

@property int A;

@property double b;

@end

@synthesize: Automatic generation of a setter and getter implementation and access to _a this member variable

The _a member variable is accessed, and if the member variable does not exist, the _a variable of the @private type is automatically generated

Cases

@implementation Class

@synthesize a = _a;

@synthesize B = _b;

@end

After Xcode version 4.4, the @property has @synthesize function, which means @property can simultaneously sound as setter and Getter Declaration and implementation

The implementation of the setter and getter methods by default accesses the member variables that begin with the underscore _

ID Type

ID: Universal pointer, can point/manipulate any OC Object ID ==nsobject *

Note that the ID does not add the following *

Construction Method: The method used to initialize the object, which is an object method

Overriding construction method Purpose: To have the member variables in the created object have some fixed values

To override the Init method:

1. Be sure to call Super's Init method: Initialize some member variables and other properties declared in the parent class

Example-(ID) init

{

self = [super init]; Current Object Self

2. The subsequent initialization is necessary if the object is initialized successfully

if (self! = nil)

{

_age = 10;

}

return self;

}

In the @implementaion class name

-(ID) init

{

if (self = [super init])

{

_age = 10;

}

return self

}

overriding in @end

Custom Construction methods:

Specification:

1. Must be the object method, must begin with _

2. return value ID type

3. Method names begin with Initwith

1. Declaration @end above in the @interface

Example @interface Person:nsobject

@property NSString *_name;

-(ID) Initwithname: (NSString *) name;

@end

2. Implement @end above in @implementation

Example @implementation person

-(ID) Initwithname: (NSString *) name

{

if (self = [super init])

{

_name = name;

}

return self;

}

@end

In the main.m file:

int main ()

{

Person *p = [[Person alloc] initwithname:@ "Rose"]

return 0;

}

Objective-c (Grammar one)

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.