The objective-c. Operator explanation

Source: Internet
Author: User

//

Main.m

in OC. Grammar

//

Created by won't tell you who I am on 15-8-9.

Copyright (c) year Xuqigang. All rights reserved.

//

#import <Foundation/Foundation.h>

#import "Test01.h"

@interface Test01:nsobject

//{

int A;

@public int b;

int C;

//}

@property int C;

-(void) print;

int Main (int argc, const Char * argv[])

{

@autoreleasepool {

//Insert code here ...

NSLog(@ "Hello, world!" );

/* OC . features of the grammar :

1, through . Operator can invoke a member method in a class directly

2, through . The operator can assign and read an instance variable, provided that the variable has already implemented the setter and getter methods in the class. The definition and implementation of setter and getter methods can be customized, or can be done by @property Declaration @synthesize implementation. If an instance variable has only a setter or getter method then the instance variable can only be assigned or evaluated. It should be noted that for example: member variable int name; Setter methods must be declared in this manner:-(void) name; The getter method must declare this:-(int) getName;

3. For a member variable that is modified with @public , we can also take a value and assign operation outside through the operator, otherwise you cannot use this method. cannot be used to access member functions.

to say no more is useless, illustrate:

*/

int data=9;

Test01 *p = [[Test01alloc] initwith :2 :3 :5]; instantiate an object p and initialize its member variable to 2 3 5 ;

/ * below to start talking about OC inside . For the use of syntax, see the following example:

P.A; Program Error cannot be used . Grammar Because the setter and getter methods are not implemented in the class

p->a; using the direct access member variable for error reasons When you define a class by using @interface, the member properties are defaulted to Protected cannot be accessed externally

data=p.b;  assignment operation is not possible here cause: No Setter Getter method implemented

*/

data=p->b;

p->b=data;

/ * The variable b can be evaluated and assigned here because the property permission for b is public

data=p.b; Program Error! This cannot be done because the member variable b is declared as a public type but does not implement a setter Getter method, so it cannot be used . operator */

Data=p->c;

P->c=data;

/* the variable c can be evaluated and assigned here because c 's attribute permission is also public*/

Data=p.c;

P.c=data;

/* Can be used for value, assignment operation reason: member variable c implements setter getter through @property and @synthesize method, so it can be used . operator */

p. Print ; passed . syntax can also call member methods in a class directly

/* p->print; The-I operator is used to access member variables that cannot be used to invoke member methods */

data=p. func ; call the Member method and return the value using the data receive method

NSLog (@ "%d\n", data);

}

return 0;

}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The objective-c. Operator explanation

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.