iOS Interview Basics Issues

Source: Internet
Author: User

Transfer from Http://www.jianshu.com/p/9c83145bb4baiOS interviewword 2396 Read 2097 comments 4 likes

1, #import和 The difference of # include, what @class represent?

@class is commonly used in header files to declare an instance variable of the class, and in M files it is necessary to use #import

And #import's advantage over # include is that it doesn't cause duplicate inclusions

2, talk about the Object-c memory management method and process?

1. When you create an object using the New,alloc and copy methods, the object has a retention counter value of 1. When you no longer use the object, you are responsible for sending a release or Autorelease message to the object. This way, the object will be destroyed at the end of its useful life.

2. When you obtain an object by any other means, assuming that the object has a retention counter value of 1 and has been set to auto-release, you do not need to do anything to ensure that the object is cleaned up. If you intend to have the object for a period of time, you need to keep it and make sure that it is released when the operation is complete.

3. If you keep an object, you need to (eventually) release or release the object automatically. You must keep the Retain method and the release method equal in number of times.

3. Does object-c have a private method? What about private variables?

There are only two methods in the Objective-c– class, static methods and instance methods. This does not seem to be a complete object-oriented one, as the OO principle is that an object exposes only useful things. Without a private method, it would be less convenient for some small-scale code reuse. Fame a private method inside a class

@interface Controller:nsobject {nsstring *something;}

+ (void) Thisisastaticmethod;

– (void) Thisisaninstancemethod;

@end

@interface Controller (Private)-

(void) Thisisaprivatemethod;

@end

@private can be used to modify private variables

In Objective‐c, all instance variables are private by default, and all instance methods are public by default

4, object-c have multiple inheritance? What's the substitute for nothing? All classes in cocoa are subclasses of NSObject

Multi-inheritance is implemented here using the protocol principal agent.

You do not have to consider the concept of a cumbersome multi-inheritance, virtual base class.

The polymorphic properties of Ood are implemented by delegates in Obj-c.

5, memory management autorelease, retain, copy, assign set method and meaning?

1, you Initialize (Alloc/init) The object, and you need to release it (release). For example:

Nsmutablearray Aarray = [[Nsarray alloc] init]; After, need [Aarray release];

2, you retain or copy the, you need to release it. For example:

[Aarray retain], need [Aarray release];

3, the object that is passed (assign), you need to consider the retain and release. For example:

Obj2 = [[Obj1 somemethod] autorelease];

Object 2 receives an automatically freed value of object 1, or passes a base data type (nsinteger,nsstring): You or you want to retain object 2 to prevent it from being automatically freed before it is used. But after retain, be sure to release it at the right time.

Questions about index count (Reference counting)

Retain value = Index count (Reference counting)

The Nsarray object retain the object in any array (retain value plus one). When Nsarray is unloaded (Dealloc), all objects in the array are freed once (the retain value is reduced by one). Not only Nsarray, but any collection class (Collection Classes) performs a similar operation. such as nsdictionary, or even uinavigationcontroller.

Alloc/init the object that is created, the index count is 1. No need to retain it again.

Methods such as [Nsarray array] and [NSDate Date] Establish an object with an index count of 1, but it is also an automatic release object. So it's a local temporary object, so it doesn't matter. If you are a variable (IVAR) that you intend to use in a full class, you must retain it.

The default class method return value is executed with the "auto-release" method. (* Nsarray in the above)

In the Unload method "Dealloc" in the class, release all the NS objects that are not balanced. (* All not autorelease, while retain value is 1)

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

Simply put, in the case of a pointer, a shallow copy simply adds a pointer to the existing memory, while a deep copy adds a pointer and requests a new memory so that the increased pointer points to the new memory, in the case of a deep copy, The memory will not be released when the same memory is repeatedly released in a shallow copy of the error

7, C and obj-c how to mix

1) Obj-c compiler handles files with the suffix m, can recognize Obj-c and C code, processing mm files can recognize obj-c,c,c++ code, but CPP files must be used only in C + + code, and CPP file include in the header file, Nor can the OBJ-C code appear because CPP is only CPP

2) in mm file mixed with CPP direct use, so obj-c mixed CPP is not a problem

3) Mixing obj-c in CPP is actually the module we want to write with Obj-c.

If the module is implemented as a class, then it is not possible to have obj-c in the header file, including #import cocoa, as defined by the standard write class of CPP class. Implementation of the file, that is, the class implementation code can use OBJ-C, can import, but the suffix is mm.

If the module is implemented as a function, then the header file is declared in C as the function, in the implementation file, C + + functions can be used inside the obj-c, but the suffix is still mm or M.

Summary: As long as the CPP files and CPP include files do not contain obj-c of things can be used, CPP mixed with OBJ-C is the key to use the interface, and not directly use implementation code, actually CPP mixed with obj-c compiled o file, this thing is actually no difference, So it can be used. OBJ-C's compiler supports CPP

8. The difference between category and class extension in objective-c.

Answer: The difference between category and extensions is that the latter can add attributes. In addition, the method that the latter adds must be implemented.

Extensions can be thought of as a private category.

9. What do we mean by OBJECTIVE-C is the Dynamic runtime language?

Answer: polymorphic. The main is to postpone the determination of the data type by compile time to the runtime.

This problem actually involves two concepts, runtime and polymorphism.

Simply put, the run-time mechanism allows us to determine the class of an object until run time, and to invoke the class object to specify the method.

Polymorphism: The ability of different objects to respond to the same message in their own way is called polymorphism. It means that a biological class (life) is-eat in the same way;

The human beings belong to the creatures, and the pigs belong to the creatures, and all inherit the life and realize their eat, but the call is the only way we call the Eat method.

That is, different objects respond to the same message in their own way (in response to the Eat selector).

So it can also be said that the runtime mechanism is the basis of polymorphism?

10. What is the difference between objective-c heap and stack?

Management mode: For the stack, is automatically managed by the compiler, without our manual control, for the heap, the release of work by the programmer control, easy to produce memory leak.

Application Size:

Stack: Under Windows, the stack is the data structure to the low address extension, which is a contiguous area of memory. This sentence means that the top of the stack of the address and the maximum capacity of the stack is the system pre-defined, in Windows, the size of the stack is 2M (also said 1M, in short, is a compile-time determination of the constant), if the request for more space than the stack's remaining space, will prompt overflow. Therefore, the space available from the stack is small.

Heap: A heap is a data structure that extends to a high address, and is a discontinuous area of memory. This is because the system is stored with a linked list of free memory address, is naturally discontinuous, and the chain of the list of traversal direction is from the low address to high address. The size of the heap is limited by the valid virtual memory in the computer system. Thus, the space of the heap is more flexible and relatively large.

Fragmentation problem: For the heap, frequent new/delete is bound to cause memory space discontinuity, resulting in a large number of fragments, so that program efficiency is reduced. For the stack, there is no problem, because the stack is advanced out of the queue, they are so one by one correspondence, so that there will never be a memory block from the middle of the stack popped

Allocation method: The heap is dynamically allocated and there are no statically allocated heaps. Stacks are allocated in 2 ways: static allocation and dynamic allocation. Static allocations are done by the compiler, such as the allocation of local variables. The dynamic allocation is assigned by the ALLOCA function, but the dynamic allocation of the stack is different from the heap, and his dynamic allocation is released by the compiler without our manual implementation.

Allocation efficiency: The stack is the data structure provided by the machine system, the computer will support the stack at the bottom: allocate the address of the special register storage stack, the stack stack has a special instruction execution, which determines the efficiency of the stack is high. The heap is provided by the C + + function library, and its mechanism is very complex.

These are the Objective-c basic interview questions that 10 interviewers often encounter, if you are just going to apply for iOS, please go back to the article and start reading it again. iOS development is still relatively promising, do you think?

iOS Interview Basics Issues

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.