2010-06-11 4 views
0

J'ai un morceau de code dont on dit qu'il renvoie une valeur booléenne. Je suis un nouveau programmeur, donc quelqu'un pourrait-il me donner un code qui permettra de déterminer si le fichier sur le chemin existe?Comment évaluer les instructions booléennes

NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 

NSString *path = [docsDirectory stringByAppendingPathComponent:@"fileName.txt"]; //returns a bool 

Répondre

1

Actuellement, la méthode stringByAppendingPathComponent: ne renvoie pas (BOOL), elle renvoie (NSString *).

Vous pouvez dire en regardant sa signature qui est:

- (NSString *)stringByAppendingPathComponent:(NSString *)str; 

Si elle a fait retourner un bool (ce qui ne fonctionne pas,) tout ce que vous avez à faire est: if (path) {//...}

Qu'est-ce que vous veut vraiment faire pour tester si un fichier existe est:

NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
NSString *path = [docsDirectory stringByAppendingPathComponent:@"fileName.txt"]; 
if ([[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory]) { 
    //File Exists So Code Goes Here 
} 
0

Si une variable contient une valeur booléenne, il vous suffit de le tester cinque:

if(var) { 
    // gets executed if var is true 
} 

En savoir plus sur conditional control structurs.

Mais stringByAppendingPathComponent ne renvoie pas une valeur booléenne, it returns a string.

Questions connexes