iOS 面试题汇总(三)

1.Difference between categories and extensions?

A:There are three difference beween categories and extensions:

  1. The extension has no name;
  2. The implementation of the extension must be in the main @implementation block of the file;
  3. The extension can easy declare property that need backend instance variable.

Reference:Difference between Category and Class Extension?

2.What is advantage of category?

A:

  • You can add method to existing class even to that class whose source is not available to you.
  • You can extend functionality of a class without subclassing.
  • You can split implementation in multiple files.

3.What are KVO and KVC?

A:

Key-value observing is a mechanism that enables an object to be notified directly when a property of another object changes.

Key-value coding is a mechanism for indirectly accessing an object’s attributes and relationships using string identifiers.

Reference:Cocoa Core Competencies

4.Difference between shallow copy and deep copy?

A:

  • shallow copy reference origin object, deep copy create a new object through clone origin object
  • deep copy is more complex than shallow copy
  • deep copy is more expensive

5.When retain count increase?

A:

  • When you create an object;
  • When you send an object retain message;

Reference:Conditions in which retainCount increases or decreases

6.If we don’t create any autorelease pool in our application then is there any autorelease pool already provided to us?

A: It depends on case, if we create app base on an UI framework, there is an autorelease pool already provided to us; otherwise, we should create one by ourself.

7.When you will create an autorelease pool in your application?

A:There are three occasions when you might use your own autorelease pool blocks:

  • If you are writing a program that is not based on a UI framework, such as a command-line tool.
  • If you write a loop that creates many temporary objects. You may use an autorelease pool block inside the loop to dispose of those objects before the next iteration. Using an autorelease pool block in the loop helps to reduce the maximum memory footprint of the application.

  • If you spawn a secondary thread. You must create your own autorelease pool block as soon as the thread begins executing; otherwise, your application will leak objects.

8.How many autorelease pool you can create in your application? Is there any limit?

A: You can create autorelease pool as much as possible, there is no limit.

9.What is purpose of delegates?

A:The purpose of delegates is archive the same reuse ability of subclass via composite objects.

委托是一种组合方法,它使组合具有与继承同样的复用能力。在委托方式下,有两个对象参与处理一个请求,接受请求的对象将操作委托给它的代理者(delegate),它类似于子类将请求交给它的父类处理。使用继承时,被继承的操作总能引用接受请求的对象。 –设计模式

10.What is difference between NSNotification and protocol?

A:

  • Protocol is To-one Relationships; NSNotification is To-many Relationships.
  • Protocol support bidirectional data-transfer between two classes.
  • Protocol doesn’t need the third object to coordinate the communication.

11.Polymorphism?

A: The ability of different objects to respond, each in its own way, to identical messages is called polymorphism.

12.Singleton?

A:A singleton class returns the same instance no matter how many times an application requests it.

13.Give us example of what are delegate methods and what are data source methods of UITableView?

A:

1
2
3
4
5
6
7
8
// Delegate methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
-
 // Data source methods
 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
 - (NSInteger)tableView:(UITalbleView *)tableView numberOfRowsInSections:(NSInteger)section;
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

14.What are mutable and immutable types in Objective C?

A:A mutable object can be mutated or changed. An immutable object cannot.

Reference:what is difference between mutable and immutable

15.When to use NSMutableArray and when to use NSArray?

A:When collection will mutable you should use NSMutableArray, otherwise use NSArray.

16.What is convenience constructor?

A:A convenience constructor is one that performs object allocation & initialization in one step & returns an autoreleased object to the caller.

Reference:Objective-C (programming language): What is a convenience constructor

17.What is responder chain?

A:The responder chain is a series of linked responder objects.

18.What is push notification?

A:Apple Push Notification service (APNs for short) is the centerpiece of the remote notifications feature. It is a robust and highly efficient service for propagating information to iOS and OS X devices. Each device establishes an accredited and encrypted IP connection with the service and receives notifications over this persistent connection. If a notification for an app arrives when that app is not running, the device alerts the user that the app has data waiting for it.