在整个 iphone 项目中使用 NSDictionary

发布于 2024-08-30 03:56:12 字数 160 浏览 5 评论 0原文

如何在不同的 NSViewController 中访问 NSDictionary。 NSDictionary 已在我的委托文件中初始化。我是否必须将我的委托文件导入到每个视图控制器文件中,或者我的委托之上的所有视图都知道我的 NSDictionary 吗?我是否需要在每个文件中声明我的字典? -谢谢 盖伊

How do you access a NSDictionary in different NSViewControllers. The NSDictionary was initialized in my delegate file. Do I have to import my delegate file to each view controller file or do all my views on top of my delegate know of my NSDictionary? Do I possibly need to declare my Dictionary in each file?
-Thanks
Guy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空城旧梦 2024-09-06 03:56:12

NSDictionary 应该在创建它的任何类中作为属性公开。您必须为需要使用它的每个其他类导入头文件(以便它们能够访问该属性)。

将这些东西放在应用程序委托中通常是不好的做法(子类不应该依赖应用程序委托)——听起来它更适合模型类(有关 MVC 的更多信息)模式,你可以看到 Apple 文档)。

The NSDictionary should be exposed as a property in whatever class creates it. You do have to import the header file for every other class that needs to use it (so that they will be able to access the property).

It's generally bad practice to put these sorts of things in the App Delegate (the child classes shouldn't have to rely on the app delegate)--it sounds like it would be better suited for a model class (for more information on the MVC pattern, you can see Apple's documentation).

别闹i 2024-09-06 03:56:12

如果您的应用程序委托中有 NSDictionary,则可以将其声明为委托中的属性。然后,在任何需要字典对象的文件中,您可以按如下方式访问它:

AppDelegate *mydelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];

NSDictionary *dict = mydelegate.dictionary;

假设您的委托文件名是 AppDelegate 并且您的 NSDictionary 对象引用名称是字典。
不过,您需要在文件中导入 AppDelegate 才能使用强制转换。

If you have your NSDictionary in your application delegate, you can declare it as a property in the delegate. Then, in any file which needs the dictionary object, you can access it as below:

AppDelegate *mydelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];

NSDictionary *dict = mydelegate.dictionary;

This is assuming your delegate file name is AppDelegate and your NSDictionary object reference name is dictionary.
You would need to import AppDelegate in your files though in order to use the cast.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文