简体   繁体   English

为IOS设置模型的正确方法是什么?

[英]What is the correct way to setup a Model for IOS?

I am creating an app and using Parse.com for Data Storage. 我正在创建一个应用程序,并将Parse.com用于数据存储。

I would like to use a Model for all the different aspects of Parse that I use. 我想对我使用的Parse的所有不同方面使用模型。 For example, if I store some Game data I would like a model to handle doing any finds/updates/entries into a Game model held on Parse. 例如,如果我存储一些游戏数据,我希望模型可以处理对在Parse上保存的游戏模型进行的任何查找/更新/输入。

So I would like a Model to handle all these methods, instead of adding these into the View Controllers. 因此,我希望模型能够处理所有这些方法,而不是将它们添加到视图控制器中。

What is the best/correct way to set these up? 设置它们的最佳/正确方法是什么? I have heard of singletons but not sure if they would be correct here. 我听说过单例,但不确定在这里是否正确。

At present, I set these up using a subclass of NSObject. 目前,我使用NSObject的子类进行设置。 I then create all the methods as class methods (no instance methods as there is no instance to create). 然后,我将所有方法创建为类方法(没有实例方法,因为没有要创建的实例)。 I then call any class method as usual. 然后,我照常调用任何类方法。

[GameModel classMethodName];

Would this be correct? 这是正确的吗? Is there a better approach or any issues with this approach? 是否有更好的方法或此方法有任何问题?

At present, I set these up using a subclass of NSObject. 目前,我使用NSObject的子类进行设置。 I then create all the methods as class methods (no instance methods as there is no instance to create). 然后,我将所有方法创建为类方法(没有实例方法,因为没有要创建的实例)。 I then call any class method as usual. 然后,我照常调用任何类方法。

This doesn't sound like a very good plan. 这听起来并不是一个很好的计划。 The job of a model is to manage the data that your application operates on. 模型的工作是管理应用程序所基于的数据。 That means that your model should have data to store, which means that you should be instantiating your model. 这意味着您的模型应该存储数据,这意味着您应该实例化模型。

Building support for your back end infrastructure (Parse, in your case) is a good idea. 为后端基础架构(在您的情况下为Parse)建立支持是一个好主意。 It's much better to put it there than to put it in your view controllers. 将其放置在视图控制器中要好得多。 But where does that supporting code store the game state, for example, or the player's history? 但是,例如,该支持代码在哪里存储游戏状态或玩家的历史记录? It should store it in the model object(s). 它应该将其存储在模型对象中。

Without getting into the whole singletons good vs. singletons bad can of worms (you should Google that), it sounds like you're using class methods for much the same reason that people often use singletons: easy access. 如果没有深入研究蠕虫(最好是谷歌)的单例,而不是单例,蠕虫(您应该使用Google)就好像您在使用类方法一样,原因与人们经常使用单例的原因相同:易于访问。 Instantiating your class and using instance methods instead of class methods would mean that you have to find a way to share the model with those objects that need it, and that's more work than just using the globally accessible class methods. 实例化您的类并使用实例方法而不是类方法将意味着您必须找到一种与需要该模型的对象共享模型的方法,而不仅仅是使用可全局访问的类方法。 Doing that work will make for a better application, though. 不过,这样做可以使应用程序更好。

For example, you could restrict each view controller's access to just those parts of the model that it needs. 例如,您可以将每个视图控制器的访问权限限制为仅需要其模型的那些部分。 The game play view controller might only get to see the current game board, and the high scores view controller would only see the player's history. 游戏视图控制器可能只会看到当前游戏板,而高分视图控制器只会看到玩家的历史记录。 That eliminates many possible bugs -- you'll never have to wonder if the game board view controller is causing a problem with the player history, because it never sees that part of the model. 这消除了许多可能的错误-您永远不必怀疑游戏板视图控制器是否导致玩家历史记录出现问题,因为它从未看到过模型的那一部分。

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

相关问题 在IOS中使用自定义字体的正确方法是什么? - What is the correct way to use a custom font in IOS? ios - 将图像添加到捆绑包中的正确方法是什么? - ios - what is the correct way to add images to the bundle? 在 iOS 中的 UICollectionViewDiffableDataSource 中更新 model 变量的正确方法 - Correct way to update model variable inside UICollectionViewDiffableDataSource in iOS 设置iOS助手的正确方法 - Correct method to setup iOS constriants 当我的应用程序在iOS上终止时,保存NSUserDefaults的正确方法是什么 - What is the correct way to save NSUserDefaults when my app is terminated on iOS ios - 将外部文件添加到我的新项目的正确方法是什么? - ios - What is the correct way to add outside files to my new project? 在iOS中从内存中清除敏感数据的正确方法是什么? - What is the correct way to clear sensitive data from memory in iOS? 同步此多线程代码(iOS / Cocoa)的正确方法是什么? - What is the correct way to synchronize this multithreaded code (iOS/Cocoa)? 提交2个版本的iOS应用的正确方法是什么? - What's the correct way to submit 2 versions of an iOS app? iPhone iOS使用情节提要实现登录/注销工作流的正确方法是什么? - iPhone iOS what is the correct way to implement login / logout workflow with a storyboard?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM