Use NSMutableArray in app delegate in another class

无人久伴 提交于 2019-12-23 06:06:13

问题


I have an NSMutableArray in my app delegate. I wish to use that mutable array in a different class in my program, as if it were a global variable. How would I do this?

Is it as simple as importing the header and referencing the object?


回答1:


Take advantage of the UIApplication singleton:

YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];

Then, access your array property like this: appDelegate.yourArrayProperty




回答2:


Make you array a property of your delegate as Sean said

@property(readonly) NSMutableArray *theArray;

Then access the array like this :

((YourAppDelegateClass *)[UIApplication sharedApplication].delegate).theArray


来源:https://stackoverflow.com/questions/6061659/use-nsmutablearray-in-app-delegate-in-another-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!