Detailed IOS interview questions (b) ..

Reprinted from the article: http://www.pythonheidong.com/blog/article/3307/

Previous article lists a total of 32 IOS face questions: http://www.cnblogs.com/fkdd/archive/2012/03/13/2394724.html

Let's start from the first question to answer:

Topic: 1.Object-c class multiple inheritance can it? You can implement multiple interfaces? What Category is? Rewriting a class way of inheritance good or better classification? why?

About multiple inheritance:

First, object-c can not be multiple inheritance, something like the code in this way is absolutely not compile, of course, you can also NSString front. ":" Get rid try again, Oh!

error

Then there is no other way to replace it? Yes, what we call pseudo inheritance, another way we can @protocol commissioned by ios everywhere to achieve.

Pseudo-inheritance

     Although again not objtive-C provide multiple inheritance, it provides another solution, so that the object can be achieved in a response message other classes (in other languages, commonly called the method, both undifferentiated). This solution is called message forwarding, it can make a response message class implemented in another class.

     In general, sent an unrecognized message will generate a runtime error, causing the application to crash, but noted that before the crash, iphone runtime object for each object provides a second chance to process the message. After capturing a message may redirect it to the object can respond to the message.

     This feature is fully implemented by forwarding the message, send a message to an object can not process a selector, the selector will be forwarded to forwardInvocation method of receiving the message object with the selector preserve the original instance of a NSInvocation and parameters are requested. Therefore, we can cover forwardInvocation methods, and to forward the message to another object.

1.1 for message forwarding

   Before adding to the program message forwarding feature, covering two methods, i.e. methodSignatureForSelector: and forwardInvocation :. methodSignatureForSelector: role is to create a valid signature method of another message classes. forwardInvocation: forwards selector to a real object that implements the message.

example:

1.

1 - (NSMethodSignature*)methodSignatureForSelector:(SEL)selector
2 {
3 NSMethodSignature* signature = [super methodSignatureForSelector:selector];
4
5 if (!signature)
6 signature = [self.carInfo methodSignatureForSelector:selector];
7
8 return signature;
9 }

2.

1 - (void)forwardInvocation:(NSInvocation *)invocation
2 {
3 SEL selector = [invocation selector];
4
5 if ([self.carInfo respondsToSelector:selector])
6 {
7 [invocation invokeWithTarget:self.carInfo];
8 }
9 }



 3. Call

. 1 Car * myCar = [Car CAR]; // Car as a class 2 [(NSString *) myCar UTF8String]   // called here the NSString UTF8String side, attention has not been achieved in the method Car

 

Explanation: iphone herein by example to illustrate the development of cheats, self.carInfo is a read-only NSString objects, in the present example Car Car class instance is not correct for another object (NSString) implemented to create a selector a valid signature. When the check is not currently running a valid signature, i.e. entering the object (here myCar) of methodSignatureForSelector: a method in this case, each iteration will be inherited in the dummy method and try to build an effective method signature opportunities such as code, when myCar call UTF8String, lack of access to news from the current object, into a second chance to capture a message, first enter methodSignatureForSelector:. method, using an iterative manner to create an effective method currently being called signature, the signature obtained, into forwardInvocation: its method implemented method (the UTF8String) call forwardInvocation:. first obtained a method call (the UTF8String), determines self.carInfo (a nsstring object) in response to whether the method, If so, you will call UTF8String object goals into self.carInfo object. in this way, we can achieve multiple inheritance, Oh! !

NOTE: If you still have questions, you can visit Apple's official documentation query message forwarding content: 
Address http://www.apple.com.cn/developer/mac/library/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/chapter_6_section_1 # // .html 
apple_ref / DOC / uid / TP40008048-CH105-SW1

2. commission

By means of a delegate @protocol way to achieve, it is also known protocol is the protocol list of a plurality of classes of shared methods, implementation of the methods listed in the protocol does not respond, implemented by others in the IOS. This applause than I want to buy a cell phone, so I have a buyIphone method, but I do not know who that buy a mobile phone, so the release of this demand out (for example, published on the website), if there is to sell the phone businessman (that is to say he can buyIphone realize this method) to see, he will accept my commission, (realization <XXXdelegate> businessman in his class), then I delegate object that points to a merchant .. when I want to buy the phone, directly to him on the line.

E.g:

@protocol MyDelegate
-(void)buyIphone:(NSString *)iphoneType money:(NSString *)money;

@end
@interface My : NSObject
{
id<MyDelegate> deleage;
}
@property(assign,nonatomic)id<MyDelegate> delegate;
@end

Code declares a protocol called Mydelegate, in which there is a buyIphone method, that is, a delegate items. When I want to buy a mobile phone BuyIphone method can simply call by delegate.

as follows:

-(void)willbuy
{
[delegate buyIphone:@"iphone 4s" money:@"4888"];

}

I do not care who the reality of this commission, as long as the delegate implements this class, and buyIphone a proxy statement of method must be implemented, then we must be able to get the results.

For example: Business mankind to achieve this commission (with <Mydelegate> represents the realization)

#import <Foundation/Foundation.h>
#import "My.h"
@interface Business : NSObject<MyDelegate>

@end

Then call the method in @implementation Business in buyIphone

#import  " Business.h " 

@implementation Business

- ( void ) buyIphone: (NSString *) iphoneType Money: (NSString *) Money
{
NSLog ( @ " phones in stock, the price to sell you, shipped in !! " ) ;
}
@end

On ok friends. This is not it also simulates multiple inheritance? By commission, in fact, you will get rid of the things to consider aspects of multiple inheritance, which concern the current class.

Khan, a question was half-written, next to continue. . . . Hand typing sour!





Reprinted from the article: http://www.pythonheidong.com/blog/article/3307/

Guess you like

Origin www.cnblogs.com/xiongbatianxiaskjdskjdksjdskdtuti/p/11350202.html