سؤال

يحتوي تطبيق iPad الخاص بي على منشأة تنزيل صغيرة ، وأريد إلحاق البيانات باستخدام NSFileHandle. المشكلة هي أن مكالمة الإنشاء تُرجع فقط مقابض ملف فارغة. ماذا ممكن ان تكون المشكلة؟ فيما يلي الأسطر الثلاثة من التعليمات البرمجية التي من المفترض أن تنشئ مقبض الملف الخاص بي:

NSString *applicationDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
self.finalPath = [applicationDocumentsDirectory stringByAppendingPathComponent: self.fileName]; 
NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath];

راجعت مسار الملف ، ولم أرى أي خطأ.

تايا

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

المحلول

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

 NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath];
 if(output == nil) {
      [[NSFileManager defaultManager] createFileAtPath:self.finalPath contents:nil attributes:nil];
      output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath];
 }

إذا كنت ترغب في إلحاق الملف إذا كان موجودًا بالفعل ، فاستخدم شيئًا مثل [output seekToEndOfFile]. سيبدو الرمز الكامل الخاص بك على النحو التالي:

 NSString *applicationDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
 self.finalPath = [applicationDocumentsDirectory stringByAppendingPathComponent: self.fileName]; 
 NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath];
 if(output == nil) {
      [[NSFileManager defaultManager] createFileAtPath:self.finalPath contents:nil attributes:nil];
      output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath];
 } else {
      [output seekToEndOfFile];
 }

نصائح أخرى

احصل على مسار دليل المستندات

+(NSURL *)getDocumentsDirectoryPath
{
    return [[[NSFileManager defaultManager]URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]lastObject];
}

حفظ النص إلى نهاية الملف

إذا لم يكن الملف موجودًا ، قم بإنشائه واكتب البيانات

+(void)saveText:(NSString *)textTobeSaved atPath:(NSString*)fileName
{
    NSString *filePath = [NSString stringWithFormat:@"%@.text",fileName];

    NSString *path = [[self getDocumentsDirectoryPath].path
                      stringByAppendingPathComponent:filePath];
    NSFileHandle *fileHandler = [NSFileHandle fileHandleForWritingAtPath:path];
    if(fileHandler == nil) {
        [[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil];
        fileHandler = [NSFileHandle fileHandleForWritingAtPath:path];
    } else {
        textTobeSaved = [NSString stringWithFormat:@"\n-----------------------\n %@",textTobeSaved];
        [fileHandler seekToEndOfFile];
    }

    [fileHandler writeData:[textTobeSaved dataUsingEncoding:NSUTF8StringEncoding]];
    [fileHandler closeFile];
}

احصل على نص من الملف مع اسم الملف المحدد

+(NSString *)getTextFromFilePath:(NSString *)fileName
{
    NSString *filePath = [NSString stringWithFormat:@"%@.text",fileName];

    NSString *path = [[self getDocumentsDirectoryPath].path
                      stringByAppendingPathComponent:filePath];
    NSLog(@"%@",path);
    if(path!=nil)
    {
    NSString *savedString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

    return savedString;
  }else{
    return @"";
  } 
}

حذف ملف

+(void)deleteFile:(NSString *)fileName
{
    NSString *filePath = [NSString stringWithFormat:@"%@.text",fileName];

    NSString *path = [[self getDocumentsDirectoryPath].path
                      stringByAppendingPathComponent:filePath];

    NSFileHandle *fileHandler = [NSFileHandle fileHandleForWritingAtPath:path];
    if(fileHandler != nil) {
        [[NSFileManager defaultManager]removeItemAtPath:path error:nil];
    }

}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top