English 中文(简体)
如何设计一个应用程序 以保持一组字符串 跨越模型 在iOS 中?
原标题:How to design an app to keep a set of strings across models in iOS?

我正在撰写一个通过服务器和客户端连接到网络的应用程序( 存储为两个字符串 ) 。 我的应用程序由多个视图控制器和模型组成( 可能每个... 10个或10个); 然而, 我几乎所有的模型都需要访问这些字符串( 可能还有2个或3个 ) 。 让这些字符串在不同的模型中持续存在的最佳方式是什么? 模型本身非常不同, 除了这些字符串之外 。

最佳回答

似乎您在整个应用程序中传播了网络代码,从而犯了一个错误。 服务器的接口是一个单独的责任, 应该将其纳入到自己的类中。 您的模型然后与该类人交谈, 而不必知道该类人与服务器交谈的任何细节 。 我为此找到的最简单的方法是假单子 。

问题回答

如果字符串不更改, 您可以使用预处理器宏 :

#define kNetwork @"myNetwork"

可以有几种可能的办法,其中一些是:

  1. You can use NSUserDefaults to save these string values and use when you need.
  2. You can store these strings in plist files.
  3. You can create a model class consisting these two string values, and create a singleton instance of that.
  4. You can create a constant class and have these string values as a constant.





相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签