Valore di ritorno per NSDictionary sul simulatore, iPhone
-
18-09-2019 - |
Domanda
Ho qualche codice che richiede un iPhone per l'esecuzione. Io però voglio mettere alla prova la mia applicazione sul simulatore. Su iPhone Io lo uso per restituire un valore:
return [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:-1], @"number"];
Sto cercando qualcosa di simile penso:
- (NSDictionary *) data
{
#if TARGET_IPHONE_SIMULATOR
something in here to return a value of 70;
#else .....
Voglio restituire un valore di 70 per l'uso nel simulatore.
Qualcuno potrebbe darmi una mano qui?
Soluzione
terminare sempre il +dictionaryWithObjectsAndKeys:
con nil
altrimenti si otterrà incidente casuale. Se c'è solo 1 chiave, è meglio usare +dictionaryWithObject:forKey:
.
Usa #else
.
return [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:
#if TARGET_IPHONE_SIMULATOR
70
#else
-1
#endif
], @"number", nil];
In alternativa, più pulito,
return [NSDictionary dictionaryWithObject:
[NSNumber numberWithInt:(TARGET_IPHONE_SIMULATOR ? 70 : -1)]
forKey:@"number"];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow