-1

我有一些代码可以从电子邮件中将设置导入我的应用程序,但它只有在plist将设置导入到已经存在的情况下才有效。

这是我目前用来导入设置并写入 plist 的代码。

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
if (url){

        NSDictionary *openedDictionary = [NSDictionary dictionaryWithContentsOfURL:url];

        // get paths from root direcory
        NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
        // get documents path
        NSString *documentsPath = [paths objectAtIndex:0];
        // get the path to our Data/plist file
        NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];
        NSDictionary *originalDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];

        NSMutableDictionary *newDictionary = [originalDictionary mutableCopy];
        for (NSString *key in openedDictionary) {
            if (!newDictionary[key]) {
                newDictionary[key] = openedDictionary[key];
            }
        }

        [newDictionary writeToFile:plistPath atomically:YES];

    }
    NSError *error = nil;
    if (![[NSFileManager defaultManager] removeItemAtURL:url error:&error]) {
        NSLog(@"error while deleting: %@", [error localizedDescription]);
    }
    return YES;
}

但是我需要做的是创建它,Data.plist如果它不存在,或者重命名通过电子邮件发送到的 plistData.plist并存储它,前提是还没有Data.plist.

4

3 回答 3

3

以下是我如何通过代码创建属性列表并从中检索数据。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"plist.plist"]; NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: path]) 
{
    path = [documentsDirectory stringByAppendingPathComponent: [NSString stringWithFormat: @"plist.plist"] ];
}




NSFileManager *fileManager = [NSFileManager defaultManager];
NSMutableDictionary *data;

if ([fileManager fileExistsAtPath: path]) 
{
            data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
}
else
{
    // If the file doesn’t exist, create an empty dictionary
    data = [[NSMutableDictionary alloc] init];
}

//To insert the data into the plist
int value = 5;
[data setObject:[NSNumber numberWithInt:value] forKey:@"value"];
[data writeToFile: path atomically:YES];
[data release];

// To retrieve the data from the plist
NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
int value1;
value1 = [[savedStock objectForKey:@"value"] intValue];
NSLog(@"%i",value1);
[savedStock release];
于 2013-08-01T12:38:43.543 回答
1
NSFileManager *defaultManager = [NSFileManager defaultManager];

    if ([defaultManager fileExistsAtPath:plistPath]) 
   {
        // rename the file
    }
    else 
    {
        //create empty file
    }

这就是创建空plist文件的方法

NSDictionary *dict = [NSDictionary dictionary];

//you can create file in any path in app sandbox, for example I'm creating in document dir.
NSString *FilePathWithName = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
FilePathWithName = [FilePathWithName stringByAppendingPathComponent:@"name.plist"];
[dict writeToFile:FilePathWithName atomically:YES];

对于重命名您可以做的是,您可以将.plist/.property文件(要重命名)加载为字典并用新名称写入该字典(如上写入 plist)并删除.plist/.property文件。

于 2013-08-01T12:06:48.590 回答
1
NSFileManager *defaultManager = [NSFileManager defaultManager];
    if ([defaultManager fileExistsAtPath:plistPath]) {
        //Do stuff with path
    }
    else {
        //Write a plist to the path
    }
于 2013-08-01T11:26:52.327 回答