任何人都可以帮我将数据附加到plist文件?

时间:2012-03-12 13:55:41

标签: iphone ios

我已经尝试过这段代码将我的数据复制到plist但是在附加.can的情况下它不起作用。任何人都建议我添加数据的好方法吗?
    NSMutableDictionary * nameDictionary = [NSMutableDictionary dictionary];

NSString *plistPath=[[NSBundle mainBundle] pathForResource:@"listNew" ofType:@"plist"];
[nameDictionary setValue:nameEntered.text forKey:@"name"];
[nameDictionary setValue:company.text forKey:@"company"];
[nameDictionary setValue:designation.text forKey:@"designation"];

// create dictionary with values in UITextFields
NSMutableArray *plist = [NSMutableArray arrayWithContentsOfFile:plistPath];
if (plist == nil) plist = [NSMutableArray array];
[plist addObject:nameDictionary];
[plist writeToFile:plistPath atomically:YES];
UIAlertView *tellErr = [[UIAlertView alloc] initWithTitle:title message:@"succsefully created" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[tellErr show];
nameEntered.text=@"";
company.text=@"";
designation.text=@"";

4 个答案:

答案 0 :(得分:1)

您应该使用+[NSKeyedArchiver archiveRootObject:toFile:]将Array对象存档到文件。并使用+[NSKeyedUnarchiver unarchiveObjectWithFile:]取消归档。

答案 1 :(得分:0)

我会选择:

NSString *plistPath=[[NSBundle mainBundle] pathForResource:@"listNew" ofType:@"plist"];
NSMutableDictionary *plist = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
if (plist == nil) plist = [[NSMutableDictionary alloc] init];
[plist setObject:nameEntered.text forKey:@"name"];
[plist setObject:company.text forKey:@"company"];
[plist setObject:designation.text forKey:@"designation"];
[plist writeToFile:plistPath atomically:YES];

...等......

当然,这取决于您想要达到的目标。

答案 2 :(得分:0)

您无法在iOS中写入应用程序主包。您必须将属性列表文件复制到可写目录;然后你可以阅读,修改它,然后重写它。

答案 3 :(得分:0)

好的伙计,这是您需要的代码。我在现有的iOS程序中使用它

首先检查词典中是否有任何信息以

开头
static BOOL dicIsNULL = YES;

....

//initialize the variable
dicIsNULL = YES;


//READ FILE
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *stringsPlistPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"listNew.plist"];

        dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:stringsPlistPath];

        //Check if this dic has values
        for (id key in dictionary) 
        {
             //NSLog(@"listNew.plist file has values");
            dicIsNULL = NO;
            break;
        }

现在根据已经创建的dictonary进行以下操作。在词典中添加您的值,这只是一个示例代码。

if (dicIsNULL == YES)
        {
            //NSLog(@"dictionary is NULL");

            dictionary = [[NSMutableDictionary alloc] init];
            dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:tempArray, infoKeyStr,  textTitleField1.text, titleKeyStr, userSelectedCatStr, catKeyStr, nil];

        }

        if (dicIsNULL == NO)
        {
            //NSLog(@"dictionary is NOT null");

            dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:stringsPlistPath];
            [dictionary setObject: tempArray forKey: infoKeyStr];
            [dictionary setObject: textTitleField1.text forKey: titleKeyStr];
            [dictionary setObject: userSelectedCatStr forKey: catKeyStr];
        }

希望这会帮助你。