使用全局变量vs读取和写入文件

时间:2021-07-23 15:45:56

I'm afraid this is a bit of an abstract question. I'm basically writing an iPhone game which revolves around a Pet, a bit like a tamagotchi for example. I've created a Pet object, which contains all the data relevant to the Pet's status. The rest of the program accesses the Pet's data and performs relevant actions.

我担心这是一个抽象的问题。我基本上是在写一个围绕宠物的iPhone游戏,例如有点像宠物狗。我创建了一个Pet对象,其中包含与Pet状态相关的所有数据。程序的其余部分访问Pet的数据并执行相关操作。

The pet is saved to file using encodeWithCoder and initWithCoder methods. So whenever a method in the program needs to access any pet data, it creates a new instance of the pet by loading it from file. If any changes are made, they are done within methods of the Pet class. These methods always end by writing the Pet to file.

使用encodeWithCoder和initWithCoder方法将pet保存到文件中。因此,只要程序中的方法需要访问任何宠物数据,它就会通过从文件加载来创建宠物的新实例。如果进行了任何更改,则它们在Pet类的方法中完成。这些方法总是通过将Pet写入文件来结束。

So as the program is running, it is constantly writing to file and reading from file, every time a change is made. If I then want to add a new variable to the Pet, say a BOOL variable called showReaction (to determine whether or not to show the Pet's reaction to a certain event say), I have to include this variable in both my encodeWithCoder and initWithCoder methods. This seems a bit cumbersome since these methods are consequently growing larger and larger. And constantly reading and writing to file seems inefficient.

因此,当程序运行时,每次进行更改时,它都会不断写入文件并从文件中读取。如果我想为Pet添加一个新变量,比如一个名为showReaction的BOOL变量(确定是否显示Pet对某个事件的反应),我必须在我的encodeWithCoder和initWithCoder方法中都包含这个变量。这看起来有点麻烦,因为这些方法因此越来越大。并且不断地读取和写入文件似乎效率低下。

I would have thought a better approach would be to have a global variable which represents the Pet. And that any method in the program can accesses this global variable in order to change it. The only time I write to file is when the player exits the game, and I only read from file once when the game loads.

我本以为更好的方法是拥有一个代表Pet的全局变量。并且程序中的任何方法都可以访问此全局变量以进行更改。我写入文件的唯一时间是玩家退出游戏时,我只在游戏加载时从文件中读取一次。

My question is - I'm not exactly sure of the best approach to this. And whether or not this is good programming practice? Should I declare the global variable in my main ViewController and then refrain from releasing it during the program's running? Will I be able to alter variables in the Pet class and have this data retained during the running of the program. I.e. setting showReaction to TRUE in one method, reading it in another method and setting it to FALSE in another method...

我的问题是 - 我不确定最好的方法。这是否是一个好的编程实践?我应该在我的主ViewController中声明全局变量,然后在程序运行期间不要释放它吗?我是否能够在Pet类中更改变量,并在程序运行期间保留此数据。即在一个方法中将showReaction设置为TRUE,在另一个方法中将其读取并在另一个方法中将其设置为FALSE ...

Any advice would be much appreciated. I've been writing this program for a while now, and am wondering if I should make these changes now before I ingrain any bad practices further. But given the size of my code, I don't want to experiment too much without checking with the experts first!

任何建议将不胜感激。我已经写了这个程序一段时间了,我想知道我是否应该在进一步修改任何不良做法之前做出这些改变。但考虑到我的代码大小,我不想在没有先咨询专家的情况下进行太多实验!

Thanks,

Michael

2 个解决方案

#1


1  

Why don't you create a singleton? that way your code won't need to worry about how it is being done. Try to decouple your code.

你为什么不创建一个单身人士?这样你的代码就不用担心它是如何完成的。尝试解耦代码。

#2


0  

I think its a bit difficult to say on an abstract level what is best in your case but often what seems simpler is often the best approach. Having just a global instance which you interact with seems to be a pretty straightforward way.

我认为在抽象层面上说你最好的情况有点困难但通常看起来更简单通常是最好的方法。只有一个与之交互的全局实例似乎是一种非常直接的方式。

#1


1  

Why don't you create a singleton? that way your code won't need to worry about how it is being done. Try to decouple your code.

你为什么不创建一个单身人士?这样你的代码就不用担心它是如何完成的。尝试解耦代码。

#2


0  

I think its a bit difficult to say on an abstract level what is best in your case but often what seems simpler is often the best approach. Having just a global instance which you interact with seems to be a pretty straightforward way.

我认为在抽象层面上说你最好的情况有点困难但通常看起来更简单通常是最好的方法。只有一个与之交互的全局实例似乎是一种非常直接的方式。