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]();
Related
I have a custom class "Book" and I am trying to save two books in an array in the following way:
class ViewController: UIViewController, UITableViewDataSource,
{
testBook.setBookTitle(title: "Harry Potter")
testBook.setbookPage(page: 12)
myBookCollection.append(testBook)
// testBook = Book()
testBook.setBookTitle(title: "testing")
testBook.setbookPage(page: 66)
myBookCollection.append(testBook)
for books in myBookCollection
{
print(books.getBookTitle())
}
}
If I run the above I my books in the array are stored as ["testing","testing"] as opposed to ["Harry Potter","testing"].
However If I uncomment testbook = Book() it works fine. Can someone please explain why this is the case ?
thanks,
Reference vs. value type.
If you define Book as a class, when you add it to array, it does not copy our object. testBook and myBookCollection[0] are holding the same object. When you change either one, the other will be affected. If you add testbook = Book(), then testBook is now pointing to a different object in memory and what you do it it has no impact on myBookCollection[0].
You can change Book from a class (reference type) to a struct (value type). In that case, when you add it to an array, it will add a copy to the array so what you do with testBook after doesn't affect it.
Here's a video from WWDC that demonstrates the exact problem you had: Building Better App with Value Types in Swift
the array doesn’t hold a copy of your Book instance , it simply hold the pointer to the object(book). When you change the title and page you are not changing the array contents, you are changing the object which testbook is pointing to.
however if you uncomment the line testBook = Book() . You create a new instance of Book class and you set the pointer testbook to the newly created object and all the changes from this point is done on the new object and that’s why it works as it should.
And all of this is true if your are using a class in your array , if you use struct that’s another story
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..
Currently working on part of the app that requires cloning an element from the content, then modifying cloned element and saving back to model. I am having a problem when saving the cloned element and having to replace the old item with cloned one. What I am currently doing is changing all the properties of the old item like so (it works):
Blocks.replace = function(item1, item2) {
for(var k in item2) {
Ember.set(item1, k, item2[k]);
}
};
var selectedEmployment = this.get("controllers.employmentDataEntry").get("selectedEmployment");
var modelItem = content.findBy("#id", selectedEmployment["#id"]);
Blocks.replace(modelItem, selectedEmployment);
I'm trying to use the ArrayController replaceContent method, but I get an error saying "Invalid array length" when trying to run the following code:
var employmentIndex = content.indexOf(modelItem);
this.replaceContent(employmentIndex, 0, selectedEmployment);
Am I doing this incorrectly? Is there a better way of replacing an item?
Note: I am using JSON as a model. The ArrayController that is used when calling replaceContent contains an array of length 2.
Fix: Need to send in an array to replaceContent method. So change selectedEmployment to [SelectedEmployment]. Also, change 0 to 1, otherwise, content will end up having both element and cloned element.
this.replaceContent(employmentIndex, 1, [selectedEmployment]);
I've imported several images into an actionScript 3 document. I've turned them all into symbols (movie clips) and given them instance names to reference from ActionScript.
Ok, so I'm putting the instances into an array so I can loop through them easily, but for some reason, whenever I'm putting in the instance name, I do a trace on the value in the array and it's giving me the symbol object back, rather than the instance object.
Basically trying to loop through the array to make each instance's visibility = false
Here's a sample:
var large_cap_extrusion_data: Array = new Array();
large_cap_extrusion_data[0] = large_cap_extrusion_menu_button;
large_cap_extrusion_data[1] = extrusion_border_large_cap
large_cap_extrusion_data[2] = "Large Cap";
large_cap_extrusion_data[3] = large_cap_main_menu_button;
var extrusion_data: Array = new Array();
extrusion_data[0] = large_cap_extrusion_data;
trace(extrusion_data[0][0]);
The traces gives:
[object large_cap_menu_button]
(the parent symbol)
rather than:
"large_cap_extrusion_menu_button"
I'd be very grateful if someone could tell me where I'm going wrong...
when you trace and object, by default it describes it type. What you want is the "name" property of the object.
Try this:
trace(extrusion_data[0][0].name);
that should give you the instance nema of the large_cap_menu_button rather than the class description. Either way, you have the right object I bet.
I want to save my Array's strucure and load it the next time I open my AIR application. Is there a way to store it to an EncryptedLocalStore item then get it later when I re-open the app?
EncryptedLocalStore.setItem() method takes a byte array when storing contents. To store an array, just use ByteArray.writeObject() method (as described in http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/ByteArray.html#writeObject()) to convert your Array to a ByteArray - and then persist the same to the ELS.
var array:Array = getArray();
var byteArray:ByteArray = new ByteArray();
byteArray.writeObject(array);
EncryptedLocalStore.setItem('somekey', byteArray);
Hope this helps.
Update: Added code to retrieve the array back.
var byteArray:ByteArray = EncryptedLocalStore.getItem('somekey');
var array:Array = byteArray.readObject() as Array;
Update: For custom classes.
In case you want to serialize your own custom classes to the ByteArray, you may have to call registerClassAlias() before writing the object to the ByteArray. For eg.
registerClassAlias("com.example.eg", ExampleClass);
I have found that it is easiest to to serialize the Array to a string and then store that string in the ELS. Then when you pull it out deserialize it back into an Array.