Getting started with iOS development☞OC Language & #183; Note 2, iosoc

Source: Internet
Author: User
Tags notification center

Getting started with iOS development☞OC language · Note 2, iosoc
1. attributes and instance variables)

1.1 What is an attribute defined in essence (what is the compiler doing for us ):

1) generate instance variables to save attribute values

2) generate the accessors (setter and getter methods) for modifying and accessing the attribute values

1.2 What We Know in actual development:

1) read-only attribute: Read-Only values cannot be modified. This property only generates the getter method and does not generate the setter method.

2) calculation attribute: there is no corresponding instance variable, and the attribute value is often calculated. In swift, this type of attribute is called a computing attribute.

3) custom setter/getter: The getter method and setter method of an attribute can be customized by the programmer. When the programmer is not satisfied with the access method (setter/getter) generated by the compiler, it can be customized.

A. If only the setter method is customized, the compiler adds the getter method and instance variable.

B. If only the getter method is customized, the compiler adds the setter method and instance variable.

C. If getter and setter are customized, the compiler will think that you do not need instance variables and will not add instance variables.

1.3 Other attributes

1) if getter and setter are both customized, instance variables will not be automatically generated. However, if you need instance variables, you can use the attribute synthesizer synthesize to specify the instance variables:

In implementation, add: @ synthesize attribute name = instance variable name;

// Use the attribute synthesizer to specify the instance variable corresponding to the attribute

@ Synthesize age = _ age; // at this time, the compiler will generate the instance variable _ age.

2) If the getter method and setter method do not need to be generated by the compiler, you can use @ dynamic in the. m file to ask the compiler not to generate an access method.

Application Scenario: This method is generally used in CoreData code.

@ Dynamic attribute name; // do not generate the setter and getter attributes of this attribute

Special Properties: the compiler does not generate setter and getter. during runtime, the program obtains the setter and getter methods through special means.

This situation is often seen in CoreData code. (For example, the attribute value must be read from the background database)

3) Access scope of instance variables

  • The instance variables defined in the interface section (. h file) are accessible by default (@ protect) within the class and within the subclass.
  • The instance variables defined in the. m file can only be accessed within the class. In fact, it cannot be accessed anywhere. All are private.
  • If you really want other people to access the instance variables defined in the interface section (. h file), you can use @ public to publish them. We do not recommend this.

// Access modifier for instance variables

{

@ Protected // this parameter is protected by default and can be omitted without writing

Int protectedVar; // It is protected by default and can only be accessed within the class and within the subclass.

@ Public

Int publicVar; // public instance variables, which are generally not used in this case.

@ Private

Int privateVar; // Private instance variable, which can only be accessed within the class

}

2. Initialization Method (constructor)

2.1 concepts

A special method used to initialize objects.

Methods Starting with init in OC are called initialization methods (constructor ).

Purpose of constructor: used to initialize an object so that an object can have certain attributes and values as soon as it is created.

[[Type alloc] init]; // init is the initialization method.

 

2.2 how to write

Every object has an initialization method by default: init, which is inherited from the NSObject class.

If the programmer is not satisfied with the inherited init method, he can rewrite it by himself.

How to write? Fixed statement:

1) You must first call the initialization method of the parent class and assign the returned value to self (self is a reference to the current object)

2) If the initial method of the parent class is called successfully, the attribute in the object is assigned an initial value. The main purpose of object initialization is to assign values to attributes.

3) when the method ends, the current object (self) must be returned ).

Note: If the initialization method returns NULL, the initialization fails.

    

2.3 precautions for initialization

1) There can be multiple initialization methods, which can be selected when initializing objects.

2) When a class does not provide any initialization method, there is also the init method. The init method inherited from the parent class initializes all attributes to 0.

3) For NULL (nil), the literal value of the NULL pointer in OC is nil, which is equivalent to NULL in C.

    

4) Return Value Type instancetype (instancetype automatically changes the type of the returned value to a specific type)

  • Starting from the iOS7 SDK, the initialization method, and the factory method for creating an object, the returned values all use the instancetype type.
  • Instancetype automatically identifies the return type based on the returned object of the method (the returned object instancetype becomes the returned type, and instancetype automatically knows the returned type). instancetype can only appear in the returned type.

2.4 id type

The id type is a dynamic type. When the program runs this statement, it knows the type of a variable in this statement.

The variable defined by the id type. The type of the value stored in the variable is uncertain. It is determined only during runtime and not during compilation. It is also called a universal pointer.

  • The id type is omnipotent and risky. The id type is only for the object type, not for the basic type. The id type can only point to the object. Is a common object type!
  • The id type variable is actually a reference (pointer) that can point to any type of object, similar to the void * (Universal pointer) in C language)

For example, id r = [[TRPerson alloc] init];

The id type is defined as the pointer to the Instance Object of the NSObject subclass.

Double d = 10.5;

Id a = & d; // ERROR

Id is a pointer, but you do not need to add *

Id stu = nil;

Variables of the id type can appear anywhere a variable can appear, such as method parameters, return value types, or local variables.

Advantages of using the id type: it can point to any object; disadvantage: the compiler has lost the check for the type.

 

Conclusion: differences between id and instancetype

The instancetype type is better than the id type, because instancetype automatically converts its return value type to its specific type. The id type is a universal type, with a certain risk (some type conversion is required, and some more steps are performed to reduce the performance and efficiency ).

3. class method)

3.1 concepts

1) instance method ):

The method starting with "-" is called the instance method.

It must be called through an instance (object). The instance method is called when messages are sent to an object.

2) Class Method

A method starting with "+" is called a class method.

The class method must be called through the class. Class methods will be called when messages are sent to the class.

If the specific implementation of a method is irrelevant to the object (that is, the object's attributes or instance variables are not used), you can consider writing a class method !!!

Note:

The member variables of the class cannot be used in class methods! (That is, objects cannot be directly accessed in class methods)

You can use the member variables of the class in the instance method!

 

3.2 why use class methods

1) it is convenient to call class methods without creating objects.

2) If there is a method that does not need to use an object attribute, you can consider writing it as a class method for convenience of calling.

3) class methods are often used to create objects. These methods are called simple factory methods or factory methods.

In class methods, you cannot directly access attributes and instance variables, or directly call instance methods. (Operations related to objects cannot be used in class methods !)

The class method belongs to the entire class and has nothing to do with the specific object. Therefore, the attributes and instance variables of the object cannot be accessed in the class method.

4. Factory Method)

A class provides a class method to return a new object to facilitate object creation. This class method is called the factory method. Is a concrete embodiment of the simple factory model.

The factory method is to put the alloc method and init... Merge methods into a method.

Factory method name format: Class Name WithXXX... (Lowercase first letter of the class name)

TRPoint * p1 = [[TRPoint alloc] initWithX: 1 andY: 2]; // create an object

TRPoint * p2 = [TRPoint pointWithX: 2 andY: 4]; // create an object using the factory Method

5. Singleton: It means a single instance. Is a special factory method. Only one object can be created for a class.

What is Singleton mode:

1) mainly used for Application resource sharing control

2) only unique objects can be generated.

3) Create and access objects using class methods

Principles of Singleton mode:

1) define an instance in the class (static global variable)

2) define a class method in the class as a singleton Method

3) if the instance defined in the class is empty in the singleton method, the instance of the class is created; otherwise, the instance is returned.

     

     

Advantages of Singleton mode: Resource Saving, efficiency improvement, and unified management.

The nsicationicationcenter notification center is also a singleton class.

UIApplication (representing an application, and the App is unique) is a singleton class.

Design Mode: (23 classic design modes)

The best solution to a typical problem.

Factory mode: simple factory mode (Factory method) Factory mode Abstract Factory

 

Shorthand: [TRPoint new]; equivalent to writing [[TRPoint alloc] init]; (a different meaning)

[[TRPoint alloc] init] is a convenient way to rewrite the initialization method (init...). [TRPoint new] cannot be written.

 

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.