Il metodo NSString initWithFormat: argomenti: accetta un va_list come argomento. Non riesco a capire quando sarà usato (o anche come usarlo). Perché Apple dovrebbe aggiungere un tale metodo quando il normale initWithFormat: è molto più user-friendly?Quando initWithFormat: argomenti: essere usato?
risposta
Non è possibile passare un elenco dinamico di argomenti di formato per -initWithFormat:
. Ad esempio, se volessi implementare lo -stringByAppendingFormat:
senza lo -initWithFormat:arguments:
, avresti un lavoro da svolgere. Con la versione va_list
, è possibile farlo:
- (NSString *)stringByAppendingFormat:(NSString *)format, ... {
va_list args;
va_start(args, format);
NSString * result = [self stringByAppendingString:[NSString stringWithFormat:format arguments:args]];
va_end(args);
return result;
}
Direi senza approfondire che Apple fornisce NSString initWithFormat:
come un metodo di utilità in cima a NSString initWithFormat:arguements:
che significa che la versione breve termina solo chiamando quella più lunga.
C'è anche [NSString stringWithFormat:] che può restituire un NSString autoreleased, salvandoti la chiamata alloc
se non hai bisogno della stringa in giro a lungo.
È utile quando la propria funzione o metodo utilizza argomenti variadici, poiché in tal caso è impossibile utilizzare il metodo vaniglia initWithFormat:
.
Ad esempio, la seguente (inutile) frammento di esempio:
void log(NSString* format, ...)
{
va_list arguments;
va_start(arguments, format);
// impossible:
// NSString* formattedString = [[NSString alloc] initWithFormat: ???];
// possible
va_list argsCopy;
va_copy(argsCopy, arguments);
NSString* formattedString = [[NSString alloc] initWithFormat:format arguments:argsCopy];
// do something cool with your string
NSLog(@"%@", formattedString);
va_end(argsCopy);
va_end(arguments);
}
- 1. Quando deve essere usato assert()?
- 2. Quando dovrebbe essere usato un memoryview?
- 3. Quando dovrebbe essere usato std :: atomic_compare_exchange_strong?
- 4. dovrebbe mai essere usato encodeURI?
- 5. stringWithFormat vs. initWithFormat su NSString
- 6. Che cosa fa '@reify' e quando dovrebbe essere usato?
- 7. Quando deve essere usato in linea in Rust?
- 8. Che cos'è std :: decay e quando dovrebbe essere usato?
- 9. Intestazione E-Mail: Versione MIME: 1.0 - Quando dovrebbe essere usato?
- 10. Quando deve essere usato `DateTime.now.utc` vs.` Time.current.utc` in Rails?
- 11. Cosa fa python sys.intern e quando dovrebbe essere usato?
- 12. Quando "dev-master" dovrebbe essere usato in composit.json?
- 13. Dovrebbe essere sempre usato std :: endl?
- 14. NSInteger dovrebbe essere usato davvero ovunque?
- 15. Per cosa può essere usato std :: remove_extent?
- 16. Dovrebbe essere usato `! Var` o` var == NULL`?
- 17. Quale specificatore di formato dovrebbe essere usato per BOOL?
- 18. Cosa significa: = significa quando usato in pseudocodice?
- 19. Come può essere usato static_cast con l'ereditarietà virtuale?
- 20. Dovrebbe essere usato instancetype sui metodi alloc/new/init?
- 21. Quando deve essere usato Many per le relazioni N: 1 nelle classi di dominio Grails?
- 22. Quando deve essere usato un parametro `out` invece di restituire un tipo complesso?
- 23. Perché dovrebbe essere usato Homebrew per installare Python?
- 24. GWT - dovrebbe essere usato solo per le parti dinamiche?
- 25. Dovrebbe essere usato esattamente IAppBuilder.CreatePerOwinContext <T>?
- 26. Perché l'oggetto istream può essere usato come espressione bool?
- 27. Può una matrice java essere usato come un HashMap chiave
- 28. Perché "big = big.add (..)" deve essere usato per sommare BigIntegers?
- 29. C: Quale carattere deve essere usato per ptrdiff_t in printf?
- 30. Come si suppone che RedirectToRoute debba essere usato?
Grande risposta, grazie. – calvinlough