天天看點

使用NSInvocation 調用執行個體的方法,且等待獲得傳回值

  1.     OBJCExternInterface * ibjInter = [[OBJCExternInterface alloc] init]; //定義類的執行個體
  2.     SEL theSelector;
  3.     NSMethodSignature *aSignature;
  4.     NSInvocation *anInvocation;
  5.     theSelector = @selector(ReadFromFile:); //選擇一個方法
  6.     aSignature = [OBJCExternInterface instanceMethodSignatureForSelector:theSelector]; //得到類和方法的方法信号
  7.     anInvocation = [NSInvocation invocationWithMethodSignature:aSignature]; //調用方法信号
  8.     [anInvocation setSelector:theSelector]; // 設立方法
  9.     [anInvocation setTarget:ibjInter];  //設定目的執行個體
  10.     [anInvocation setArgument:&arrParameters atIndex:2];//設定參數(1個參數的方法 index 為2,1為隐藏參數),可設定多個參數
  11.     id result;
  12.     [anInvocation invoke]; //調用方法
  13.     [anInvocation getReturnValue:&result]; //完成調用設定調用傳回值