iOS face question 2

Source: Internet
Author: User

1. Write a method to get iOS memory usage. Gets the current device's available memory and the header file of the occupied memory

#import <sys/sysctl.h>

#import <mach/mach.h>

Get current device available memory (in MB)

-(double) availablememory

{

vm_statistics_data_t Vmstats;

mach_msg_type_number_t infocount = Host_vm_info_count;

kern_return_t Kernreturn = Host_statistics (Mach_host_self (),

Host_vm_info,

(host_info_t) &vmstats,

&infocount);

if (Kernreturn! = kern_success) {

return nsnotfound;

}

Return ((Vm_page_size *vmstats.free_count)/1024.0)/1024.0;

}

Gets the memory occupied by the current task in megabytes (MB)

-(double) usedmemory

{

task_basic_info_data_t TaskInfo;

mach_msg_type_number_t infocount = Task_basic_info_count;

kern_return_t Kernreturn = Task_info (Mach_task_self (),

Task_basic_info,

(task_info_t) &taskinfo,

&infocount);

if (Kernreturn! = kern_success

) {

return nsnotfound;

}

return taskinfo.resident_size/1024.0/1024.0;}

    1. Deep copy and shallow copy understanding?
      Deep copy copy is the content, shallow copy copy is the pointer. The biggest difference between a deep copy and a shallow copy is whether the address of the member object changes, and if the address of the subclass object is changed then it is a deep copy.

2, how to achieve a singleton class.
Static Losingleton * shareinstance;

+ (Losingleton *) sharedinstance{

@synchronized (self) {// this thing is actually a lock. If the self is accessed by other threads, it is blocked. This is typically used to protect a single instance of a deadlock

if (shareinstance = = nil) {

Shareinstance = [[Super Allocwithzone:null] init];

}

}

return shareinstance;

}

// second Way

+ (Losingleton *) sharedinstance

{

static Losingleton *sharedinstance = nil;

static dispatch_once_t Oncetoken; Lock

dispatch_once (& Oncetoken, ^ {//call at most once

Sharedinstance = [[Self alloc] init];

});

return sharedinstance;

}

3. What is safe release?

Set the pointer to nil after the object release

  1. What is Runloop?
    A runloop is an event-handling loop that is used to schedule work and to process input time. The purpose of using Runloop is to keep your threads busy while they are working , and hibernate when they are not working. The Runloop is designed to reduce the idle idling of the CPU .
  2. What is serialization and deserialization and what can be used to do? How do I implement the storage of complex objects in OC?
    If you need to store a complex object, it is often necessary to serialize the object in a binary way, which is called archiving. If an object needs to be serialized, it is necessary to follow the nscoding protocol, which consists of two methods:-(ID) Initwithcoder: (nscoder*) coder;// from coder The data is read in the corresponding variable, which is the deserialization of the data. -(void) Encodewithcoder: (nscoder*) coder;// reads the instance variable and writes the data to the coder , which is the serialized data.
  3. Write a standard macro min, this macro input two parameters and return the smaller one?
    #define KMIN (x x) > (Y))? (Y):(X)
  4. Does the iphone OS have a garbage collection mechanism? Briefly explain the OC memory management.
    The iphone OS does not have a garbage collection mechanism.
    The garbage collection mechanism is used to dynamically reclaim the memory space that is occupied by objects without any references in an idle time, in an unscheduled fashion.
  5. Briefly describe the life cycle when the application presses the home key into the background and the life cycle from the background back to the foreground?
    Status of the application:
    Not runningNot running, program does not start
    InactiveInactive, the program runs in the foreground, but does not receive events and is normally in this state without event handling.ActiveActivating the program in the foreground and receiving the eventbackgoundBackground   The program is in the background and can execute code, and most programs will stay in this state for a while after entering this state.SuspendedHang Up  The program cannot execute code in the background. -(BOOL) Application: (UIApplication *) application willfinishlaunchingwithoptions: (nsdictionary *) launchOptions
    Tells the agent that the process has started but has not yet entered a state save
    -(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions
    Tells the agent to start the basic Completion program ready to start running
    -(void) Applicationwillresignactive: (uiapplication *) application
    When an application is going into an inactive state, during which time the application does not receive messages or events, such as coming to the phone
    -(void) Applicationdidbecomeactive: (uiapplication *) application
    When the application executes in the active state, this is exactly the opposite of the method above.
    -(void) Applicationdidenterbackground: (uiapplication *) application
    Called when the program is pushed to the background. So to set the background to continue running, you can set it in this function
    -(void) Applicationwillenterforeground: (uiapplication *) application
    Called when the program is going back to the foreground from the background, which is exactly the opposite of the method above.
    -(void) Applicationwillterminate: (uiapplication *) application
    When the program is about to exit is called, it is usually used to save the data and some cleanup work before exiting. This needs to set the key value of the uiapplicationexitsonsuspend.
    -(void) applicationdidfinishlaunching: (uiapplication*) application
    //When the program is loaded and executed
  6. When did the Viewcontroller Alloc,loadview, Viewdidload,viewwillappear,viewdidunload,dealloc, and Init were called? What should be done in these functions when customizing Viewcontroller?

Alloc initializing the current Viewcontroller

Loadview: The nib view page is not being used, and the subclass will create its own custom view layer

Viewdidload: called after attempting to be loaded

viewwillappear: Call when attempting to appear

viewdidunload: Discard > after<ios6 The method is called when the system memory is tight , releasing the current not in window The attempted and corresponding controller shown in the

iOS face question 2

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.