Explanation of variables and Data Types in Objective-C

Source: Internet
Author: User

Objective-CMediumVariableAndData TypeIs the content to be introduced in this article. It is very detailed from the class initialization, scope reviewVariable),Data TypeSummary, etc. This article thinks this article is very suitable for beginners to refer to, let's first look at the details.

I. class initialization

When writing initialization, follow two policies: If your class contains multiple initialization methods, one of them should be the specified desigrated) initialization method, all other initialization methods should use this method. Generally, it is the most complex initialization method, and generally it is the initialization method with the most parameters ). By creating a specified initialization method, you can concentrate most of the initialization code into a single method. Then, anyone who wants to derive a subclass from this class can reload this specified initialization method to ensure that the new instance is correctly initialized.

II. Scope Review

1. commands that control the scope of instance variables

When declaring instance variables in the interface, you can specify three commands before the instance variables to control the class scope more accurately.

1) protected-the instance variables following this command can be directly accessed by the class and the methods defined in any subclass. Default value.

2) private -- the instance variables following this command can be directly accessed by the methods defined in this class, but cannot be accessed directly by the methods defined in the quilt class.

3) public -- the instance variables following this command can be directly accessed by the methods defined in this class, or directly accessed by methods defined in other classes or modules.

4) package: For a 64-bit image, you can access this instance variable anywhere where the image of this class is implemented.

For example:

 
 
  1. @ Interface Printer: NSObject
  2. {
  3. @ Private
  4. Int pageCount;
  5. Int tonerlevel; // no one from the Printer subclass can access it.
  6. @ Protected
  7. // Other instance variables // can be accessed by the quilt class and can be accessed by the Printer class
  8. }
  9. ......
  10. @ End

The @ public command allows other methods or functions to access instance variables by using the pointer operator->.

2. external variables

If a variable statement is written at the beginning of a program, except for methods, class definitions, and function definitions, the value of this variable can be referenced anywhere in this module. -- Global variable: it is a convention to Use lowercase g as the first letter of the global variable; external variable. For example, int fMoveNumber;

An external variable is a variable that can be accessed and changed by any other method or function. In an external variable module that needs to be accessed, the variable declaration is the same as that of a common method, and you only need to add the extern. Notify the system to access the global variables defined in other files. For example, extern int gMoveNumber;

3. Static variables

Except for methods in a specific class, no other methods need to access this specific variable. You can define the variable as static in the file that contains the implementation of this specific class. For example, static int gGlobalVar = 0;

Iii. Storage Class specifiers

1. auto

Declare an automatic local variable. The default declaration method for internal variables of a function or method. For example, auto int index; -- int index;

The default value of static variables is 0, while that of automatic variables is not.

2. const

Set the const attribute for variables whose values remain unchanged in the program. The value cannot be changed and must be initialized. For example, const double pi = 3.141592654;

3. volatile

The value of the specified type variable will change to the opposite of const ). To prevent the compiler from optimizing variable assignments that seem redundant, and avoid repeatedly checking variables whose values do not change. For example:

 
 
  1. volatile char *outPort;      
  2. *outPort = '0';     
  3. *outPort = 'N'; 

Prevents the first value assignment statement from being deleted from the program.

Iv. enumeration data type enum)

Assign a series of values to a variable.

The sequence of names and identifiers of The enum enumerated data type is contained in a pair of curly braces)

For example:

 
 
  1. enum flag{false,true};  
  2. enum flag endOfData,matchFound; 

You can assign true and false variables to these two variables.

If you want an enumerated identifier to correspond to a specific integer, you can specify an integer for the identifier when defining the data type. The enumeration identifier that appears once in the list is assigned the number of sequences starting with a specific integer.

For example:

 
 
  1. enum direction{up,down,left=10,right};  
  2. up=0,down=1,left=10,right=11 

Enumeration identifiers can share the same value. For example:

 
 
  1. enum boolean{no=0,false=0,yes=1,true=1}; 

You can explicitly assign an integer to a variable of the enumeration type, using the type conversion operator. For example:

 
 
  1. lastMonth=(enum month)(monthValue=-1); 

Use enumeration as an independent data type.

When defining the enumerated data type, you can omit the data type name and use the variable Declaration as one of the specific enumerated data types. For example, enum {east, west, south, north} direction;

When defining the enumerated data type, make sure that the enumerated identifier is different from the variable name and other identifier defined in the same scope.

V. typedef statements

Assign another name for the data type. For example, typedef int Counter; -- int = Counter

Vi. Data Type Conversion

1. conversion rules

1) if one operand is of the long double type, the other operand is converted to the long double type, and the calculation result is also.

2) if one operand is of the double type, the other operand is converted to the double type, and the calculation result is also.

3) if one operand is of the float type, the other operand is converted to the float type, and the calculation result is also.

4) if an operand is of the _ Bool, char, short int, bit field, or enumeration data type, all operations are converted to the int type.

5) if one operand is of the long int type, the other operand is converted to the long int type, and the calculation result is of the same type.

6) if one operand is of the long int type, the other operand is converted to the long int type, and the calculation result is of the same type.

7) both are int type and the calculation result is also.

2. symbol Extension

As long as the signed int or short int is converted to an integer of more bytes, the symbol bit will be extended to the left side during the conversion process.

On some computers, characters are processed as signed numbers. Symbol extension occurs when you convert a character to an integer.

Summary: DetailsObjective-CMediumVariableAndData TypeI hope this article will help you!

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.