mardi 28 juin 2016

Why does Length not work on a NSString?

I am trying to check the length of string, but I get unrecognized selector when the code is executed and hits the IF statement.

THE VALUE IN THE DICTIONARY IS A STRING.

NSString * checkString = [myDictionary objectForKey:@"somekey"];
    NSLog(@"length: %lu", checkString, (unsigned long)[checkString length]);

if([checkString length]  > 0){

}

ERROR From console:

length: 0
[__NSCFNumber length]: unrecognized selector sent to instance

For simplicity:

 NSString * checkString = @"my string"; //[myDictionary objectForKey:@"somekey"];
    NSLog(@"length: %lu", checkString, (unsigned long)[checkString length]);

    if([checkString length] > 0){
}

length: 9
[__NSCFNumber length]: unrecognized selector sent to instance

Why?

Aucun commentaire:

Enregistrer un commentaire