How to get elements from an array in another .m file?

I have an array of 6 NSNumber in a file called level.m . In another game.m file game.m I would like to “get” this array and change each of its objects to separate integers.

I tried using NSUserDefaults , but this seems like a very uncomfortable way of doing what I want to do. Is there a better way?

+4
source share
2 answers

Yes, there is a much better way than using NSUserDefaults!

You can simply access the array directly from your game. First, you must import level.h into game.m (for example: #import "level.h" ).

Now say that you want to enter the value of the number at index 0 in your array. you can do it this way: int myNumber = [[[name_of_level_class name_of_array] objectAtIndex:0] intValue];

A quick NSLog will prove that the integer myNumber will be equal to any number of your index 0 in an array of your level .m

0
source

Add your array to AppDelegate and it will be available globally throughout the application.

Create a property and synthesize an array in AppDelegate.

You can use ewhere where you want to use an instance of AppDelegate

-1
source

Source: https://habr.com/ru/post/1434890/


All Articles