无法在IOS中创建plist文件

无法在IOS中创建plist文件,ios,objective-c,plist,Ios,Objective C,Plist,在我的应用程序中,我需要将数据存储在一个plist文件中,以便在另一个UIViewController中使用,并在应用程序中保留一个很小的数据库。我这样做: 我获得了需要存储在此文件中的信息 我在NSDictionary 我将NSDictionary插入了NSMutableArray 现在,我创建了一个实用程序类,用于处理与文件相关的工作 但它不会在Documents目录中创建plist文件。这是我的密码: ViewController.m - (void)saveActivity {

在我的应用程序中,我需要将数据存储在一个plist文件中,以便在另一个
UIViewController
中使用,并在应用程序中保留一个很小的数据库。我这样做:

  • 我获得了需要存储在此文件中的信息
  • 我在
    NSDictionary
  • 我将
    NSDictionary
    插入了
    NSMutableArray
  • 现在,我创建了一个实用程序类,用于处理与文件相关的工作 但它不会在Documents目录中创建plist文件。这是我的密码:
  • ViewController.m

    - (void)saveActivity {
        if ([FileHander plistExist]) {
            NSMutableArray *dataFromFile = [[NSMutableArray alloc]init];
            dataFromFile = [FileHander loadDataFromFile];
            if (![[dataFromFile lastObject]objectForKey:@"type"]) {
                [FileHander saveFileWithArray:arrayActivity];
            } else {
                [dataFromFile addObject:arrayActivity];
                [FileHander saveFileWithArray:dataFromFile];
            }
        } else {
            NSLog(@"%@", arrayActivity);
            [FileHander saveFileWithArray:arrayActivity];
        }
    }
    
    #import "FileHander.h"
    
    @implementation FileHander
    
    + (BOOL) plistExist {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDir = [paths objectAtIndex:0];
        NSString *path = [documentsDir stringByAppendingPathComponent:@"activity.plist"];
        return [[NSFileManager defaultManager]fileExistsAtPath:path];
    }
    
    + (NSMutableArray *) loadDataFromFile {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDir = [paths objectAtIndex:0];
        NSString *path = [documentsDir stringByAppendingPathComponent:@"activity.plist"];
        NSMutableArray *dataFound = [NSArray arrayWithContentsOfFile:path];
        return dataFound;
    }
    
    + (BOOL) deletePlist {
        NSFileManager *manager = [NSFileManager defaultManager];
        NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString *filePath = [documentsPath stringByAppendingPathComponent:@"activity.plist"];
        NSError *error;
        BOOL success =[manager removeItemAtPath:filePath error:&error];
        if (success) {
            return YES;
        }
        else
        {
            return NO;
        }
    }
    
    + (void) saveFileWithArray: (NSMutableArray*)activityArray {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"activity.plist"];
        [activityArray writeToFile:fileName atomically:YES];
    }
    
    @end
    
    arrayActivity
    包含以下内容(坐标、速度、时间等):

    当我执行此应用程序时,它无法在设备上创建plist文件(我使用Mac应用程序iExplorer查找此文件),我的代码中有什么错误? 我用同样的代码开发了另一个应用程序,效果非常好。 我希望你能帮我解决这个问题。
    谢谢

    请尝试下面的代码来创建plist文件。。我试过了,效果很好

    使用NSARRAY创建文件:

    - (void) saveFileWithArray: (NSMutableArray*)activityArray {  
        NSData * data = [NSKeyedArchiver archivedDataWithRootObject:activityArray];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docDir = [paths objectAtIndex:0];
    
        NSString *Path =[NSString stringWithFormat:@"%@/%@.plist",docDir,@"isLogin"];
        NSLog(@"-->%@",Path);        // PATH OF YOUR PLIST FILE
        BOOL didWriteSuccessfull = [data writeToFile:Path atomically:YES];
           if (didWriteSuccessfull) {
               NSLog(@"store succsessfully");
              }
         else {
               NSLog(@"Error in Storing");
        } 
    }
    
    从文件获取NSARRAY:

    - (NSMutableArray *) loadDataFromFile {
       NSArray *myArray=[[NSArray alloc]init];
       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
       NSString *docDir = [paths objectAtIndex:0];
    
       NSString *Path =[NSString stringWithFormat:@"%@/%@.plist",docDir,@"isLogin"];
    
       if ([[NSFileManager defaultManager] fileExistsAtPath:Path])
           {
              NSLog(@"exist");
              myArray=[[NSArray alloc]initWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:[NSData dataWithContentsOfFile:Path]]];
              return [NSMutableArray arrayWithArray:myArray];
           }
            else{
                NSLog(@"not exist");
                return nil;
           }
     }
    

    我建议检查
    writeToFile:atomically:
    的返回值,并打印出您正在发送的路径(
    NSLog(@“path:%@”,fileName)
    ),以查看可能发生的情况。我尝试在模拟器上运行它,它创建的文件没有坐标和速度(显然),为什么它不能在设备上运行?我试过了,它告诉我:
    PATH:/var/mobile/Applications/0874CD25-D82E-4FC4-BB5C-5302BE5DA95E/Documents/activity.plist
    如果不检查错误,很难说。您可能还希望转储activityArray,以确保所有类型都可以正确编码到plist中。这就解释了为什么可以写入空的plist而不能写入包含条目的plist。我必须在这个plist中存储以下数据:坐标、速度、时间、步骤和活动类型。我将数据编码为:坐标编码为
    NSMutableArray
    ,速度编码为
    NSMutableArray
    (我将有计算平均速度的速度),时间和类型为2个简单的
    NSString
    ,步骤编码为“NSNumber”,不会按照他想要的方式创建plist文件,不过。它使用存储在plist中的单个数据对象创建plist文件。因为plist中存储的
    数据是不透明的,所以它没有提供太多的诊断用途。@gaige:你说得对。。方法是不同的,但他可以实现和克服他的目标problem@RahulPatel:它工作正常,但它创建了一个非常复杂的plist文件,很难解码。。。使用我的方法,我创建了一个非常简单的plist文件,非常容易解码…@lucgian841:对于解码,我还提到了方法。。请查收it@gaige:你能帮我解决这个问题吗?Rahul的解决方案有点太复杂了
    
    - (NSMutableArray *) loadDataFromFile {
       NSArray *myArray=[[NSArray alloc]init];
       NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
       NSString *docDir = [paths objectAtIndex:0];
    
       NSString *Path =[NSString stringWithFormat:@"%@/%@.plist",docDir,@"isLogin"];
    
       if ([[NSFileManager defaultManager] fileExistsAtPath:Path])
           {
              NSLog(@"exist");
              myArray=[[NSArray alloc]initWithArray:[NSKeyedUnarchiver unarchiveObjectWithData:[NSData dataWithContentsOfFile:Path]]];
              return [NSMutableArray arrayWithArray:myArray];
           }
            else{
                NSLog(@"not exist");
                return nil;
           }
     }