NSMutable Array in other class

风流意气都作罢 提交于 2020-01-25 23:46:17

问题


I declared an NSMutableArray in my .h and I set property and synthesize, but if i want use this array in other class? How I do?


回答1:


If you have, as you stated, created an @property for the NSMutableArray, you can easily access it in other classes by name. For example:

#import "MyClass.h"

...

MyClass *myClass = [[MyClass alloc]init];
NSMutableArray *array = [myClass.myArray mutableCopy];

(Note: I did not use memory management.)




回答2:


The basic idea here is that in your original class, the array is referred to by a pointer. Your original class would allocate it and presumably load it. Other parts of your program can be handed the contents of the property, which is a pointer, assign that to their own pointer holder, and use it as if you had declared it there.

So if MyClass has a property of MyArray, which is an NSMutableArry *, then MyArray is a pointer holder, (just 'pointer' for short).

You program can then make a new pointer, like NSMutableArray *ThatArray, and then simply do:

MyClass *aClass = [[MyClass alloc] initWithMyInitStuff];
NSMutableArray *ThatArray = aClass.MyArray;

NSLog("Count of ThatArray: %d", [That.Array count]);


来源:https://stackoverflow.com/questions/5516266/nsmutable-array-in-other-class

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