iPhone - met behulp van NSInvocation: konstante waarde
-
18-09-2019 - |
Vra
Ek hantering van 'n ou iPhone OS 2.x projek en ek wil verenigbaarheid te hou, terwyl die ontwerp vir 3.x.
Ek gebruik NSInvocation, is 'n kode soos hierdie
NSInvocation* invoc = [NSInvocation invocationWithMethodSignature:
[cell methodSignatureForSelector:
@selector(initWithStyle:reuseIdentifier:)]];
[invoc setTarget:cell];
[invoc setSelector:@selector(initWithStyle:reuseIdentifier:)];
int arg2 = UITableViewCellStyleDefault; //????
[invoc setArgument:&arg2 atIndex:2];
[invoc setArgument:&identificadorNormal atIndex:3];
[invoc invoke];
'n kode in 'n manier dat beide 3.0 en 2.0 soos, elkeen met behulp van sy behoorlike sintaksis het.
Ek het 'n probleem op die lyn wat ek gemerk met vraagtekens.
Die probleem daar is dat ek probeer om te wys aan ARG2, 'n konstante wat nog nie gedefinieer in OS 2.0. As alles met NSInvocation is om dinge te doen indirek aan samesteller foute te vermy, hoe doen ek dit konstant 'n veranderlike stel in 'n indirekte wyse? 'N soort van performSelector "toewys waarde aan veranderlike" ...
is dit moontlik? dankie vir enige hulp.
Oplossing
UITableViewCellStyleDefault
word gedefinieer as 0
sodat jy 0
kan gebruik waar jy normaalweg UITableViewCellStyleDefault
sou gebruik. Ook, daar is geen rede om 'n NSInvocation gebruik, sal dit te doen:
UITableViewCell *cell = [UITableViewCell alloc];
if ([cell respondsToSelector:@selector(initWithStyle:reuseIdentifier:)])
cell = [(id)cell initWithStyle:0 reuseIdentifier:reuseIdentifier];
else
cell = [cell initWithFrame:CGRectZero reuseIdentifier:reuseIdentifier];
-[UITableViewCell initWithFrame:reuseIdentifier:]
sal nog werk op 3.x, dit is net afgekeur.
Ander wenke
NSInvocation* invoc = [NSInvocation invocationWithMethodSignature:
[cell methodSignatureForSelector:
@selector(initWithStyle:reuseIdentifier:)]];
[invoc setTarget:cell];
[invoc setSelector:@selector(initWithStyle:reuseIdentifier:)];
int arg2;
#if (__IPHONE_3_0)
arg2 = UITableViewCellStyleDefault;
#else
//add 2.0 related constant here
#endif
[invoc setArgument:&arg2 atIndex:2];
[invoc setArgument:&identificadorNormal atIndex:3];
[invoc invoke];
#if (__IPHONE_3_0)
arg2 = UITableViewCellStyleDefault;
#endif