0
回答
Object_c使用技巧
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

1.判断对像是否实现了某方法。

//.h
#import <Foundation/Foundation.h>

@interface Tester : NSObject {
    
}
-(void) test:(NSString*) msg;
-(void) notImp;
@end
//.m
#import "Tester.h"
@implementation Tester
-(void) test:(NSString*) msg
{
    NSLog(@"%@", msg);
}
@end
//main
#import <Foundation/Foundation.h>
#import "Tester.h"
int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    id tester = [[Tester alloc] init];//注意,这里使用id
    
    SEL testSelector = @selector(test:);
    SEL notImpSelector = @selector(notImp:);
    
    if([tester respondsToSelector:testSelector])
    {
        //tester.m中实现了test方法
        [tester test:@"invoke test method"];
    }
    if([tester respondsToSelector:notImpSelector])
    {
        //test.m中没有实现此主就去
        [tester notImp];
    }
    
    [pool drain];
    return 0;
}
 

 

2.获取函数指针

Tester.h
#import <Foundation/Foundation.h>

@interface Tester : NSObject {
    
}
-(void) Test:(NSString*) msg;
@end
 
Tester.m
#import "Tester.h"

@implementation Tester
-(void) Test:(NSString*) msg
{
    NSLog(@"%@", msg);
        
}

@end
main.m
#import <Foundation/Foundation.h>
#import "Tester.h"
int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    id tester = [[Tester alloc] init];
    
    SEL testSelector = @selector(Test:); //构造选择器
    IMP testIMP = [tester methodForSelector: testSelector];//获取函数的指针
    
    for(int i=0;i<10;i++)
    {
        //int to NSString
        //直接使用指针调用对像函数
        testIMP(tester,testSelector, [NSString stringWithFormat:@"%d",i]);
    }
                      
    [pool drain]; 
    return 0;
}
 

 

3.将函数调用封装成对象

CurrentDate.h
#import <Foundation/Foundation.h>

@interface CurrentDate : NSObject {
    
}
- (NSString *) stringForDate: (NSDate *)date 
              usingFormatter: (NSDateFormatter *)formatter;
@end
CurrentDate.m
#import "CurrentDate.h"

@implementation CurrentDate
- (NSString *) stringForDate: (NSDate *)date 
              usingFormatter: (NSDateFormatter *)formatter
{
    return [formatter stringFromDate: date];
}
@end
main.m
#import <Foundation/Foundation.h>
#import "CurrentDate.h"
//参考:http://theocacao.com/document.page/264
int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    //原始调用
    NSDateFormatter * dateFormat = [[NSDateFormatter alloc] 
                                    initWithDateFormat:@"%b %d %Y" 
                                    allowNaturalLanguage: NO];
    CurrentDate * currentDateClassObject = [[CurrentDate alloc] init];
    NSString * currentDate = [currentDateClassObject 
                              stringForDate: [NSDate date] 
                              usingFormatter: dateFormat];
    NSLog(@"currentDate: %@", currentDate);
    
    
    //NSInvocation调用
    SEL mySelector = @selector(stringForDate:usingFormatter:);
    NSMethodSignature * sig = [[currentDateClassObject class] 
                               instanceMethodSignatureForSelector: mySelector];
    
    NSInvocation * myInvocation = [NSInvocation invocationWithMethodSignature: sig];
    [myInvocation setTarget: currentDateClassObject];
    [myInvocation setSelector: mySelector];
    
    NSDate * myDate = [NSDate date];
    [myInvocation setArgument: &myDate atIndex: 2];
    
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle: NSDateFormatterMediumStyle];    
    [myInvocation setArgument: &dateFormatter atIndex: 3];
    
    NSString * result = nil;    
    [myInvocation retainArguments];    
    [myInvocation invoke];
    [myInvocation getReturnValue: &result];
    NSLog(@"The result is: %@", result);
    
    [pool drain];
    return 0;
}
 


原文链接:http://blog.csdn.net/favormm/article/details/6346861
<无标签>
举报
长平狐
发帖于5年前 0回/70阅
顶部