【问题标题】:iPhone App not saving values to .plist fileiPhone App 未将值保存到 .plist 文件
【发布时间】:2009-12-18 19:04:10
【问题描述】:

大家好。我正在开发一个在消费者第一次使用时显示模式的应用程序。我有一个 .plist 文件,一旦按下 Save 按钮,它将存储我请求的信息。我可以很好地读取 .plist 文件,当我运行我的保存方法时,它似乎可以正常工作,但 .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];
}

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

【问题讨论】:

    标签: iphone plist


    【解决方案1】:

    您无法写入捆绑目录。

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

    【讨论】:

    • 太棒了!就是这样,但我想我还有另一个问题。我可以去查看文件系统上的 plist 文件,值在那里,但它似乎又创建了 2 个值键对。诡异的。我希望它使用我导入资源文件夹的 plist 文件。我想我也必须弄清楚这一点。感谢您的回答!
    • 如果是我,我会使用 Caches 或 Documents 文件夹中没有 plist 文件作为应用程序之前从未成功完全启动的标志,并且仅在应用和/或用户成功且完全完成初始“首次应用运行”工作流程后,才在缓存/文档中创建 plist 文件。
    【解决方案2】:

    除此之外,您可以在模拟器中写入包,即使您不能在设备上 - 这导致我浪费了大量时间在模拟器和设备之间进行调试

    【讨论】:

      【解决方案3】:

      如果这对任何人都有用,这是我用来做类似事情的一些代码。我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);
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多