How to pass the objects of myArray? - arrays

I'm a newbie, I have gone through basics but couldn't figure this one out. I've googled so much but finally I land here.
fontFamily = [[UIFont alloc]init];
fontSize = [[NSMutableArray alloc]initWithObjects:#"10",#"15",#"20",#"25",#"30", nil ];
[text setFont: [[UIFont familyNames] size:<-----
And now, how can I pass the objects to size?

I truly don't know what your looking for. But if you are looking to access the strings you put in the array, you can do so like this:
[fontSize objectAtIndex:2]
You can put that any place you would want to put #"20" since that is what is at index 2.
You should note that you are putting string values in the array not number values.
Edit in response to comment.
You don't really want to just pass the array anywhere what you want to do is something like:
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
// if calling for size component
return fontSize.count;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
// if calling for size component
return [fontSize objectAtIndex:row];
}
If I'm off a little I apologize, I don't use pickerViews much.

Related

How to display element from an array

In the below image there is an array given to me on doing console.log
Array on doing console.log
The name of the array is commentData
if i do commentData.name i get the name
but when i do commentData.replyoncomment.Rname or commentData.replyoncomment[0].Rname
i get undefined even on commentData.replyoncomment
how do i solve this problem
if you need anything else ask me.
Update:
Thats how i turned commentData to an array
const convert = JSON.stringify(comment)
const comments = Object.values(JSON.parse(convert))
const commentData = comments
when i do commentData.name or commentData.commment
it give me the name/comment
but when i do commentData.replyoncomment i get undefined.
see the first image to understand better
on running this i get
{JSON.stringify(commentData)}
[[{"_createdAt":"2022-08-07T14:34:40Z","_id":"dqDIn547oWb3s2Y4OEwhMK","comment":"sdfanj akf js","name":"nkfndm nj","replyoncomment":[]},{"_createdAt":"2022-08-07T03:13:01Z","_id":"SD6gTXwnOmN4hstl5nkSbH","comment":"hello, I'm the admin of the webstie","name":"Mian Mohid Naeem","replyoncomment":[{"Rcomment":"gfdhhh","Rname":"gff","_createdAt":"2022-08-07T10:17:33Z"},{"Rcomment":"print("Hello world")","Rname":"programmer","_createdAt":"2022-08-07T03:13:40Z"}]}]]
anything else you need to see in the code ask me...
You should map the commentData.replyoncomment to, cause it is an array too, and React can't render arrays.
So nest an additional map to the commentData.replyoncomment.
I was willing to help you, if you have any more questions, ask me.

How do I assign a variable to refer to an array?

Disclaimer: I am pretty much a novice to Swift so prepare to cringe.
I have a series of arrays set out, when a user taps a button I would like one of these arrays to be called so I can use it in a function. They are named with thisArray then a number, i.e: thisArray1, thisArray2, etc.
I cannot find a way to make this work, the closest I have come to what I want is shown below but as you can all tell this definitely does not work.
var currentArray = "thisArray" + selectedArrayNumber
The outcome of this is to use the variable like below:
button1.setTitle(currentArray[1], for .normal)
If any of you can shed some light on this situation and tell me how badly I have gone wrong that would be much appreciated.
Create an array of your arrays, and then retrieve it using the selectedArrayNumber.
let arrays = [thisArray1, thisArray2, etc]
let currentArray = arrays[selectedArrayNumber]

Pass current object first Array as3

Is it possible to pass an array whilst placing the currentTarget object first in the array?
I've searched online and only found reference to index, indexOf etc but no resources on how to do this anywhere.
I can send the array no problem but it's always as first loaded, through selecting a different object it must be possible to ammend the array or splice the new object to the beginning but would really be grateful of any assistance as to how to achieve this.
Not 100% sure what you're asking, but you can use the Array::unShift(...args) method to insert an object at the beginning of an array.
Edit:
From your comment below, I think I get what you're saying. You can just swap the item you're looking for with the one at the beginning:
var index = array.indexOf(event.currentTarget);
array[index] = array[0];
array[0] = event.currentTarget;
or something similar to that anyways.

How to effectively garbage collect in AS3

I have a flash game that I'm building where I have an array keeping track of a bunch of power ups on the screen. When the player goes an grabs one it needs to disappear from the screen (which is easy) but it also needs to be removed from the array so that collision detection loops don't become really cumbersome. I tried using splice, but I keep get null reference errors, here's the relevant code.
public function collect():void {
try {
Main.powerUps.splice(index, 1);
stage.removeChild(this);
}catch (e:Error) {
trace("Error in splice");
}
}
}
Then when I create my PowerUp object I pass it a parameter that gets assigned to index which is the length of the array of Power Ups at the time. Can anyone see what's wrong with this code or (preferably) provide a more elegant solution? Thanks in advance for any help.
Elegant solution: use indexOf() and splice() together!
var index:int = Main.powerUps.indexOf( powerup );
Main.powerUps.splice(index, 1);
Where powerup is a reference to the object stored within the array Main.powerUps.
I also created a little class a while back that may be useful to you:
https://github.com/MartyWallace/Lotus/blob/master/Lotus/lotus/utils/Set.as
It has a .remove() method so you can just do:
powerUps.remove(powerup);

Array of Colors error. Bug?

K. I'm getting stuck here.
I'm trying to make an array with different color values.
My problem is that when I do...
teamColor[i] = currentColor... all color values in my array turn into the currentColor.
(I would upload more code, but that would be a massive mess, considering that I have code everywhere with references from movie clips that are as far as 3 layers deep. HOWEVER, this would be irrelevant anyways (probably), because I tested this with color values on my main timeline, without any references to or from anything deeply nested)
I'm GUESSING that this is just some horrible bug, but if it's not (and I hope it isn't), please guide me in what to do to fix this problem.
I would like to add that I tried adding strings in there and that the strings remained their original, intended, value, while the color exhibited the same phenomenon.
[Partially resolved]:
I changed my code by creating separate variables for each color instead of putting the variables into an array (not what I really wanted to do, but it works). My code looks like this:
`
if (teamColor != 0)
{
this["team"+teamColor+"Color"] = new ColorTransform(0,0,0,1,currentColor.redOffset,currentColor.greenOffset,currentColor.blueOffset,0)
teamColor = 0
namebox.addboxes()//function in a movieclip
}`
teamColor is now an int that is changed based on which box a user clicks from a movie clip that has a dynamically generated name, based off of what the variable value in a loop was when it was created. (E.G: 'tempboxname[ttns].name = i;')
teamColor is then equal to that name when the user clicks it.
I have another movieclip with colors in it and the above function is called to check if any teamColor change has occurred, and if it has, act accordingly. (The idea of having teamColor equal to 0 is so that if the user clicks twice, nothing changes. I other conditionals for other colors, all within the same function).
That is how I fixed me code.
It's not what I wanted, because it's not an array (meaning a seemingly infinite number of teamColors, and thus, teams) but it'll do for me. If anyone has any suggestions, feel free to suggest.
I'm no ActionScript wiz, but what it looks like to me is that currentColor is an object that is being passed into the array by reference. This means that all array entries that you assigned currentColor will be pointing at the same currentColor object, not a copy. My advice is to make a copy and then assign that into the array.
It would be much better if you could give me more code to look at. For instance, the loop that contains that code segment would be nice. If I find a different error I'll edit my answer.
here i'm creating and then adding simple 0xRRGGBB color objects into a vector. the color objects are then parsed into 0xRRGGBB hexadecimal strings and traced.
certainly it's not exactly what your looking for, but hopefully it will help you.
var red:uint = 0xFF0000;
var green:uint = 0x00FF00;
var blue:uint = 0x0000FF;
var colors:Vector.<uint> = new Vector.<uint>()
colors.push(red, green, blue);
for each (var color:uint in colors)
{
var output:String = color.toString(16);
while (output.length < 6)
output = "0" + output;
trace("0x" + output.toUpperCase());
}
Output:
//0xFF0000
//0x00FF00
//0x0000FF

Resources