简体   繁体   English

读/写一个NSCountedSet到plist

[英]read/write an NSCountedSet to plist

I am developing a game app where i am using an NSCountedSet as my character inventory where the inventory dynamically changes from view to view. 我正在开发一个游戏应用程序,其中我使用NSCountedSet作为角色清单,清单在视图之间动态变化。

In other words: the user can buy items from view 1 and add to the inventory, then the user switches to view 2 and uses some items and those should be removed from the inventory, and so on.. 换句话说:用户可以从视图1购买商品并将其添加到库存中,然后用户切换到视图2并使用某些商品,这些商品应从库存中删除,依此类推。

My questions are: 我的问题是:

1.How can I write and read a NSCounted set efficiently to a plist? 1.如何有效地将NSCounted集写入和读入plist?

2.is the best approach to write the data to disk as view 1 closes and the reread the data as view 2 opens? 2.最好的方法是在视图1关闭时将数据写入磁盘,并在视图2打开时重新读取数据吗? or is there a way i can read the data once when the app launches, make all the changes and then save the data back when the app is terminating? 还是有一种方法可以在应用程序启动时读取一次数据,进行所有更改,然后在应用程序终止时将数据保存回去?

The data consists of strings and numbers only and is small in ammount. 数据仅由字符串和数字组成,并且数量很少。 THe following are snippets from my code: 以下是我的代码片段:

- (void) initInventory
{
//initialize the inventory with some string objects
[Inventory addObject:@"x"];
[Inventory addObject:@"y"];
[Inventory addObject:@"z"];
}
- (void) addItemToInvetory:(NSString*)ItemName
{
//add object passed in method to the inventory
[Inventory addObject:ItemName];
}

- (void) removeItemFromInventory:(NSString*)ItemName
{
//add object passed in method to the inventory
[Inventory removeObject:ItemName];
}

1.How can I write and read a NSCounted set efficiently to a plist? 1.如何有效地将NSCounted集写入和读入plist? ...The data consists of strings and numbers only and is small in amount. ...数据仅由字符串和数字组成,数量很少。

You can just record it using an array of (alternating) strings and numbers. 您可以使用(交替的)字符串和数字数组来记录它。 The number represents the count of the string object. 该数字表示字符串对象的计数。 For a small set, you should not need to worry about the performance of the operation. 对于小型设备,您无需担心操作的性能。

2.is the best approach to write the data to disk as view 1 closes and the reread the data as view 2 opens? 2.最好的方法是在视图1关闭时将数据写入磁盘,并在视图2打开时重新读取数据吗? or is there a way i can read the data once when the app launches, make all the changes and then save the data back when the app is terminating? 还是有一种方法可以在应用程序启动时读取一次数据,进行所有更改,然后在应用程序终止时将数据保存回去?

You can pass it (the model) from one view controller to the next, and just share the same model instance in many cases. 您可以将其(模型)从一个视图控制器传递到下一个视图控制器,并且在许多情况下只需共享同一模型实例。 Whether it makes sense to dispose or not depends on whether or not you need a reference, and how often that information is needed. 处置是否有意义取决于您是否需要参考,以及该信息需要多长时间。 So best practice depends on the memory and your ability to ensure the data is correct. 因此,最佳做法取决于内存和确保数据正确的能力。 For example, you may opt to share in order to avoid unnecessary I/O, and to keep the data synchronized, but you should avoiding holding thousands of objects if you don't need them anytime soon. 例如,您可以选择共享以避免不必要的I / O,并使数据保持同步,但是如果您很快不需要它们,则应避免保存数千个对象。

If your data were not small, you should consider something like CoreData instead (3 values is extra-tiny). 如果数据不小,则应考虑使用类似CoreData的东西(3个值是多余的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM