Use NSMutableArray in app delegate in another clas

2019-06-13 05:48发布

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?

2条回答
闹够了就滚
2楼-- · 2019-06-13 06:12

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
查看更多
Bombasti
3楼-- · 2019-06-13 06:21

Take advantage of the UIApplication singleton:

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

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

查看更多
登录 后发表回答