I have two Vectors one called "SET_grid" which should never me changed and one called "tmp_grid" which can, but how do i copy SET_grid to tmp_grid without binding it to the original, so if tmp_grid change then the SET_gird doesn't, these Vectors are both multidimensional e.g.
public var tmp_grid:Vector.<Vector.<node>> = new Vector.<Vector.<node>>(2);
public var SET_grid:Vector.<Vector.<node>> = new Vector.<Vector.<node>>(2);
so i would use them like this....
tmp_grid[x][y].sayhello();
tmp_grid = SET_grid does not work
tmp_grid = SET_grid.concat(); // nor does this one
Any help would be great
Nested arrays cannot be cloned without iterations. It's because they're nested :)
What this means is that you have to use nested loops and push to second vectors..
Related
Please forgive my terminology, Im not educated on the proper.
Lets say I have multiple movieclip variables
var rblock1:MovieClip = new Rblock();
var rblock2:MovieClip = new Rblock();
var rblock3:MovieClip = new Rblock();
var yblock1:MovieClip = new Yblock();
var yblock2:MovieClip = new Yblock();
var yblock3:MovieClip = new Yblock();
I have them added to an array
var blockarray:Array = new Array(rblock1, rblock2, rblock3, yblock1, yblock2, yblock3);
var block
I want to create a for loop with an if statement that triggers if a variable is Rblock and not Yblock, for example
for each (block in blockarray)
{
if (block==Rblock)
{
trace("rblock");
}
}
The issue is that obviously "if (block==Rblock)" doesnt work.
How should this be written?
You apparently want to check if a block is red or yellow by checking against its class name. You can do it with this:
if (block is Rblock) {...} // yes, red
I have figured out a work around not really a perfect solution, which will only work for certain scenarios...
if each class has a unique trait you can identify it that way, for example...
if all variables defined by the Rblock class are wider than the Yblock class you could say
if (block.width>x) { trace(Rblock); }
Like I said this is only a work around though and only works for movieclip variables defined by classes that are different, if anyone has the actual solution please post
for example
var imageViewArray:UIImageView = [imageView1,imageView2,imageView3]
I want to chage sameimageView.image = img or imageView.isUserInteractionEnabled = false to all Image View inside the array
1. It's an array
First of all it's not
var imageViewArray:UIImageView
but
var imageViewArray:[UIImageView]
because you want an array of UIImageView right?
2. Naming conventions
Secondly is Swift we don't name a variable after it's type so imageViewArray becomes imageViews.
3. map
Now if you really hate the for in and the foreach your can write
imageViews = imageViews.map { imageView in
imageView.isUserInteractionEnabled = true
return imageView
}
or as suggested by Bohdan Ivanov in the comments
imageViews.map { $0.isUserInteractionEnabled = true }
4. Wrap up
This answer shows you how to use the wrong construct (map) to do something that should be made with the right construct (for in).
That's the point of having several constructs, everything could be made with an IF THEN and a GOTO. But a good code uses the construct that best fits that specific scenario.
So, the best solution for this scenario is absolutely the for in or the for each
imageViews.forEach { $0.isUserInteractionEnabled = true }
I suppose I need to be able to get the name of the class an object belongs to.
1: I have two classes
CandyBar
BottledWater
2: I put their names in two arrays
var foodArray(CandyBar, BottledWater);
var waterArray();
3: I add children to stage using the array; like this
var foodItem = new foodArray[i];
4: I later try and move the reference for BottledWater to the waterArray, but can't. I'm not creating any new object or anything, just moving the reference from one array to another. Furthermore, I cant remove the reference either.
I suppose I could say I am adding children from an array, then wanting to push a reference to that "class" into another array, while removing the reference from the original array.
The proper way to make an Array variable is:
var foodArray: Array = [CandyBar, BottledWater];
var waterArray: Array = new Array();
than you can do
waterArray.push( new foodArray[0] );
or
var foodItem = new foodArray[i]();
i have an array.
At some stage, i'm adding more data to it.
So we have:
$editable = someArrayGeneratingFunctionHere();
$points = preg_split('/,/',$this->data['Video']['points']);
lovely. Now, the "points" array has a bunch of data that may or may not already be in the editable array.
What i want is to check if the data is in editable, and add if not.
I'd like to do this efficiently too.
So, i have this method:
private function associateWithRelatedBodyParts($editable, $keysAlreadyPresent, ...){
$point = getOtherPointsThatAreRelatedToThisPoint();
if (!isset($keysAlreadyPresent[$point])){
insertDataIntoEditable();
} //else the value is already here. Do not add it again!
return $editable;
}
so the whole thing looks like this:
$editable = someArrayGeneratingFunctionHere();
$points = preg_split('/,/',$this->data['Video']['points']);
$valuesInEditable = ...
foreach ($points as $point){
$editable = $this->associateWithRelatedBodyParts($editable, $valuesInEditable,...);
}
What a lot of setup! The whole point of all this is thus:
i want to flatten the original $editable array, because that way, i can quickly test if a point is in the editable. If it is not, i'll add it.
My current way to retrive the valuesInEditable array is
$valuesInEditable = Set::combine($editable, 'BodyPartsVideo.{n}.body_part_id','BodyPartsVideo.{n}.body_part_id');
This is moronic. I'm sticking the same value twice into the array. What i'd really like is just:
$valuesInEditable = Set::combine($editable, 'BodyPartsVideo.{n}.body_part_id',True);
or something like that. So the whole point of this question is:
how do i set a default value using Set:combine in cakephp. If you have a better suggestion, i'd love to hear it.
Without seeing the structure of your array, it seems like you are going through a lot of work to combine the two arrays. Is there a reason you cannot use
$final_array = array_merge($points, $editable);
before running the set combine?
I'm using a parent to pass a multi-dimensional array to a child. Structure of the array, named projectPositions is as follows (with example data):
projectPositions[0][0] = 1;
projectPositions[0][1] = 5;
projectPositions[0][2] = '1AD';
projectPositions[0][3] = 'User name';
I need to take this inherited array and turn it into an arrayCollection so that I can use it as a dataProvider. Currently, my init function (which runs onCreationComplete) has this code in it to handle this task of array -> arrayCollection:
for (var i:int = 0; i < projectPositions.length; i++)
{
tempObject = new Object;
tempObject.startOffset = projectPositions[i][0];
tempObject.numDays = projectPositions[i][1];
tempObject.role = projectPositions[i][2];
tempObject.student = projectPositions[i][3];
positionsAC.addItemAt(tempObject, positionsAC.length);
}
Then, during a repeater, I use positionsAC as the dataprovider and reference the items in the following way:
<mx:Repeater id="indPositions" dataProvider="{positionsAC}" startingIndex="0" count="{projectPositions.length}">
<components:block id="thisBlock" offSet="{indPositions.currentItem.startOffset}" numDays="{indPositions.currentItem.numDays}" position="{indPositions.currentItem.role}" sName="{indPositions.currentItem.student}" />
</mx:Repeater>
This all works fine and returns the desired effect, but the load time of this application is around 10 seconds. I'm 99% sure that the load time is caused by the array -> arrayCollection for loop. Is there an easier way to achieve the desired effect without having to wait so long for the page to load?
The issue your having loading items could be because you are using a repeater instead of a list class.
With a repeater, there will be a block created in memory, and drawn on the screen. So, if you have 100 items in your array, then 100 blocks will be created. this could slow down both initial creation and the overall app.
A list based class focuses on a technique called renderer recycling; which means only the displayed elements are created and rendered on the screen. So, depending on settings, you'd usually have 7-10 'block' instances on the screen, no matter how many items you have in your array.
change
positionsAC.addItemAt(tempObject, positionsAC.length);
to
positionsAC.addItem(tempObject);
addItemAt is causing a reindex of the collection which can greatly slow down the collection.
[EDIT]
Put this trace statement before and after the loop
take the output and subtract one from the other and that will show how many milliseconds the loop has run.
var date:Date = new Date( );
trace( date.getTime())