IOS constants, variables, attributes and features, ios constant variables

Source: Internet
Author: User
Tags modifiers

IOS constants, variables, attributes and features, ios constant variables

1. Declare constants in Objective-C using the keyword const. For example, const double PI = 3.1514;

2. Variables in Objective-C can be divided into member variables, local variables, and global variables (rarely used, do not use as much as possible ).

// -------------------- Constant -----------------//

Method 1:

# Define kDetailKey @ "detail text"

# Define DOWNLOAD_TIMEOUT 60.0

# Define degresssToRadian (x) (M_PT * (X)/180.0)

This method uses # define to define functions, strings, and numbers. The only difference is that strings must be preceded by the "@" symbol.

 

Method 2:

Typedef enum {

KTagLanguageView = 100,

KTagSeriesView,

KTagSeriesDetailView,

KTagThumbView,

KTagVideoView,

KTagFullPhotoView,

} TagSystemViews;

Use enumeration to define constants. Here, kTagSeriesView is equal to 101. In the program, kTagSeriesView is directly used to represent this constant, which is the same as C/C ++.

 

Method 3:

Use static constant declaration in the. m or. mm file, which is the same as C/C ++, for example:

Static NSString * BlockColorAlphaComponentKey = @ "blockColorAlphaComponent ";

 

 

// -------------------- Variable -----------------//

1. to force an object to hide its data, the compiler limits the scope of instance variables to restrict its visibility in the program.

However, to provide flexibility, Apple also allows developers to display the Set range (select one from four ).

2. Four compilation commands are as follows:

@ Private

Instance variables can only be accessed by declared classes.

 

@ Protected

The instance variable can be declared as its class and subclass. All instance variables that do not display a specified range are @ protected.

 

@ Public

Instance variables can be accessed anywhere.

 

@ Packge

When modern is used for running, a @ package instance variable is actually @ public in the executable file image implementing this class, but it is @ private outside.

The @ package in OC is similar to the variables in C language and the private_extern of the function. Any image other than the implementation class image that wants to use this instance variable will cause link error.

This type is most commonly used for instance variables of the framework class. @ private is too restrictive and @ protected or @ public is too open.

 

 

 

// -------------- Attribute -----------------//

Attribute: @ property is only a type of instruction for the compiler. It can automatically generate the corresponding getter and setter methods after compilation.

1. Public attributes indicate how you plan to use this type of objects.

2. Attribute Declaration is an instruction that tells the compiler how to generate an access method for a variable. (After adding an attribute declaration, you will learn about the access method)

3. id is a common type. OC uses id to represent any type of objects. It can be used as a placeholder to indicate that this is an uncertain type object or reference. Therefore, all objects can be represented by IDs.

4. the compiler instruction @ property can declare attributes for a class. This is a good way to be lazy. Using @ property to declare attributes is equivalent to declaring and implementing the accesser method for the corresponding instance variables at the same time. Of course, you can also use the Attribute class to control the behavior of the automatically generated accessors.

 

5. instance variables in OC are private by default. Private variables are only visible in the instance methods of the class itself and its subclass.

 

6. iOS OC declares the difference between variables in @ interface brackets and @ proper:

Method 1: declare it directly in braces in @ interface.

@ Interface MyTest: NSObject

{

NSString * mystr;

 

}

Method 2: directly declare with @ property

@ Interface MyTest: NSObject

@ Property (strong, nonatomic) NSString * mystr;

Then in the. m file @ synthesize mystr = _ myStr; (you can also remove @ synthesize in the. m file)

Differences:

The member variables declared in method 1 can only be used inside the class, but cannot be used outside the class (that is, they cannot be displayed by class name. vertices .).

Method 2: access can be performed outside the class, or through the underline + variable name or self. variable name within the class.

Method 2 is recommended for Apple development templates.

7. OC is the strict parent set of C, and can do everything C can do.

The property is actually a setter method and a getter method. This applies to all accesses to instance variables. You can set the value through the setter method and get the value through the getter method.

You do not need to optimize things that do not need to be optimized. You need to optimize things that take a lot of time, and accessing instance variables does not take much time.

8. h is a common API

. M is a private API and all your implementations.

 

All pointers are either strong or weak, because OC needs to know how to handle memory and heap. Strong indicates to keep the storage, and keep the content in the heap.

 

7. plist: Property configuration file. Based on the XML format, it also plays a part of the role of defining the UI. Xcode will read the configuration information during compilation.

8. The class Declaration provides the interface between the class and the programmer. The class declaration is the interface, and its implementation code is the place where the actual task is actually executed. Class Declaration starts with the @ interface command and ends with the @ end command.

9. Blue vertical line: It is the distance between the UI control and the screen border and alignment reference line, used to set the position of the control on the screen of different sizes.

 

 

13. The attribute uses the dot syntax. In other cases, the square brackets mark syntax is used.

***********************************

Copy:

1. Simple replication can only implement shallow copy: assign values to the pointer, so that each Pointer Points to the same memory space, and the operation is not safe.

2. in iOS, this can be implemented as follows: the Foundation class already complies with the <NSCopying> and <NSMutableCopying> protocols, that is, the copy and mutableCopy methods are implemented, therefore, Foundation objects can use these methods to create copies or mutable copies of objects.

 

**************************

Declare custom features for Attributes

1. Access Method

A. The @ synthesize mark is used to generate the accessor method for the attribute.

B. By default, if the declared attribute is propertyName, The automatically generated accessors of the compiler are propertyName: And setPropertyName :.

C. You can use the custom attributes "getter = getterName" and "setter = setterName" to set the name of the accessor method.

D. The name of the custom accessors method causes the syntactic sugar of the "Point sentence" to be poor.

2. writability

If the readonly feature is added to the attribute, the compiler will not automatically generate the setter accesser method, and the attribute will become read-only.

3. features supported by setter

A. The setter method is not just a simple assignment. There are several modifiers used to describe the setter method: assign, retain, copy, and weak, the setter methods generated with different modifiers are different.

B. The default value is assign, which means that the setter method is actually a simple value assignment.

C. Retain: indicates that the retain method of the new value and the release method of the old value will be called after the value is assigned.

D. Copy: copy the value first, and then assign the copy to the instance variable. This modifier is only applicable to the type that implements the NSCopying protocol.

E. Weak: indicates that this value will be a weak reference, and there is no relation between the reference and the referenced value. If the system destroys the object referenced by a property, the property value automatically changes to nil. Generally, if the parent object has a reference to a sub-object, the sub-object should not hold a strong reference to the parent object, but use a weak reference method to reference the parent object.

F. Strong: indicates that this value is a strong reference, and there is a relationship between the reference and the referenced value. From the perspective of memory usage, it means that the referenced object will not be recycled before the referenced object is recycled.

4. atomicity and non-atomicity: atomi and nonatomic are used to determine whether the getter and setter generated by the compiler are atomic operations.

 

A. Nonatomic specifies that the accesser mode is non-thread-safe, that is, multiple threads can access this attribute at the same time.

B. By default, the accessors are thread-safe, and there is no risk even in a multi-threaded environment. However, even if you want to ensure normal operation in a multi-threaded environment, the accessors are not necessarily thread-safe. If the accessor method is specified as non-thread-safe, the getter method can directly return the value of the instance variable, which is more efficient.

 

********************

Synthesizing attributes

1. after an attribute is declared, the compiler will automatically complete the relevant code, that is, the compiler will automatically declare an instance variable and the relevant accessors method. The name of the instance variable is prefixed with an underscore.

2. However, sometimes we need to specify the name of the instance variable by ourselves. At this time, we need to use the @ synthesize command.

3.

A. If you have to manually specify the name of the instance variable corresponding to the attribute, use the @ synthesize command after the @ implementation command to make the following declaration:

@ Synthesize label;

This line of code requires the compiler to automatically generate accessors Based on the attributes declared in the interface.

B. You can also use the following method to specify the name of the instance variable used by the accessor method:

@ Synthesize label = _ label;

The instance variable name is _ label, and this is a good way to encapsulate instance variables, so that all accesses to the instance variables are executed through the accesser method. At the same time, this method can avoid conflicts between the name automatically obtained by the compiler and the name of used instance variables.

 

 

 

 

 

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.