无法在iOS中创建PLIST文件



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

  1. 我得到我需要存储在此文件中的信息
  2. 我将此信息插入NSDictionary
  3. 我将NSDictionary插入NSMutableArray
  4. 现在我做了一个实用程序,可以用文件处理工作但是它不会在文档目录中创建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];
    }
}

arrayActivity包含此(坐标,速度,时间等...):

(
        {
        coordinates =         (
            "<+45.44083526,+9.19919774> +/- 10.00m (speed 0.00 mps / course 71.02) @ 04/02/14 12:46:08 Ora standard dell'Europa centrale",
            "<+45.44083561,+9.19919755> +/- 65.00m (speed -1.00 mps / course -1.00) @ 04/02/14 12:46:33 Ora standard dell'Europa centrale",
            "<+45.44083612,+9.19919728> +/- 65.00m (speed -1.00 mps / course -1.00) @ 04/02/14 12:46:33 Ora standard dell'Europa centrale",
            "<+45.44083663,+9.19919700> +/- 65.00m (speed -1.00 mps / course -1.00) @ 04/02/14 12:46:33 Ora standard dell'Europa centrale",
            "<+45.44083714,+9.19919673> +/- 65.00m (speed -1.00 mps / course -1.00) @ 04/02/14 12:46:33 Ora standard dell'Europa centrale",
            "<+45.44084181,+9.19917132> +/- 65.00m (speed -1.00 mps / course -1.00) @ 04/02/14 12:46:33 Ora standard dell'Europa centrale",
            "<+45.44083100,+9.19919440> +/- 65.00m (speed -1.00 mps / course -1.00) @ 04/02/14 12:46:33 Ora standard dell'Europa centrale",
            "<+45.44088744,+9.19913454> +/- 50.00m (speed 0.00 mps / course -1.00) @ 04/02/14 12:46:35 Ora standard dell'Europa centrale",
            "<+45.44088744,+9.19913454> +/- 30.00m (speed 0.00 mps / course -1.00) @ 04/02/14 12:46:36 Ora standard dell'Europa centrale",
            "<+45.44088744,+9.19913454> +/- 30.00m (speed 0.00 mps / course -1.00) @ 04/02/14 12:46:37 Ora standard dell'Europa centrale",
            "<+45.44088744,+9.19913454> +/- 30.00m (speed 0.00 mps / course -1.00) @ 04/02/14 12:46:39 Ora standard dell'Europa centrale"
        );
        speed =         (
        );
        steps = 16;
        time = "00:00:08";
        type = Trekking;
    }
)

实用程序类是:

filehandler.m

#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

当我执行此应用程序时,它无法在设备上创建PLIST文件(我通过使用Mac App 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;
       }
 }

最新更新