第16月第8天 NSInvocation存储 函数指针 va_arg lldb

时间:2023-03-09 03:58:54
第16月第8天 NSInvocation存储 函数指针 va_arg lldb

1.NSInvocation存储

-(void)setInvok:(id)target sel:(SEL)sel key:(id)key
{
if(!target) return;
NSMethodSignature *sig=[target methodSignatureForSelector:sel];
NSInvocation *invo=[NSInvocation invocationWithMethodSignature:sig];
[invo setTarget:target];
[invo setSelector:sel]; NSDictionary *dict = [self currentStyleDict];
[dict setValue:invo forKey:key];//以key的形式保存invo
} -(NSInvocation*)selectInvok:(NSString *)key style:(PALiveStyle)style
{
NSDictionary *dict = [self getStyleDict:style];
return [dict valueForKey:key];
}

http://blog.sina.com.cn/s/blog_4beb28f30100qask.html

2.函数指针

//常规写法
switch ( oper ){
case ADD:
result = add( oper1, oper2 );
break;
case SUB:
result = sub( oper1, oper2 );
break;
case MUL:
result = mul( oper1, oper2 );
break;
case DIV:
result = div( oper1, oper2 );
break;
...
}
//使用转移表之后的写法
double add( double, double );
double sub( double, double );
double mul( double, double );
double div( double, double );
...
double (*oper_func[])( double, double ) = {
add, sub, mul, div, ...
};
//下面的语句替换前面整条switch语句
result = oper_func[ oper ]( op1, op2 );

https://segmentfault.com/a/1190000000578532

3.runtime

http://justsee.iteye.com/blog/2163777

4.调用是arg后记得传nil

- (bool)startPlayingStream:(NSString *)streamID inView:(UIView*)view
{
// NSInvocation *invo=[[MessageInvok shareInvok] selectInvok:@"start_playing_stream" style:PALiveStyleNormal];
// if(invo)
// {
//// [data retain];
// [invo setArgument:&streamID atIndex:2]; //设置参数
// [invo setArgument:&view atIndex:3]; //设置参数
// [invo invoke]; //调用
// }
// [self invokeMethod:@"start_playing_stream" style:PALiveStyleNormal,streamID,view];
[self invokeMethod:@"start_playing_stream" style:PALiveStyleNormal arg:streamID,view, nil]; return true;
} - (bool)stopPlayingStream:(NSString *)streamID
{ return true;
} -(NSInvocation *)invokeMethod:(NSString *)method style:(PALiveStyle)style arg:(id)arg, ...
{
NSMutableArray *arrayList = [NSMutableArray array]; id eachItem;
va_list argumentList;
if (arg)
{
[arrayList addObject: arg];
va_start(argumentList, arg);
while((eachItem = va_arg(argumentList, id)))
{
[arrayList addObject: eachItem];
}
va_end(argumentList);
} NSInvocation *invo=[[MessageInvok shareInvok] selectInvok:@"start_playing_stream" style:style];
if(invo)
{
for (NSUInteger i =; i<arrayList.count; i++) {
NSInteger idx = i+;
id data = arrayList[i];
[invo setArgument:&data atIndex:idx];
} [invo invoke]; //调用
} return invo;
}

5.lldb e

e aView.backgroundColor = [UIColor greenColor];

http://yulingtianxia.com/blog/2015/11/13/Summary-of-the-first-month-in-the-internship-of-Tencent/

https://github.com/opensource-apple/objc4/blob/master/runtime/Messengers.subproj/objc-msg-x86_64.s