iOS 存储对象数组 [英] iOS store Array of Objects

查看:57
本文介绍了iOS 存储对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我实现了几个 tableviewcontroller,它们为每个单元格显示一个 NSObject.每个 tableviewcontroller 都有一个应用于数组的不同过滤器",因此每个表都显示这些对象的特定组.在每个 tableviewcontroller 的 viewdidload 中,我从 NSUserdefaults 加载我的数组,过滤它并显示它.这工作得很好,我正在使用 NSCoder 来获取编码/解码的对象以进行存储.仅出于开发目的,我以编程方式创建每个对象(initWithName:(NSString*)name 等),将所有对象添加到数组中并存储它.但我确信这不是最终的解决方案.问题:我正在寻找一种方法来实现这个对象数组,而无需在代码中手动创建每个对象.对象的数量是固定的,它们的内容不能改变.他们只需要加载.添加:也许在未来的更新中,我会在列表中添加新项目.我该怎么做?

In my application i implemented several tableviewcontrollers that display a NSObject for each cell. Every tableviewcontroller has a different "filter" that is applied to the array so every table displays a specific group of these objects. In the viewdidload of each tableviewcontroller i load my array from NSUserdefaults, filter it, and display it. This works just fine, i am using NSCoder to get the Objects encoded/decoded for storing. For developing purposes only, i create each Object programmatically (initWithName:(NSString*)name etc.), add all Objects to the array and store it. But i am sure this is not the final solution. Question: I am searching for a way to implement this array of objects without creating each object by hand in code.The number of objects is fixed und their content can not change. They just need to be loaded. Add: maybe in future updates i will add new items to the list. How can i do it?

所以我像@Luke Smith 所说的那样实现了它,而且效果很好!谢谢!

So i implemented it like @Luke Smith said and it works perfectly! Thank you!

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"MyPlist" ofType:@"plist"];
NSArray *allElements = [[NSArray alloc] initWithContentsOfFile:plistPath];

for (NSDictionary *newObject in allElements){
    Object *tempObject = [[Object alloc] init];
    tempObject.Type = [newObject objectForKey:@"Type"];
    tempObject.isFavorite = [newObject objectForKey:@"isFavorite"];
    [Elements addObject:tempObject];//MutableArray storing all Elements
}

但是当我进行了一些头脑风暴时,我得出的结论是我需要编辑一些条目.不幸的是,由于 .plist 位于 MainBundle 中,因此无法编辑...是否可以从一开始就将 .plist 存储在文档目录中(可能在 xcode 中)?这是我的 .plist :

But as i did a little bit of brain-storming i came to the conclusion that i would need to edit some entries. Unfortunately since the .plist is in the MainBundle it is not editable... Is it possible to store the .plist in the documents directory from the beginning ( maybe in xcode )? This is my .plist :

<plist version="1.0">
<array>
<dict>
    <key>isFavorite</key>
    <false/>
    <key>Name</key>
    <string>Obj1</string>
</dict><dict>
    <key>isFavorite</key>
    <true/>
    <key>Name</key>
    <string>Obj2</string>
</dict>
</array>
</plist>

因此,当用户按下特定按钮时,我想更改此项目的 .plist 文件中的 isFavorite 值.

So when the user pushes a specific button, i would want to change the isFavorite value in the .plist file for this Item.

推荐答案

看看从 plist 加载你的值.

Look at loading your values from a plist.

您需要在您的项目中创建一个新的 plist 文件并将您的属性添加到其中(看起来有点像 JSON 编辑器 - 相当容易理解)

You need to create a new plist file in your project and add your properties to it (looks a bit like a JSON editor - is fairly easy to understand)

在代码中,您可以使用如下一行代码将 plist 加载到内存中:

In code, you can then load your plist into memory using a line of code like this :

NSString *pathToMyPlist = [[NSBundle mainBundle] pathForResource:@"MyPlist" ofType:@"plist"];

NSDictionary *myPlistContents = [[NSDictionary alloc] initWithContentsOfFile:pathToMyPlist];

然后,您将拥有一个 NSDictionary,其中包含您在 plist 中指定的键和值.您可以遍历这些键和值来初始化您的对象.

You then have an NSDictionary containing the keys and values you specified in your plist. You can loop through these keys and values to initialize your objects.

这篇关于iOS 存储对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆