iOS constants, variables, attributes, and their attributes

Source: Internet
Author: User
Tags modifiers

1.objective-c declare constants using the keyword Const. such as: const double PI = 3.1514;

Variables in 2.objective-c can be divided into member variables, local variables, and global variables (rarely used, try not to use them).

————————------------constant —————————————————//

Method One:

#define Kdetailkey @ "Detail text"

#define Download_timeout 60.0

#define Degressstoradian (x) (m_pt* (x)/180.0)

This method defines functions, strings, and numbers directly using # define, and the only difference is that the string needs to be preceded by the "@" symbol, just as in normal C + +.

Method Two:

typedef enum{

Ktaglanguageview = .

Ktagseriesview,

Ktagseriesdetailview,

Ktagthumbview,

Ktagvideoview,

Ktagfullphotoview,

}tagsystemviews;

Use an enumeration to define constants, where Ktagseriesview equals 101, and use Ktagseriesview directly in the program to represent this constant, as in C + +.

Method Three:

Use static constant declarations in. m or. mm files, just as you would with C + + methods, for example:

Static nsstring *blockcoloralphacomponentkey =@ "blockcoloralphacomponent";

————————------------variable —————————————————//

1. To force an object to hide its data, the compiler restricts the scope of the instance variable to limit its visibility in the program.

But to provide flexibility, Apple also lets developers display the set range (four choices).

2. The four compiler directives are as follows:

@private

An instance variable can only be accessed by the class that declares it.

@protected

An instance variable can be declared with its class and subclass, and all instance variables that do not display the specified range are @protected.

@public

Instance variables can be accessed from anywhere.

@packge

With the modern runtime, a @package instance variable is actually @public in the implementation of this class's executable image, but outside is @private.

The @package in OC is similar to the private_extern of variables and functions in C language. Any use of this instance variable outside the mirroring of the implementation class will cause link error to be thrown.

This type is most commonly used for instance variables of framework classes, too restrictive using @private, and too open with @protected or @public.

—————————————— attribute —————————————————//

Properties: @property: @property is just an instruction to the compiler, which can automatically generate the appropriate getter and setter methods for you after compiling.

1. Public properties indicate how you intend to use objects of this class.

2. A property declaration is a directive that tells the compiler how to generate an access method for a variable. (after adding the attribute declaration, you will learn about the access method)

The 3.id type is a generic type, and OC uses an ID to represent an object of any type, which can be represented as a placeholder for an object or reference of an indeterminate type. Therefore, all objects can be represented by an ID.

4. compiler directive @property can declare a property for a class, which is a good way to be lazy, declaring a property with @property is equivalent to declaring and implementing an accessor method for the corresponding instance variable. Of course, you can also control the behavior of an automatically generated accessor method by specifying the attribute's attribute (Attribute) class.

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

6.iOS OC Declares the difference between the variable in @interface brackets and the use of @proper:

Method one: declared directly in the curly braces in @interface.

@interface Mytest:nsobject

{

nsstring *mystr;

}

Way two: direct with @property declaration

@interface Mytest:nsobject

@property (strong,nonatomic) NSString *mystr;

subsequently @synthesize mystr = _mystr in. m files; (can also be added without @synthesize in. m files)

Difference:

A member variable declared in a method is used only within its own class and cannot be used outside of the class (that is, by means of the class name.). The point is not displayed. )。

Mode two can be accessed outside the class, or in the inside of the class by an underscore + variable name or self. Variable name.

The Apple Development template recommended is mode two.

7.OC is a strict parent set of C, and C can do things in OC.

property is actually a setter method and a getter method, all access to the instance variable is so, set the value by setter method, get the value by getter method

You don't need to optimize things that you don't need to optimize, you have to optimize things that take a lot of time, and accessing instance variables doesn't take much time.

8..h is a common API

. M is a private API and all of your implementations.

All pointers are either strong or weak, because OC needs to know how to handle memory and heap. Strong means to keep this stored, in the heap, to keep what this refers to

7.plist: The property profile, which is based on XML, also plays a part in defining the UI, and Xcode reads the configuration information in the compilation.

8. The Declaration of a class provides an interface between this class and the programmer, the declaration of the class is the interface, and the implementation code is where the actual task is actually performed. The declaration of the class begins with the @interface instruction and ends with the @end instruction.

9. Blue Vertical Line: It is the distance guide for UI controls and screen borders, and the alignment guides that set the position of the control on screens of different sizes.

13. Attributes use point syntax, and other cases use square brackets to mark the syntax.

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

Copy

1. Simple copy can only achieve shallow copy: pointer assignment, so that each pointer points to the same piece of memory space, the operation is not safe.

2. This can be done in iOS: The Foundation class has complied with the <NSCopying> and <NSMutableCopying> protocol, which implements the copy and Mutablecopy methods, Therefore, the foundation object can use these methods to create a copy or a mutable copy of an object.

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

Declaring custom attributes for a property

1. Accessor methods

A. We generate accessor methods for properties by @synthesize this tag.

B. By default, if we declare a property named PropertyName, the compiler automatically generates an accessor method of PropertyName: and Setpropertyname:.

C. You can set the name of the accessor method through the custom attributes of both the Getter=gettername and Setter=settername properties.

D. The name of the custom accessor method causes the "dot sentence" syntax to be syntactically sweet.

2. Writable

If attributes are added to the ReadOnly attribute: The compiler does not automatically generate setter accessor methods, and the properties become read-only.

Features supported by 3.setter

A. Setter methods are more than simple assignments, so several modifiers are used to describe the setter method: Assign,retain,copy and weak, which represent the generated setter method differently with different modifiers.

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

C. Retain: The release method that represents the Retain method and the old value that will invoke the new value after the assignment.

D. Copy: Indicates that a copy of the value is first copied and then assigned to the instance variable, which is only applicable to the type that implements the Nscopying protocol.

E. Weak: Indicates that the value will be a weak reference, there is no relationship between the reference and the reference. If the system destroys an object referenced by a property, the value of the property is automatically changed to nil. In general, if a parent object has a reference to a child object, then the child object should not hold a strong reference to the parent object, but instead use a weak reference to refer to the parent object.

F. Strong: Indicates that this value is a strong reference, and that the reference and the referenced have a owning relationship. From a memory usage standpoint, the referenced object is not recycled until the reference object is recycled.

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

A. NONATOMIC specifies that accessors are non-thread-safe, that is, multiple threads can access this property at the same time.

B. By default, accessor methods are thread-safe, even in multithreaded environments. However, even if you want to ensure that you are working in a multithreaded environment, accessors do not necessarily have to be thread-safe. Instead of specifying the accessor method as non-thread safe, you can have the Getter method return the value of the instance variable directly and be more efficient.

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

Composition (synthesizing) properties

1. When a property is declared, the compiler automatically complements the associated code, that is, the compiler automatically declares an instance variable and the associated accessor method, and the name of the instance variable is preceded by an underscore in the attribute name.

2. But sometimes we need to specify the name of the instance variable, and then we need to use the @synthesize directive.

3.

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

@synthesize label;

This line of code requires the compiler to automatically generate accessor methods based on the attributes declared in the interface.

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

@synthesize label = _label;

This indicates that the instance variable is named _label, and this is a good way to encapsulate an instance variable so that all access to that instance variable is performed through accessor methods. At the same time, this method avoids the case where the compiler automatically takes a name and conflicts with the used instance variable name.

iOS constants, variables, attributes, and their attributes

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.