2013-01-21 10 views
9

Ho un NSMutableArray (_theListOfAllQuestions) che sto popolando con i numeri da un file. Poi ho confrontato gli oggetti di quell'array con qNr (NSString) e ho ricevuto un errore. Ho persino lanciato l'array su un altro NSString, _checkQuestions, solo per essere sicuro di confrontare NSStrings. Ho provato a usare l'oggetto per confrontare anche.Perché ottengo un errore nel confrontare NSString? (- [__ NSCFNumber isEqualToString:]: selettore non riconosciuto inviato all'istanza)

-(void)read_A_Question:(NSString *)qNr { 
NSLog(@"read_A_Question: %@", qNr); 
int counter = 0; 
for (NSString *item in _theListOfAllQuestions) { 
    NSLog(@"item: %@", item); 
    _checkQuestions = _theListOfAllQuestions[counter]; //_checkQuestion = NSString 
    NSLog(@"_checkQuestions: %@", _checkQuestions); 
    if ([_checkQuestions isEqualToString:qNr]) { 
     NSLog(@">>HIT<<"); 
     exit(0); //Just for the testing 
    } 
    counter++; 
} 

Quando si esegue questo codice ottengo il seguente NSLog:

read_A_Question: 421 
item: 1193 
_checkQuestions: 1193 

... ed errori:

-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x9246d80 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x9246d80'

Credo che ho ancora il confronto NSString con un numero di qualche sorta ma a me sembra che sto confrontando NSString vs NSString?

Potrei davvero bisogno di aiuto qui per 1) capire il problema, 2) risolvere il problema?

+0

Ebbene '_checkQuestions', in almeno un caso, è un 'NSNumber' e non un' NSString' . Cosa si trova esattamente in '_theListOfAllQuestions'? E come viene popolato? –

risposta

15

Sostituire questa linea

if ([_checkQuestions isEqualToString:qNr]) 

con

if ([[NSString stringWithFormat:@"%@",_checkQuestions] isEqualToString:[NSString stringWithFormat:@"%@",qNr]]) 

Spero che ti aiuta ..

+2

Non c'è bisogno di 'stringWithFormat' come' NSNumber' ha già una proprietà '-stringValue' che restituisce il numero come una stringa. –

+0

@yulz Non conosco il tipo di dati di _checkQuestions e non c'è nulla di male nell'usare stringWithFormat .. –

+0

@Praatek, che l'ha reso GRAZIE. – PeterK

2

L'array _theListOfAllQuestions ha oggetti NSNumber e non NSString oggetti. Quindi non puoi usare direttamente isEqualToString.

Prova questo,

for (NSString *item in _theListOfAllQuestions) { 
    NSLog(@"item: %@", item); 
    _checkQuestions = _theListOfAllQuestions[counter]; //_checkQuestion = NSString 
    NSLog(@"_checkQuestions: %@", _checkQuestions); 
    if ([[_checkQuestions stringValue] isEqualToString:qNr]) { 
     NSLog(@">>HIT<<"); 
     exit(0); //Just for the testing 
    } 
    counter++; 
} 
+0

ABC, lo ha provato anche in precedenza e ha ottenuto "Nessuna interfaccia @ visibile per 'NSString' dichiara il selettore 'stringValue'" – PeterK

+0

Questo perché si dovrebbe dichiarare '_checkQuestions' come NSNumber e non NSSString. Il tuo array ha NSNumbers e non stringhe. L'uso di '[NSString stringWithFormat: @"% @ ", _ checkQuestions]' non è raccomandato in questo caso. Ciò darà risultati inaspettati in seguito. – iDev

+1

Imposta _checkQuestions su 'NSNumber'. Puoi digitare cast '[(NSNumber *) _ checkQuestions stringValue]' nella tua istruzione if o modificare il tipo in cui hai dichiarato _checkQuestions a NSNumber –

Problemi correlati