zoukankan      html  css  js  c++  java
  • NSInvocation调用

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

  • 相关阅读:
    AtCoder ABC154 F
    题解 LA4390
    题解 LA4064
    题解 UVa11529
    【题解】洛谷 P6295 有标号 DAG 计数【生成函数 多项式】
    NOIP 2020 自闭记 暨 后期计划
    【CF246E】Blood Cousins Return【dsu on tree】
    【CF208E】Blood Cousins【dsu on tree】
    【CF570D】Tree Requests【dsu on tree】
    dsu on tree 学习笔记
  • 原文地址:https://www.cnblogs.com/chenjunbiao/p/2022197.html
Copyright © 2011-2022 走看看