2008-09-19 4 views

उत्तर

70

NSFileManager की fileExistsAtPath:isDirectory: विधि का उपयोग करें। ऐप्पल के दस्तावेज़ here देखें।

+1

बिल्कुल नहीं। क्योंकि आप 'isDirectory' के रूप में बूल के लिए पॉइंटर पास कर रहे हैं। इसका मतलब है कि अगर इस तरह के नाम वाली कोई फ़ाइल है, तो यह विधि YES को वापस लाती है और सूचक ''डायरेक्टर' को 'NO'' लिखती है। – yas375

+1

लेकिन सब कुछ यूनिक्स में एक फ़ाइल है। – uchuugaka

10

[NSFileManager fileExistsAtPath: isDirectory:]

Returns a Boolean value that indicates whether a specified file exists. 

- (BOOL)fileExistsAtPath:(NSString *)path isDirectory:(BOOL *)isDirectory 

Parameters 
path 
The path of a file or directory. If path begins with a tilde (~), it must first be expanded with stringByExpandingTildeInPath, or this method will return NO. 

isDirectory 
Upon return, contains YES if path is a directory or if the final path element is a symbolic link that points to a directory, otherwise contains NO. If path doesn’t exist, the return value is undefined. Pass NULL if you do not need this information. 

Return Value 
YES if there is a file or directory at path, otherwise NO. If path specifies a symbolic link, this method traverses the link and returns YES or NO based on the existence of the file or directory at the link destination. 
+0

पहली पंक्ति उलझन में है कि 'fileExists..' एक क्लास विधि है। कृपया उत्तर को अद्यतन करें। मैं कर सकता था, लेकिन यह बहुत पुराना जवाब है, अगर आप बेहतर होगा। –

13

NSFileManager.h में एप्पल से कुछ अच्छी सलाह फाइल सिस्टम की जाँच के बारे में:

"यह (एक लोड करने की तरह एक ऑपरेशन प्रयास करने के लिए कहीं बेहतर है फ़ाइल या निर्देशिका बनाना) और ऑपरेशन सफल होने के समय से पहले पता लगाने की कोशिश करने के लिए गलती से त्रुटि को संभालना है। फाइल सिस्टम की वर्तमान स्थिति या फाइल सिस्टम पर एक विशेष फ़ाइल के आधार पर व्यवहार की भविष्यवाणी करने का प्रयास करना विषमता को प्रोत्साहित कर रहा है फाइल सिस्टम दौड़ की स्थिति के चेहरे में व्यवहार। "

+0

यह दस्तावेज़ों में भी उल्लेख किया गया है: http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html#//apple_ref/occ/instm/ NSFileManager/fileExistsAtPath: – Monolo

6

एनएसएफइलमेनगर फाइल से संबंधित एपीआई देखने के लिए सबसे अच्छी जगह है। अपने path के रूप में एक NSURL वस्तु है, तो

NSString *pathToFile = @"..."; 
BOOL isDir = NO; 
BOOL isFile = [[NSFileManager defaultManager] fileExistsAtPath:pathToFile isDirectory:&isDir]; 

if(isFile) 
{ 
    //it is a file, process it here how ever you like, check isDir to see if its a directory 
} 
else 
{ 
    //not a file, this is an error, handle it! 
} 
1

, यह पथ का उपयोग करने के लिए इसे NSString में परिवर्तित करने के लिए बेहतर है: आप की आवश्यकता विशिष्ट API - fileExistsAtPath:isDirectory:.

उदाहरण है।

NSFileManager*fm = [NSFileManager defaultManager]; 

NSURL* path = [[[fm URLsForDirectory:NSDocumentDirectory 
          inDomains:NSUserDomainMask] objectAtIndex:0]       
           URLByAppendingPathComponent:@"photos"]; 

NSError *theError = nil; 
if(![fm fileExistsAtPath:[path path]]){ 
    NSLog(@"dir doesn't exists"); 
}else 
    NSLog(@"dir exists"); 
संबंधित मुद्दे