hoping for advice on something.
希望得到一些建议。
I have a Levels Engine class that creates an NSMutable Array called levelsArray.
我有一个Levels Engine类,它创建一个名为levelsArray的NSMutable Array。
I am passing the data to a Levels View Controller which is working just fine.
我正在将数据传递给Levels View Controller,它正常工作。
I also have a Particle Emitter class to which I am hoping to pass the level data.
我还有一个粒子发射器类,我希望传递水平数据。
However I am constantly being told that the count level of the array is 0 when I pass it to the Particle Emitter class.
然而,当我将它传递给粒子发射器类时,我不断被告知数组的计数级别为0。
The array has been setup properly:
数组已正确设置:
**LevelsEngine.h**
@interface
LevelsEngine : NSObject {
NSMutableArray *levelsArray; }
@property (retain) NSMutableArray
*levelsArray;
**LevelsEngine.m**
@synthesize levelsArray;
LevelsArray =[NSMutableArray array];
**Code used in ParticleEmitter.m**
newlevelsArray = [NSMutableArray array];
newlevelsArray=view.levelsArray;
Am I right in thinking I am having this error because I am trying to pass the array data from one NSObject to another and not to a view controller?If so how can I pass the data?
我是否正确地认为我有这个错误,因为我试图将数组数据从一个NSObject传递到另一个NSObject而不是视图控制器?如果是这样我怎样才能传递数据?
2 个解决方案
#1
1
Couple of things.
几件事。
**Code used in ParticleEmitter.m**
newlevelsArray = [NSMutableArray array];
newlevelsArray=view.levelsArray;
The first line is creating a new array. The 2nd line is assigning newlevelsArray to be a pointer to the array in view.levelsArray, leaving the object you created in line #1 orphaned.
第一行是创建一个新数组。第二行是将newlevelsArray指定为view.levelsArray中指向数组的指针,使您在第1行中创建的对象保持孤立状态。
I think you were intending the 2nd line to be a field by field copy of the array, but assignments of objects don't work that way.
我认为你打算将第二行作为数组的字段副本,但是对象的赋值不会那样。
You can fix this by 2 things.
你可以通过两件事来解决这个问题。
1) Remove the first line newlevelsArray = [NSMutableArray array];
1)删除第一行newlevelsArray = [NSMutableArray array];
2) Change the 2nd line to `newlevelsArray = [view.levelsArray copy];
2)将第二行更改为`newlevelsArray = [view.levelsArray copy];
This will actually do a copy, which is probably what you want since you can then go ahead and modify newlevelsArray in ParticleEmitter.m without changing the value in view.
这实际上会做一个副本,这可能是你想要的,因为你可以继续修改ParticleEmitter.m中的newlevelsArray而不改变视图中的值。
Important note: don't forget to create a -dealloc: method in your Particle emitter class which releases newlevelsArray:
重要说明:不要忘记在Particle发射器类中创建一个-dealloc:方法,该类释放newlevelsArray:
-(void)dealloc {
if (newlevelsArray) [newlevelsArray release];
[super dealloc];
}
An alternative solution is to use setters.
另一种解决方案是使用setter。
Instead of:
2) Change the 2nd line to newlevelsArray = [view.levelsArray copy];
2)将第二行更改为newlevelsArray = [view.levelsArray copy];
Do:
2) Change the 2nd line to this.newlevelsArray = view.levelsArray;
Where you have to define newlevelsArray to be a property of the ParticleEmitter class using
2)将第二行更改为this.newlevelsArray = view.levelsArray;您必须将newlevelsArray定义为使用的ParticleEmitter类的属性
@property (copy) NSMutableArray * newlevelsArray;
@property(copy)NSMutableArray * newlevelsArray;
Note the use of "copy" instead of "retain". This will do a field by field copy of the array, which is most likely advisable for containers of mutable objects.
注意使用“复制”而不是“保留”。这将执行数组的字段副本,这很可能适用于可变对象的容器。
#2
0
You need to change your code,
你需要改变你的代码,
call the newlevelarray in the LevelsEngine.h calls.
在LevelsEngine.h调用中调用newlevelarray。
and your code should look like
你的代码应该是这样的
Classobject.newlevelsArray =[nsarray arraywitharray: LevlesArray] ;
Classobject.newlevelsArray = [nsarray arraywitharray:LevlesArray];
This should solve your problem.
这应该可以解决您的问题。
#1
1
Couple of things.
几件事。
**Code used in ParticleEmitter.m**
newlevelsArray = [NSMutableArray array];
newlevelsArray=view.levelsArray;
The first line is creating a new array. The 2nd line is assigning newlevelsArray to be a pointer to the array in view.levelsArray, leaving the object you created in line #1 orphaned.
第一行是创建一个新数组。第二行是将newlevelsArray指定为view.levelsArray中指向数组的指针,使您在第1行中创建的对象保持孤立状态。
I think you were intending the 2nd line to be a field by field copy of the array, but assignments of objects don't work that way.
我认为你打算将第二行作为数组的字段副本,但是对象的赋值不会那样。
You can fix this by 2 things.
你可以通过两件事来解决这个问题。
1) Remove the first line newlevelsArray = [NSMutableArray array];
1)删除第一行newlevelsArray = [NSMutableArray array];
2) Change the 2nd line to `newlevelsArray = [view.levelsArray copy];
2)将第二行更改为`newlevelsArray = [view.levelsArray copy];
This will actually do a copy, which is probably what you want since you can then go ahead and modify newlevelsArray in ParticleEmitter.m without changing the value in view.
这实际上会做一个副本,这可能是你想要的,因为你可以继续修改ParticleEmitter.m中的newlevelsArray而不改变视图中的值。
Important note: don't forget to create a -dealloc: method in your Particle emitter class which releases newlevelsArray:
重要说明:不要忘记在Particle发射器类中创建一个-dealloc:方法,该类释放newlevelsArray:
-(void)dealloc {
if (newlevelsArray) [newlevelsArray release];
[super dealloc];
}
An alternative solution is to use setters.
另一种解决方案是使用setter。
Instead of:
2) Change the 2nd line to newlevelsArray = [view.levelsArray copy];
2)将第二行更改为newlevelsArray = [view.levelsArray copy];
Do:
2) Change the 2nd line to this.newlevelsArray = view.levelsArray;
Where you have to define newlevelsArray to be a property of the ParticleEmitter class using
2)将第二行更改为this.newlevelsArray = view.levelsArray;您必须将newlevelsArray定义为使用的ParticleEmitter类的属性
@property (copy) NSMutableArray * newlevelsArray;
@property(copy)NSMutableArray * newlevelsArray;
Note the use of "copy" instead of "retain". This will do a field by field copy of the array, which is most likely advisable for containers of mutable objects.
注意使用“复制”而不是“保留”。这将执行数组的字段副本,这很可能适用于可变对象的容器。
#2
0
You need to change your code,
你需要改变你的代码,
call the newlevelarray in the LevelsEngine.h calls.
在LevelsEngine.h调用中调用newlevelarray。
and your code should look like
你的代码应该是这样的
Classobject.newlevelsArray =[nsarray arraywitharray: LevlesArray] ;
Classobject.newlevelsArray = [nsarray arraywitharray:LevlesArray];
This should solve your problem.
这应该可以解决您的问题。