开发者

Using NSDictionary throughout an iphone project

开发者 https://www.devze.com 2022-12-27 19:29 出处:网络
How do you access a NSDictionary in different N开发者_StackOverflow中文版SViewControllers. The NSDictionary was initialized in my delegate file. Do I have to import my delegate file to each view contr

How do you access a NSDictionary in different N开发者_StackOverflow中文版SViewControllers. 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


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).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号