Come & # 8220; trasmettere & # 8221; un numero variabile di argomenti per + stringWithFormat di NSString:
-
07-07-2019 - |
Domanda
Vorrei scrivere una funzione in Objective-C come quella qui sotto, che accetta un numero variabile di argomenti e li passa su + stringWithFormat:
. Conosco vsnprintf
, ma ciò implicherebbe la conversione del 'formato' NSString in C e viceversa (e significherebbe anche convertire anche i segnaposto di formattazione al suo interno ...).
Il codice seguente viene compilato, ma ovviamente non si comporta come voglio :)
NSString *estr(NSString *format, ...) {
va_list args;
va_start(args, format);
NSString *s = [NSString stringWithFormat:format, args];
va_end(args);
return s;
}
Fondamentalmente: esiste una versione va_list
del metodo + stringWithFormat:
o è possibile scriverne una?
Soluzione
NSString *estr(NSString *format, ...) {
va_list args;
va_start(args, format);
NSString *s = [[[NSString alloc] initWithFormat:format arguments:args] autorelease];
va_end(args);
return s;
}
non sembrano avere un costruttore di convenienza " stringWith ... " Versione
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow