jeudi 9 avril 2015

Is there a way to Make NSInvocation surport variable parmas function line [NSstring stringWithFormat:..]

Apple doc says "NSInvocation does not support invocations of methods with either variable numbers of arguments or union arguments. "


i searched for hours ,some people says var_list works, but i tryed ,it does Not


but I think there may be a way to do the same thing (reflection) on variable params function,as i metioned ,[stringWithFormat:],


so , I found a way ,please readt the code bellow .



@interface INTObj : NSObject
@property (nonatomic, assign) int realvalue
@end

@interface FloatObj : NSObject
@property (nonatomic, assign) float realvalue
@end

typedef id (*Obj_Imp)(id,SEL,...);
Method md = class_getClassMethod(obj, selector); // here, obj means NSString
Obj_Imp fun = (Obj_Imp )method_getImplementation(md); // stringWithFormat:...

NSString *arg1 = @"hello %f %d";

FloatObj *fo = [[FloatObj alloc] init];
fo.realvalue = 11.3;

INTObj *io = [[INTObj alloc] init];
io.realvalue = 5;


NSObject *arr[3] = {arg1, fo,io};


// it cracks . exc_bad_Access code = 1
NSString *result = fun(obj , selector, arr[0], [arr[1] realvalue],[arr[2] realvalue]) ;



// it works but i cant do this ,because i don't know the type of argument 1 at Runtime
NSString *result = fun(obj , selector, arr[0],[(FloatObj *)arr[1] realvalue],[arr[2] realvalue])


why does the second calling of the function "fun" works while the first one cracks?


is there a better way to to do this?






Aucun commentaire:

Enregistrer un commentaire