如何在 ios 中保存、检索、删除和更新 Plist 文件中的数据?

2023-12-22

我正在创建一个 iPhone 应用程序,在其中我可以获取所有国家/地区名称、徽标和玩家名称。我想将该数据保存在.plist代替sqlite服务器。我不知道如何创建 plist 文件DocumentDirectory并保存数据。

请有人建议我如何将数据保存在 plist 文件中。


我正在通过屏幕截图并逐步进行操作。请按照此操作,您将得到答案。

首先,您必须通过 Xcode 创建属性列表。

Step:1

Step:2

Step:3

在保存按钮操作中保存数据:

   // Take 3 array for save the data .....

    -(IBAction)save_Action:(id)sender
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsPath = [paths objectAtIndex:0];
        NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"manuallyData.plist"];

        [self.nameArr addObject:self.nameField.text];
        [self.countryArr addObject:self.countryField.text];
        [self.imageArr addObject:@"image.png"];

        NSDictionary *plistDict = [[NSDictionary alloc] initWithObjects: [NSArray arrayWithObjects: self.nameArr, self.countryArr, self.imageArr, nil] forKeys:[NSArray arrayWithObjects: @"Name", @"Country",@"Image", nil]];

        NSError *error = nil;
        NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];

        if(plistData)
        {
            [plistData writeToFile:plistPath atomically:YES];
            alertLbl.text = @"Data saved sucessfully";
        }
        else
        {
            alertLbl.text = @"Data not saved";
        }
    }
 // Data is saved in your plist and plist is saved in DocumentDirectory

Step:4

从 plist 文件中检索数据:

    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"manuallyData.plist"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
    {
        plistPath = [[NSBundle mainBundle] pathForResource:@"manuallyData" ofType:@"plist"];
    }

    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    self.nameArr = [dict objectForKey:@"Name"];
    self.countryArr = [dict objectForKey:@"Country"];

Step:5

从 plist 文件中删除数据:

    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"manuallyData.plist"];
    NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:(NSString *)plistPath];

    self.nameArr = [dictionary objectForKey:@"Name"];
    self.countryArr = [dictionary objectForKey:@"Country"];

    [self.nameArr removeObjectAtIndex:indexPath.row];
    [self.countryArr removeObjectAtIndex:indexPath.row];

    [dictionary writeToFile:plistPath atomically:YES];

Step:6

在“更新”上更新您的数据,单击“操作”:

    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];
    NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"manuallyData.plist"];

    if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
    {
        plistPath = [[NSBundle mainBundle] pathForResource:@"manuallyData" ofType:@"plist"];
    }

    self.plistDic = [[NSDictionary alloc] initWithContentsOfFile:plistPath];

    [[self.plistDic objectForKey:@"Name"] removeObjectAtIndex:self.indexPath];
    [[self.plistDic objectForKey:@"Country"] removeObjectAtIndex:self.indexPath];
    [[self.plistDic objectForKey:@"Image"] removeObjectAtIndex:self.indexPath];

    [[self.plistDic objectForKey:@"Name"] insertObject:nameField.text atIndex:self.indexPath];
    [[self.plistDic objectForKey:@"Country"] insertObject:countryField.text atIndex:self.indexPath];
    [[self.plistDic objectForKey:@"Image"] insertObject:@"dhoni.jpg" atIndex:self.indexPath];

    [self.plistDic writeToFile:plistPath atomically:YES];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 ios 中保存、检索、删除和更新 Plist 文件中的数据? 的相关文章

随机推荐

  • 使用八度无头

    是否有可能使用 Octave 无头 像这样的东西 octave lt 5 4 gt result txt Using octave silent eval 5 4 gt result txt 你会得到 ans 9 in result txt
  • 是否可以在CSS中设置img标签的src属性?

    是否可以设置srcCSS 中的属性值 大多数情况下 我们这样使用它 img src pathTo myImage jpg 我希望它是这样的 img class myClass myClass some src property url pa
  • Newtonsoft.Json.DeserializeObject 抛出什么异常?

    Newtonsoft Json DeserializeObject 抛出什么异常 我想处理他们 http james newtonking com json help topic html M Newtonsoft Json JsonCon
  • GIT:根文件夹中存在 CRA 应用程序,我如何正确推送到 git?

    我对 git 很陌生 这可能是一个愚蠢的问题 但我真的不知道如何解决这个问题 我一直在努力寻找答案 但我找不到 我试图使用 React js 和 Node js 创建我的项目 我创建了从 npx create react app clien
  • 存储用户设置 - 使用“标志”或“位”而不是一堆布尔值有什么问题吗?

    我正在为我的 MVC 应用程序设计用户设置 现在我有大约 20 个用户可以切换的布尔设置 由于每个用户总是拥有每个设置 因此我正在考虑将每个设置存储为用户表中的布尔值 尽管随着应用程序需求的增长 这会变得笨拙 第一个问题 在这种情况下 表上
  • Swift-删除推送通知徽章号码?

    我试图快速删除图标徽章 但 PFInstallation 似乎不再起作用 我该怎么做呢 您可以通过将应用程序徽章图标设置为 0 来 删除 它 斯威夫特 UIApplication sharedApplication applicationI
  • 我可以在 emberjs 中为视图提供显示动画吗

    这是使用 emberjs 路由器的示例http jsbin com agameq edit http jsbin com agameq edit 现在我想要一些显示动画 例如当路线更改时淡入或淡出 我应该怎么办 Every View在 em
  • 选择 Apache Hive 中是否存在表

    我有一个配置单元查询 其格式如下 select from table1 left join select from table2 on some condition The table2根据环境的不同 可能不存在 因此 如果仅存在 tabl
  • MIPS $gp 寄存器

    使用有危险吗 gp注册来存储一个值 我想我的问题是真正的功能是什么 gp它是否以某种方式在幕后被调用 以便如果我使用它 事情可能会变得非常非常错误 好吧 gp 寄存器指向全局区域 习俗 http en wikipedia org wiki
  • 文件中的修改历史记录

    我曾在一些没有使用源代码管理的地方工作过 他们似乎养成了在更改的代码周围添加注释来解释更改的习惯 以便可以恢复事情 我发现这使得代码非常难以阅读 并且相当坚定地认为在引入源代码控制之后不需要这样的注释 因为修订历史记录将让您将票证与更改相匹
  • 在Android中将电话号码格式化为E164格式

    我想将设备中联系人的每个电话号码转换为 E 164 格式 所以 我在下面使用了开源 库电话号码 https github com googlei18n 所以我像这里一样使用它 Phonenumber PhoneNumber formatte
  • linspace 相对于冒号“:”运算符的优点是什么?

    写作有什么好处吗 t linspace 0 20 21 over t 0 1 20 我知道前者产生一个向量 就像第一个一样 谁能告诉我一些情况linspace有用过t 0 1 20 这不仅仅是可用性 尽管文档说 The linspace函数
  • MS Word、OpenXML、PageSetup、方向和 4 方向边距

    我用 OpenXML 制作了这个文档 我正在学习 OpenXML 哦 太难了 MainDocumentPart m wd AddMainDocumentPart m Document new Document Body b1 new Bod
  • 在页面加载时在 CSS 中触发没有 javascript 的转换

    我想通过 CSS 过渡来增加栏的宽度 但不喜欢通过悬停事件触发它 例如http jsfiddle net TFHn9 http jsfiddle net TFHn9 我只是想要一个即时动画 当页面加载时 当然不需要Javascript 有机
  • 如何编译/安装node.js(无法配置cxx编译器!)(Ubuntu)。

    如何在 Ubuntu 上编译 安装 Node js 它失败并出现以下错误cxx compiler 单线安装所有需要的依赖项 curl 和 git 并不是真正需要的 但非常有用 如果通过 nvm 安装也需要 sudo apt get inst
  • 如何在 JavaScript 中实现锁

    怎么可能相当于lock在 C 中可以用 JavaScript 实现吗 因此 为了解释我的想法 一个简单的用例是 用户点击按钮B B引发 onclick 事件 如果B is in event state该事件正在等待B将在ready stat
  • Android中如何获取真实的设备型号?

    例如 在我的 Xperia mini 手机上 建造模型 http developer android com reference android os Build html MODEL回报 st15i 建造 制造商 http develop
  • 何时在 OpenGL 中使用 glBufferData

    我刚刚开始学习 C 中的顶点缓冲区对象 我正在读一本关于 OpenGL 的书 书中说 VBO 渲染比其他形式的渲染更高效 因为数据存储在 GPU 上而不是堆上 但是 我很困惑如果您仍然需要将数据数组从堆加载到 GPU 这会是怎么回事 每隔几
  • 恢复后重新安装自制符号链接

    重新安装后 我从时间机器恢复了运行 10 9 的 Mac 但似乎下面的符号链接 usr local bin因为有些公式已经丢失了 公式依然按照安装brew list formula但没有来自的链接 usr local bin e g usr
  • 如何在 ios 中保存、检索、删除和更新 Plist 文件中的数据?

    我正在创建一个 iPhone 应用程序 在其中我可以获取所有国家 地区名称 徽标和玩家名称 我想将该数据保存在 plist代替sqlite服务器 我不知道如何创建 plist 文件DocumentDirectory并保存数据 请有人建议我如