2012-06-19 1 views
3
if ([dict objectForKey:@"photo"] !=(id)[NSNull null]) 
    {NSLOG(@"dictinary is not empty")} 

Cela ne fonctionne pas pour moi. pour vérifier balise videiPhone Check dictionnaire vide

+0

La clé photo est une chaîne ou une valeur numérique? – Dinesh

+0

tag est dans la chaîne – Tiger

+0

c'est que je reçois photo = ""; – Tiger

Répondre

27

Utilisez count.

Par exemple:

if ([dict count] == 0) { 
    NSLog("empty"); 
} 

Si vous voulez vérifier une clé alors:

if([dict objectForKey:@"photo"]) { 
    NSLog(@"There's an object in photo"); 
} 
0

Si la objectForKey:@"photo" est nulle quand pas présent, vous pouvez simplement faire: if ([dictionary objectForKey:@"photo"]) {NSLog(@"dictionary is not empty);}

+0

ce n'est pas un moyen correct de vérifier – Tiger

+2

Oui, c'est. C'est une bonne façon de faire une instruction 'if'. –

+0

Oui. Il est en effet. – san

0

essayer ce code pour vérifier est la valeur nulle ou non

NSString *value=[dict objectForKey:@"photo"]; 

if (value.length != 0) 
{ 
    NSLog("not empty!"); 
} 
else 
{ 
    NSLog("empty!"); 
} 
+0

Ha! J'aime l'évitement flagrant d'une comparaison à «nul»! – CodaFi

0

J'ai pris dans la chaîne comme:

NSString *strPhoto=[dict objectForKey:@"photo"]; 

if (strPhoto ==(id)[NSNull null] || [strPhoto isEqualToString:@""] || strPhoto==nil || [strPhoto length]==0) { 
} 

Il travaille pour moi !!!!!!!!!!

2

Cela fonctionne pour moi. Vous pouvez gérer Null avec ce code.

[dict isKindOfClass:[NSNull class]]; 
Questions connexes