iOS Knowledge Collection

Source: Internet
Author: User

First, the understanding of multi-threaded development and iOS have several ways to implement multithreading?

Benefits:

1, the use of threads can take a long time in the program to put the task in the background to deal with, film, video download;

2, take advantage of multi-core processor, concurrent execution let the system run faster, smoother, better user experience.

Disadvantages:

1, a large number of threads to reduce the readability of code;

2, more threads need more memory space;

3. When multiple threads are competing for the same resource, be aware of thread safety issues.

iOS has three multi-threaded programming techniques:

1, Nsthread (two ways to create) [Nsthread detachnewthreadselector: @selector (dosomething:) totarget:self Withobject:nil]; Nsthread *mythread = [[Nsthread alloc] initwithtarget:self selector: @selector (dosomething:) Object:nil];

[MyThread start];

2, Nsoperationqueue

Nsoperationqueue *oprationqueue = [[Nsoperationqueue alloc] init];

Oprationqueue addoperationwithblock:^{

This block statement chunk executes in a child thread

}

3. Grand Central Dispatch (GCD)

Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{

Time-consuming operations

Dispatch_async (Dispatch_get_main_queue (), ^{

Update interface

});

});

Do not display methods for creating threads: Performselectorinbackground:withobject: Creating a thread with the NSObject class method, [Obj performselectorinbackground:@ Selector (dosomething) Withobject:nil];

What is the difference between thread synchronization and Asynchrony? How does multi-threaded synchronization work in iOS?

Synchronization: A thread waits for the last thread to execute before executing the current thread.

Async: Do two or more things at the same time. Like listening to a song while reading a newspaper.

Implementation: Atomic operations (Atomic), Locking (Nslock, nsrecursive, Nsconditionlock), @synchronized gcd serial queue, GCD barrier, Nsoperationqueue set maximum concurrency of 1

What are the different ways of iOS local data storage? How does iOS implement the storage of complex objects?

Nskeyedarchiver (archive) holds data in the form of an archive that requires adherence to the Nscoding protocol, and that the class corresponding to the object must provide Encodewithcoder: and Initwithcoder: Methods.

1.NSUserDefaults: Used to save application settings and properties, user-saved data. The data still exists after the user opens the program again or after powering it on. The types of data that Nsuserdefaults can store include: NSData, NSString, NSNumber, NSDate, Nsarray, Nsdictionary.

2.Write write mode: Permanently saved on disk.

3.SQLite (FMDB, CoreData)

Nscoding + Nskeyedarchiver implements the storage of complex objects.

Iv. how to achieve a singleton class?

The first way

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;

}

The 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;

}

V. What is safe release?

Position the pointer to nil after the object release.

Vi. brief description of your understanding of UIView, UIWindow and Calayer

UIView inherits from Uiresponder, Uiresponder inherits from Nsobject,uiview and can respond to user events. Calayer inherits from NSObject, so Calayer cannot respond to events. UIView build interface, UIView focus on the management of content, Calayer focus on the content of the drawing. UIView is used to display content, can handle user events, Calayer is used to draw content, to animate content, to rely on and uiview to display, can not handle user events.

Seven, what is protocol? What is an agent? Write a delegate's interface? What attributes are used for the property declaration of the delegate? Why?

The protocol provides a set of methods, but is not responsible for implementation, if a class follows a protocol, and implements the protocol inside the method, then we call this class is to follow the agent of a certain protocol. The Declaration of a property uses assign to prevent the issue of circular references.

Viii. What is the description category (categories) and extension (extensions) respectively? And the difference between the two? What is the difference between inheritance and category in implementation? Why can category only add methods to objects, but cannot add member variables?

Category Category: The extension method for a class without knowing the source code;

Extension: Declares private methods and variables for a class. Inheritance creates a new class, and the class is just an extension of the class, or the previous class. The purpose of a class is to add a method to a known class.

Nine, objective-c have private methods? What about private variables? If not, is there any alternative?

There are only two methods in the Objective-c– class, static methods and instance methods.

@private to decorate a private variable

All instance variables in OC are private by default, and all instance methods are public by default.

What does the minus sign and the plus sign in OBJC mean?

+ static method, also called class method,-instance method

Xi. for statements nsstring* testobject = [[NSData alloc] Init];testobject What types of objects are at compile time and run time respectively?

Compile time is nsstring type, when run is NSData type

12. How is the interaction between all objects implemented in OC?

function pointer implementation

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

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

2, in general, import objective C header file with #import, including C + + header file with # include.

3, #import determine that a file can only be imported once, which allows you to have no problem in the recursive inclusion.

4. #import<> contains classes in the iOS Framework class library, #import "" contains the custom classes in the project.

5. The advantage of #import比起 # include is that it avoids the problem of repeated references. So in OC we basically use import.

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

The @protected of a class instance (member) variable, @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.

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

1. No * number

2. Dynamic Data type

3, can point to any class of objects (set is nil), and do not care about its specific type

4. Check its specific type at run time

5. You can send any (existing) message to it

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

The principles of memory management are: Who creates, who releases, who references, who manages.

Follow the cocoa touch usage principles;

Memory management mainly to avoid "premature release" and "Memory Leak", for "premature release" need to pay attention to the @property setting characteristics, must use the keyword to the attribute, for "memory leak", must apply for the release, be careful.

1. When you create an object using the new, alloc, or Copy method, the object reference counter is 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.

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

3, if retain an object, need to release or autorelease the object, keep retain method and release method use the same number of times.

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

18. What is the function of attribute readwrite,readonly,assign,retain,copy,nonatomic, in that case?

Assign for simple data types, such as Nsinteger,double,bool;

Retain and copy are used for objects;

ReadWrite is a readable and writable feature; When getter methods and setter methods need to be generated;

ReadOnly is a read-only feature that only generates getter methods, does not generate setter methods, and does not want attributes to change outside of the class;

Assign is an assignment attribute, the setter method assigns the passed-in parameter to the instance variable, and only when the variable is set;

Retain represents the holding characteristics, setter method will pass in the parameters of the first reservation, and then assign value, the Retaincount of the passed parameter will be +1;

Copy represents an assignment attribute, and the setter method copies a copy of the incoming object; When a completely new variable is required;

Nonatomic non-atomic operations, determines whether the compiler-generated setter getter is an atomic operation, atomic represents multithreading security, and generally uses nonatomic.

19, Objective-c How to memory management, say your views and solutions?

OBJECTIVE-C memory management has three main ways of Arc (automatic memory count), manual memory count, memory pool.

20. The objects generated by those keywords need to be released manually?

The Alloc or new object generated by the keyword needs to be released manually

21. How to perform performance testing on iOS devices?

profile-> Instruments->time Profiler

22. What is category? Is it better to rewrite a class in a way that inherits or classifies? Why?

Category is the class, the general situation with good classification, with category to rewrite the method of the class, only valid for this category, will not affect the other classes and the original class relationship.

23. What is the method for creating threads in Object C? If you execute code in the main thread, what is the method? What if you want to delay the execution of code and methods?

There are three ways to create a thread: Use Nsthread to create, use GCD dispatch, use a subclass nsoperation, and then add it to Nsoperationqueue; Execute code on the main thread by Performselectoronmainthread, if you want to delay the execution of code can be used PerformSelector:onThread:withObject:waitUntilDone

24. What is notification? When to use delegate, when to use notification?

Observer mode, the controller adds his own notification to Defaultnotificationcenter, and other classes register this notification to be notified, These classes can do their own operations when receiving notifications (the observers are notified by default in random order by multiple observers, and each observer waits until one of the current observers has been completed before his turn can be used to arrange the observer's reaction sequence in a notificationqueue manner, You can also set the reflection time in the Add observer, and cancel the observation needs to be written off in both Viewdidunload and Dealloc.

Delegate for one-to-one relationships, and reciever can return a value to Sender,notification can be for One-to-one/many/none, Reciever cannot return a value to sender. Therefore, delegate is used by sender to receive a function feedback value to reciever, notification is used to notify multiple object of an event.

iOS Knowledge Collection

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.