كيفية التحقق إذا كان المجلد موجود في الكاكاو & الهدف C ؟

StackOverflow https://stackoverflow.com/questions/99395

  •  01-07-2019
  •  | 
  •  

سؤال

كيفية التحقق إذا كان مجلد (دليل) موجودة في الكاكاو باستخدام الهدف C ؟

هل كانت مفيدة؟

المحلول

استخدام NSFileManager's fileExistsAtPath:isDirectory: الأسلوب.ترى أبل مستندات هنا.

نصائح أخرى

نصيحة جيدة من أبل في NSFileManager.ح بشأن التحقق من نظام الملفات:

"إنه أفضل بكثير من محاولة عملية (مثل تحميل ملف أو إنشاء دليل) و التعامل مع الخطأ برشاقة من ذلك هو محاولة معرفة قبل الموعد المحدد ما إذا كانت العملية سوف تنجح.محاولة المسند السلوك بناء على الوضع الحالي من الملفات أو ملف معين على نظام الملفات هو تشجيع السلوك الغريب في مواجهة الملفات شروط السباق."

[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.

NSFileManager هو أفضل مكان للبحث عن ملفات ذات واجهات برمجة التطبيقات.محددة API تحتاج - fileExistsAtPath:isDirectory:.

على سبيل المثال:

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!
}

إذا كان لديك NSURL وجوه path, إنه من الأفضل استخدام الطريق إلى تحويله إلى NSString.

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");
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top