iOS face question (partial)

Source: Internet
Author: User

1. What is the difference between heap and stack?

Management method:

For the stack, it is automatically managed by the compiler, without our manual control, and for the heap, the release work is controlled by the programmer and is prone to memory leak.

Application Size:

Stack: Under Widows, the stack is a data structure that extends to the low address, and 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 widows, the size of the stack is 2M (also said 1M, in short, is a compile-time determination of the constant), if the application space exceeds the remaining space of the stack, will prompt overflow (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 due to the fact that the operating system is stored with a chain representation of the free memory address, which is naturally discontinuous, while the traversal direction of the list is from the low address to the 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 issues:

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 can never be a memory block from the middle of the stack popped

Allocation method:

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 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.


Summarize:

the difference between heap and stack can be seen in the following analogy:
Use the stack like we go to a restaurant to eat, just order (send application), pay, and eat (use), eat enough to go, do not bother to cut vegetables, wash vegetables and other preparation work and washing dishes, brush pot , such as finishing, his benefits are fast, but less freedom.
The use of the heap as if you do like to eat food, more trouble, after eating also have to clean, cleaning and other follow-up work (or used up not to wash the bad again), but more in line with their own taste, and the freedom of large


What is the role of 2.static keywords?

1) The function body static variable scope is the function body, different from the auto variable, the memory of the variable is allocated only once, so its value will remain the last value at the next call

2) The static global variable within the module can be accessed by functions used within the module, but not by other functions outside the module;

3) The static function within the module can only be called by other functions within the module, and the use of the function is limited to the module in which it is declared;

4) static member variables in a class are owned by the entire class and have only one copy of all objects of the class;

5) The static member function in the class is owned by the entire class, which does not receive the this pointer, and therefore can only access the static member variables of the class


3. Write an algorithm to sort {9,4,2,6,1,3,5}

    Nsarray *array = @[@9,@4,@2,@6,@1,@3,@5];    Nsmutablearray *marray = [Nsmutablearray array];    for (int i = 0; i<array.count; i++)    {        [Marray addobject:array[i]];    }    NSLog (@ "Sort before%@", Marray);    for (int i = 0; i<marray.count-1; i++)    {for        (int j = 0; j<marray.count-1-i; j + +)        {            NSString *firs t = marray[j];            NSString *secend = marray[j+1];            if ([first compare:secend] = = nsordereddescending)            {                [Marray exchangeobjectatindex:j withobjectatindex:j+1 ];            }        }    }    NSLog (@ "%@ after sorting", Marray);


4. There is a rope, a length of 30 meters, a short cut is a long cut of 3/5, the length of a section of how many meters?




1. What are the differences between class and class extensions?

The difference between category and extensions is that the latter can add attributes. The other method that the latter adds is the one that must be implemented

Extensions can be considered a private category.


Can 2.objcetive-c classes inherit multiple interfaces? What is Category?

Objective-c can not implement multiple inheritance, can implement multiple interfaces, through the implementation of multiple interfaces can accomplish multiple inheritance of C + +, category is a class, the method of rewriting classes, only valid for this category, does not affect the other classes and the original class relationship.


3.object-c attribute Characteristics
(assign, retain, copy, ReadOnly, ReadWrite, Atomic, nonatomic)

1) Assign: Assignment property, setter method assigns the passed parameter to the instance variable; only when the variable is set;

2) Retain: Represents the holding characteristics, setter method will pass in the parameters of the first reservation, and then assign the value, the Retaincount will be +1 when passing parameters;

3) Copy: Represents the assignment attribute, the Setter method copies the incoming object to a copy, requires a completely new variable;

4) ReadOnly: Read-only feature, generate getter methods only, do not generate setter methods, do not want attributes to change outside the class

5) ReadWrite: Readable writable feature, when getter methods and setter methods need to be generated

6) Nonatomic: Non-atomic operation, determines whether the compiler-generated setter getter is an atomic operation; atomic means multithreading security, general use Nonatomic


4. Write a setter method to complete the @property (nonatomic,retain) nsstring *name, write a setter method to complete the property (nonatomic,copy) NSString *name

-(void) SetName: (NSString *) str{     [str retain];     [Name release];     name = str;} -(void) SetName: (NSString *) str{     id = [str copy];     [Name release];     name = str;}


5.object-c How to manage the memory, say your opinion and solution? What is the difference between ARC and MRC memory management?

There are three main methods of memory management in OBJECT-C: ARC (Automatic memory count), MRC (manual memory Count), memory pool.

1) Arc (Automatic Memory Technology): ARC is the code at compile time for you to automatically insert release or autorelease in the appropriate location, you just need to apply for memory, use it, and do not need to release it, the system will help you solve, ARC than MRC save a lot of code, Makes development more efficient, Ios5/mac OS X 10.7 starts to import, arc differs from garbage collection mechanism

2) MRC (Manual memory Technology): That is, after a period of memory is applied, there is a variable to save the memory of the number of times used, we temporarily call it a counter, when the counter becomes 0, then the time to release the memory. For example, when a memory in program A is successfully applied, then the counter is changed from 0 to 1 (we call this process alloc), and then program B also needs to use this memory, then the counter will change from 1 to 2 (we call this process retain). Immediately after program a no longer needs this memory, program a will reduce this counter by 1 (we call this process release). Program B also does not need this memory, then also the counter minus 1 (this process or release). When the system (i.e. Foundation) finds this counter to be 0, the memory recycle program is called to reclaim the memory (we call this process the Dealloc). If there is no foundation, then the maintenance counters, freeing up memory and other tasks need to be done by hand. Workaround: Typically created by static methods of the class, the function name does not appear in the Alloc or Init typeface, such as [NSString string] and [Nsarray Arratwithobject:], after the creation of the reference count +0, after the function is released after the stack, It is equivalent to a local variable on a stack, and of course can extend the lifetime of an object by retain.

3) Memory Pool: You can control the timing of memory requests and recoveries by creating and freeing memory pools. Workaround: The autorelease is joined to the system memory pool, the memory pool is nested, and each memory pool needs to have a create release pair, just like the one written in the main function. The use is also very single, such as [[[[NSString Alloc] initwithformat:@ "Hey you!"] Autorelease], a NSString object is added to the most internal system memory pool, and when we release the memory pool, the objects are freed.

The difference between arc and MRC

in fact, arc internal mechanism principle is also originated from MRC, ARC is in IOS 5/mac OS X 10.7 start to import, use Xcode4.2 can use this function. ARC's primary purpose is to make the code concise, programming simplification, development more liking should also launch, reduce unnecessary small problem small negligence; As the name implies, automatic reference count management, the application of memory, the use and release process to the system automatically implemented, we can not be involved in the process, but in fact, the MRC principle , but it is the system to help us do the management;
MRC, manual Reference Counter management, is in our application to a piece of memory, after use, to manually release, release mechanism involves counter problems, if not free memory, will cause memory waste, commonly known as memory leaks, and even caused a lot of unknown error results, this program has a great threat, but, When to release, how to release, pay attention to what problems, very fastidious, this is the inconvenience of MRC, but also the reason for Apple to launch arc;
the specific mechanism of MRC, the counter is what, in the process of the changes in the program, to what extent will release memory, how to operate; recommended to consult related documents;
MRC, in the code up and down form the main expression, when the object is called, to do retain operation, use finished to release, and finally to rewrite the Dealloc method, all objects of the class is released, so the MRC code will have Autorelease,retain , release and other words,
arc does not allow these words and should be done automatically by the system for these operations.

Reference Counter
1. Methods related to memory management
1) Alloc reference counter is automatically set to 1
2) Retain reference counter +1 returns the current instance object after +1
3) Release reference counter-1, and not necessarily the release
4) Retaincount Gets the value of the reference counter
5) Dealloc The system is called automatically before the instance object is destroyed.
be sure to tune [Super Dealloc]

terminology related to memory management
1) Zombie object: This object is destroyed, no longer used, no message can be sent to it
2) Wild pointer: A pointer to a zombie object (memory not available), sending a message to the wild pointer will have an uncontrolled effect.
3) NULL pointer: No pointer to any object, sending a message to a null pointer does not produce any behavior

Memory Management Principles
1. If you want to hold an object, you must be responsible for making a retain operation, referencing counter +1.
2. If you want to give up ownership of an object, be responsible for letting it do a release operation, referencing counter -1.
3. Who retain, who release


6. Introduction of protocols and categories

Category: Sometimes we need to add some methods to an already defined class, rather than trying to rewrite the class. For example, when the project is already large, the amount of code is greater, or there are many methods already wrapped in the class, and other code has called the class to create the object and use the method of the class, you can use the class to extend the new method.

Note: Categories can only extend methods, not member variables.

Protocol: Protocol (Protocol) is similar to the Java Language Interface (interface), defines a set of methods, and does not provide a specific implementation, only those "obey" (conform to) or "adopt" (adopt) the Protocol class to give their own implementation. Protocols are not classes themselves, they define only the interfaces that other objects are responsible for implementing. When implementing the method of the protocol in its own class, the user's class is in compliance with this Protocol, and the method of the Protocol Declaration can be implemented by any one class.

7. What is the role of the delegate (agent)? How does the agent implement, please simple code example?

Delegate (agent) role: The role of the delegate (agent) has two, one is the value of the transmission, one is the event.

1) The transmission value is in class B to send their own data or objects to a class, let a class to show or deal with. (This function is particularly useful when passing parameters between two view views)

2) The event is what happens to Class A, which tells the person who is concerned about the event, that is, the object of the delegate, the object of the delegate to consider what should be done after the event. Simply put, if an event occurs in Class A, it does not come out by itself, but by entrusting the delegate to the Class B of its delegate (of course, the delegate object B will implement the method of the delegate).

Realize:

In the. h file

@protocol everyframedelegate<nsobject>-(void) dosomethingeveryframe; @end @property (nonatomic,assign) id< everyframedelegate>delegate;


8. Describe a retain cycle example that you have encountered


9. The difference between #import and #include @class

#import是Objective-C keyword to import the header file, #include是C/c++ import header file keyword, using #import header file will be imported automatically once, will not repeat the import, equivalent to # include and #pragma once @class tell the compiler the declaration of a class, when executed, only to see the implementation of the class file, you can resolve the header file of the mutual inclusion; #import <> files used to contain the system, #import "" to contain the user header file


10. Inside the method of an object: Self.name = @ "Object" and _name = @ "Object" What's the difference?

Self.name = @ "Object" invokes the object's SetName () method;

_name = @ "Object" assigns an object directly to the Name property of the current object


11. Please outline the mechanism of Self.name = nil and the difference from [_name release]

Self.name = nil; call SetName with the Nil parameter: method

The accessors generated by [_name release] will automatically release the previous name object


12. Please explain the sandbox mechanism of iOS app

The sandbox mechanism (sandbox) in iOS is a security system that specifies that an application can only read files within a folder created for the app and cannot access content elsewhere. All non-code files are saved in this place, such as slices, sounds, attribute lists and text files, etc.,

1) Each application is in the upcoming sandbox

2) You can't go across your sandbox to access the contents of another application sandbox

3) The application requires permission authentication to request or receive data outward


What are the types of data in 13.object-c, and what are their differences from the basic data types?

The data types of objective-c are NSString, NSNumber, Nsarray, Nsmutablearray, NSData Wait, these are classes, are objects after creation, and the basic data type of the C language int, is only a certain byte of memory space for storing values; Nsinteger is the basic data type, not a subclass of NSObject. Nsinteger is the alias of the base data type int or long, and the difference is with whether the Nsinteger determines whether it is an int or a long based on whether the system is 32-bit or 64-bit.


What is the image storage type of the 14.iOS UI

Images are generally stored as nsdata types


15. What is wrong with this code?
-(void) SetName: (NSString *) newname{

Self.name = NewName;
}

Dead loop


16 When do you use Nsmutablearray? When do I use Nsarray?

Nsarray creates a static array, and once created, it is no longer possible to add and delete objects in the array.

Nsmutablearray creates a dynamic array, optionally adding or removing elements from the array

When to use which, see the engineering needs

iOS face question (partial)

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.