iOS classic face question (i)

Source: Internet
Author: User

Objective-c Classic Face question

1, Objective-c class can inherit multiple? Can I use multiple protocols? What are the basic features of OC language?

It is not possible to multiply inheritance, you can use multiple protocols. The basic features of OC language are: encapsulation, inheritance, polymorphism.

2. What is the difference between the #import和 # include? What's the difference between #import<> and #import?

#import能避免头文件被重复包含的问题:

(1) Generally speaking, when importing objective C header file with #import, including A/C + + header file with # include. Use include to pay attention to the problem of repeated references: class A,class B all refer to class C,class D if you refer to class A and Class B, you will be quoted a duplicate reference error.

(2) #import确定一个文件只能被导入一次, this allows you to have no problem with recursive inclusion. # include no matter how many times it is imported, it will import directly, resulting in a duplicate import problem, so the advantage of #import比起 # include is that it avoids the problem of repeated references. So in OC we basically use import.

#import <> go directly to the system library, typically the classes in the iOS Framework class library,

#import "" First look in this code system, if you do not go to the system library to find. Typically a custom class in a project.

3. What is category? How do you extend a class by inheriting good or class? Why?

Category is an item of category. Use a good class, because the inheritance to meet the relationship of A is a B, and the class only needs to meet A has a B relationship, the limitations are smaller, you can extend the function of a class without defining subclasses, but also to separate the definition of the class into different source files, the category to rewrite the method of the class, Valid for this category only, does not affect the relationship of other classes to the original class.

4. What is extension? What is the role?

Extension (extension): Adds a class in the implementation file of its own class to declare a private method.

5. @protected of Class instance (member) variables, @private, what does @public declaration mean?

@protected: Protected, the instance variable can only be accessed within the class and its subclasses and cannot be accessed within other classes.

@private: Private, the instance variable can only be accessed within that class and cannot be accessed within other classes.

@public: Common, this instance variable can be accessed by anyone.

6. What are the characteristics of an ID declared object?

No * number

Dynamic Data types

You can point to any class object (set to nil) without caring about its specific type Ø at run time check its specific type

can send any (existing) message to it

7. What is a delegate? What are the attributes of the property declaration between the delegate and the principal? Why?

Delegate: One object holds a reference to another object, and the referenced object implements a predetermined protocol that notifies the referenced object of changes in the Reference object.

The property declaration properties of both the delegate and the principal are assign rather than retain in order to avoid a memory leak caused by circular references.

The problem of circular referencing is so understood:

For example, two classes of objects A and B are created in the main function, and the reference count is now 1. Now let A and B reference each other (A has a property is a B object, the property description is Retain;b has a property is a object, the property description is retain), now the reference count of two objects has increased 1, has become 2. Now execute [A release]; [B release];   At this point the main function of the created object has freed itself of ownership of the object, but at this point the reference count of A and B are still 1 because they reference each other. At this point you find that A and B will not be released, because to release a you must first release B, and then release a in the Dealloc method of B. Similarly, to release B, you must first release a, and then release B in the Dealloc method of a. So these two objects will always be in memory and not released. This is called the circular reference problem. To solve this problem, the general method can set the referenced property to assign instead of retain to handle it.

8. What is the difference between a shallow copy and a deep copy?

Shallow copy: Copies only pointers to objects, not the reference object itself, and copies the object reference count plus 1 (such as the NSString object call copy is a shallow copy).

Deep copy: Copies the reference object itself and copies a new object based on the original object (such as a Nsmutablestring object called copy is a deep copy).

It means that I have an A object, after copying a copy to get the A_copy object, for shallow copy, A and a_copy point to the same memory resources, copy is just a pointer, the object itself is a resource or only one copy, if we do a modification to the a_copy operation, Then we find that the object referenced by A is also modified, which in fact violates the idea of copying copies. Deep replication is a good understanding that there are two independent objects in memory.

Using a friend on the Internet, the popular words will be:
Shallow copy is like you and your shadow, you are finished, your shadow is finished deep copy is like you and your clone, you are finished, your clone is still alive.

9. What are the several principles of memory management? By default, which keywords generate objects that need to be released manually? Which cases do not require manual release and will go directly to the auto-release pool?

MRC

Golden rule: If an object uses Alloc,[mutable]copy,retain, then you must use the appropriate release or autorelease

When you create an object using the new, alloc, or Copy method, the object references a counter of 1. If you do not need to use the object, you can send a release or autorelease message to it, which is destroyed when it is finished using it.

If you get an object by other means, you can assume that the object reference count is 1, and that it is set to Autorelease, you do not need to clean the object, if you do need to retain this object, you need to use the finished release.

If you retain an object, you need to release or autorelease the object, keeping the Retain method and the release method using the same number of times.

Objects generated using the new, alloc, copy keywords, and retain objects need to be released manually. Objects set to Autorelease do not need to be released manually and will go directly to the auto-free pool.

ARC

Automatic reference counting, the use of Arc does not need to retain,release,autorelease, etc., the compiler will automatically add memory management code, control the object's life cycle.

Using Arc:

When we assign a value to a variable (except for initialization), we need to use the __weak or __strong keyword to declare the variable's strong and weak reference

Release,retain,autorelease should not appear in the program (copy can be used)

Reference count cannot be printed using the Retaincount property

The Dealloc method of the class should not be called again [super Dealloc]

Whether a heap memory is freed depends on whether the stack pointer of the __strong type is 0, and when the __strong type has a pointer count of 0, the object should be freed, regardless of the __weak pointer.

ARC's golden rule: as long as there is another variable (pointer) pointing to the object, the object is saved in memory

__strong tells the compiler to add retain,__strong here to omit

__strong and __weak

__strong default is omitted

When the pointer changes point, the old object is freed, and the new object is preserved

The __weak function is equivalent to assign, but when the object does not exist, it automatically points to nil

The set method takes ownership of the object

At the end of the program run, the ARC mechanism frees up memory for all objects

Create a new object you cannot use __weak

The string is in the constant area and will not be freed.

The array holds the ownership of the object, and the elements in the delete array are equal to the objects in the arrays to release once

Circular reference full use __weak to define attributes

Advantages of Arc:

ARC does not need to be in retain, release, no less release to cause memory leaks, excessive release causes the program to crash

ARC can produce more concise code, more robust use

The GC garbage collection mechanism developed by MAC application is also banned by arc

Apple encourages developers to migrate manually managed memory to arc, while new projects also recommend the use of Arc

ARC is the trend!

10, how to implement a singleton model of the class, give ideas, do not write code.

  The main points of the singleton pattern:

1. Only one instance of a class

2. You must create this instance yourself

3. This instance must be provided to the entire system on its own

From the specific implementation point of view to pay attention to three points:

1. The singleton mode class only provides private constructors

2. The class definition contains a static private object of the class

3. This class provides a static common function for creating or fetching static private objects of its own

You must first create a global instance that is typically stored in a global variable that is set to nil, provides a factory method to access the global instance, checks if the variable is nil, if nil creates a new instance, and then returns the global instance. The initialization of a global variable is +allocwithzone when the factory method is first called, so you need to override the method to prevent a new instance from being created in a standard alloc way

To prevent a new instance from being obtained by the copy method, you need to implement the-copywithzone method, just return the object itself in this method, and the reference count does not need to be changed, because objects in singleton mode are not allowed to be destroyed, so they do not have to be preserved

Because the global instance does not allow deallocation, the Retain,release,autorelease method needs to be rewritten

11. What is the role of @class?

In a header file, it is generally only necessary to know the name of the referenced class. There is no need to know the entity variables and methods inside, so it is common to use @class in the header file to declare that the name is the name of the class. In the implementation class, you need to use #import to include the header file of the referenced class, because it will use the internal entity variables and methods of the referenced class.

The role of @class is to tell the compiler that there is such a class, use it, no problem. @class can also solve the problem of circular dependencies, such as A.h import B.h, and B.h import A.h, each header file compilation to let the object compile successfully before the line. The use of @class can prevent this from happening.

12. What is KVC? What are the characteristics of KVO?

KVC is a key-value encoding that can be used to read and set class properties by specifying a string identifier that represents the name of the property to be accessed.

KVO is a key-value observation, characterized by the use of key values to observe an observer who can register as an object, to be notified when a property of that object changes

13. What is MVC? What are the characteristics?

MVC is a design pattern that consists of a model, a view, a controller 3 parts.

Model: A class that holds application data, a class that processes business logic

Views: Windows, controls, and elements that other users can see and interact with

Controller: A class that binds a model and tries to determine how to handle user input

14. When defining attributes, what is the use of copy, assign, retain?

Using assign: for underlying data types (nsinteger,cgfloat) and C data types (int, float, double,char, and so on)

Use copy: When you want to get a copy of the source object without changing the contents of the source object, such as NSString

Use retain: When you want to take ownership of the source object, the other nsobject and its subclasses

iOS classic face question (i)

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.