iPhone App不将值保存到.plist文件

时间:2009-12-18 19:04:10

标签: iphone plist

嘿大家好我正在开发一个应用程序,在消费者第一次使用它时显示模态。我有一个.plist文件,一旦按下Save按钮,它将存储我请求的信息。我可以很好地阅读.plist文件,当我运行我的保存方法时,SEEMS工作正常,但.plist文件没有更新。我不太确定这里的问题。我像这样展示模态。

 - (void) getConsumerInfo {
 NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"consumer.plist"];
 NSMutableDictionary *plistConsumerInfo = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];

 NSString *hasAppeared = [plistConsumerInfo objectForKey:@"HasAppeared"];

 if(hasAppeared != kHasAppeared) {
  ConsumerInfoViewController *tmpConsumerInfoVC = [[ConsumerInfoViewController alloc]
               initWithNibName:@"ConsumerInfoView" bundle:nil];
  self.consumerInfoViewController = tmpConsumerInfoVC;
  [self presentModalViewController:consumerInfoViewController animated:YES];
  [tmpConsumerInfoVC release];
 }
}

应用程序启动时,第一个查看手机中的viewDidLoad方法会调用此方法。在consumerInfoViewController中,我有输入数据的文本字段,当按下Save按钮时,它调用此方法。

    - (IBAction)saveConsumerInfo:(id)sender {
 NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"consumer.plist"];
 NSMutableDictionary *plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];

 NSString *tmpDiversName = txtDiversName.text;
 NSString *tmpLicenseType = txtLicenseType.text;
 NSString *tmpLicenseNum = txtLicenseNumber.text;
 NSString *tmpHasAppeared = @"1";
 NSString *tmpNumJumps = @"3";

 [plistDict setValue:[[NSString alloc] initWithFormat:@"%@", tmpDiversName] forKey:@"ConsumerName"];
 [plistDict setValue:[[NSString alloc] initWithFormat:@"%@", tmpLicenseType] forKey:@"LicenseType"];
 [plistDict setValue:[[NSString alloc] initWithFormat:@"%@", tmpLicenseNum] forKey:@"LicenseNumb"];
 [plistDict setValue:[[NSString alloc] initWithFormat:@"%@", tmpNumJumps] forKey:@"NumJumps"];
 [plistDict setValue:[[NSString alloc] initWithFormat:@"%d", tmpHasAppeared] forKey:@"Show"];
 [plistDict writeToFile:filePath atomically: YES];
 NSLog([[NSString alloc] initWithContentsOfFile:filePath]);
 [self dismissModalViewControllerAnimated:YES];
}

这一切都运行良好,没有打嗝,但文件永远不会更新。我希望它能够更新,以便我可以在整个应用程序中使用此信息并更新标志,以便在输入数据后不再显示视图。如果您需要更多信息,请告诉我。提前谢谢!

3 个答案:

答案 0 :(得分:2)

您无法写入捆绑目录。

请改用Caches文件夹或Documents文件夹。有关如何修改NSSearchPathForDirectoriesInDomains或NSHomeDirectory函数的返回信息以查找这些文件夹的位置(在某处放置数据),请参阅iPhone文档。

答案 1 :(得分:0)

为了补充一点,你可以在模拟器中写入捆绑包,即使你不能在设备上 - 这会导致我在模拟器和设备之间进行大量的时间调试

答案 2 :(得分:0)

如果这对任何人都有用,这是我用来做类似事情的一些代码。我use it在第一次运行时复制一些示例文档,或者在新版本大于上一版本时(如果有必要)复制(注释部分)副本。

BOOL firstrun()
{
    CFStringRef firstRunKey = CFSTR("lastVersion");

    CFNumberFormatterRef formatter;

    CFStringRef currentVersionString;
    CFMutableStringRef currentVersionMutableString;
    CFNumberRef currentVersion;

    CFStringRef lastVersionRunString;
//  CFMutableStringRef lastVersionRunMutableString;
//  CFNumberRef lastVersionRun;



    currentVersionString = CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), kCFBundleVersionKey);
    currentVersionMutableString = CFStringCreateMutableCopy(NULL, 0, currentVersionString);
    CFStringFindAndReplace(currentVersionMutableString, CFSTR("."), CFSTR(""), CFRangeMake(0, CFStringGetLength(currentVersionString)), 0);
    formatter = CFNumberFormatterCreate(NULL, NULL, kCFNumberFormatterNoStyle);
    currentVersion = CFNumberFormatterCreateNumberFromString(NULL, formatter, currentVersionMutableString, NULL, kCFNumberFormatterParseIntegersOnly);

    lastVersionRunString = CFPreferencesCopyAppValue(firstRunKey, kCFPreferencesCurrentApplication);

    if (lastVersionRunString == NULL)
    {
        // first run
        NSFileManager *f = [NSFileManager defaultManager];
        NSBundle *b = [NSBundle mainBundle];
        NSError *e;

        NSArray *xs = [NSArray arrayWithObjects: @"Welcome", 
                              @"A Child's Garden of Verses", 
                                     @"Address to a Haggis", 
                                               @"Sonnet 18", nil];

        for (id x in xs)
        {
            BOOL s = [f copyItemAtPath: [b pathForResource: x ofType: @"txt"] 
                   toPath: [DocumentManager pathForDocumentWithName: x]  
                    error: &e];

            if (s == YES)
            {
                NSLog(@"Copied first run file %@ successfully.", x);
            }
            else {
                NSLog(@"Failed to copy %@.", x);
            }
        }
    } // else {
        // The following is to enable support for new releases needing to show new information
/*
        lastVersionRunMutableString = CFStringCreateMutableCopy(NULL, 0, lastVersionRunString);
        CFStringFindAndReplace(lastVersionRunMutableString, 
                               CFSTR("."), 
                               CFSTR(""), 
                               CFRangeMake(0, CFStringGetLength(lastVersionRunMutableString)), 
                               0);
        lastVersionRun = CFNumberFormatterCreateNumberFromString(NULL, formatter, lastVersionRunMutableString, NULL, kCFNumberFormatterParseIntegersOnly);

        CFComparisonResult cr = CFNumberCompare(lastVersionRun, currentVersion, NULL);
*/      

//  }

    // Update last run version
    CFPreferencesSetAppValue(firstRunKey, currentVersionString, kCFPreferencesCurrentApplication);
    CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication);

    CFRelease(currentVersionMutableString);
    CFRelease(formatter);
    CFRelease(currentVersion);

    return (lastVersionRunString == NULL);

    }