foreach limit in swiftUI - arrays

What is best way to limit ForEach count? Let me know if anybody knows the best way to limit this.
I tried "arrayItem.prefix(n)" but I found no changes.
ForEach(0..<viewModel.prop.prefix(1), id: \.self) { item in

Related

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]

How to efficiently get a collection that contains multiple nested collections

Title might not be the best, an improvement would be much appreciated!
What i mean is actually very simple question:
Say i have a School class containing an array of Students that has an array of Notebooks, and i want to refer all the Notebooks in my School
public class School{
students: Student[];
}
public class Student{
notebooks: Notebook[]
}
How should i refer to all of the Notebooks in my school?
Looking for a more efficient way from saving all the Notebooks in a separate array achieved with a for loop..
Thanks in advance :)
It all depends on what you want to do with the output or each individual notebook? This function closely replicates to for loops without all the boilerplate.
school.students.forEach(function(student) {
student.notebooks.forEach(function(notebook) {
doSomethingWithNotebook(notebook);
});
});
// this returns all notebooks for all students and puts it into a single (flat) array
var allNotebooks = school.students.reduce(function(collection, student) {
return collection.concat(sudent.notebooks);
}, []);
// now you can do things like filter out only New notebooks
var newNotebooks = allNotebooks.filter(notebook => notebook.isNew);
Conceptually, my reflex would be to look for something like an n-dimensional array/matrix, think
myMatrix[ [school][student][notebook[]] ]
(but with valid syntax),
and then figure out how to pull myMatrix[[anything][anything][Notbooks[]]].
This would end up avoiding the for loop, but anything that's going to let you select all of the 3nested elements I'm pretty dang sure is going to be just as complex with those elements stored nested or not.
Not a definitive answer per se, but without more details on what you need, the best I can do is offer up avenues of thought.

I have ten buttons, I need to know which button the user has selected and save that information to the users profile

Ok so I have been trying to figure this out for ages to no avail!!!
I have a list of 10 buttons, the user picks 1 of these buttons and I need to save that result to my users profile which is on a different View Controller. I was trying to put the buttons in an array but was not able to. Any help would be appreciated, I am very new to swift and xcode!
I tried adding them to a outlet collection but it didn't work either.
var genreArray = ["rockGenreButton", "bluesGenreButton","funkGenreButton", "indieGenreButton", "metalGenreButton", "popGenreButton", "altGenreButton","electronicGenreButton", "countryGenreButton", "tradGenreButton"]
Above is the array I created I am lost on which step to take next how do I call which [0] of the array was selected and save to currentUser?
Sorry if this is a really stupid question but I can't find a relevant answer anywhere!
You can add button tag and find which button has been clicked.
once you have created outlet for all buttons (same function)
#IBAction func btnClick(sender: AnyObject) {
print(sender.tag.description)
}
just ctrl click each button to same action
to get the tag of the pressed button
Please let me know if you need more explanation.
Using the tag property of views encourages lazy/bad design in software. It is storing data in views.
Not only that but how is any other developer (including yourself in 6 months time) going to know what if(btn.tag == 4) actually means? It hides the actual purpose of what you're doing.
One of the simplest ways to do something like this in a much better design approach is to create separate functions. One for each button. There is no reason for all your buttons to run the same function.
The first improvement would be to have functions like buttonOnePressed, buttonTwoPressed and so on.
#IBAction func buttonOnePressed {
// do something
}
Again though you are hiding the actual meaning of your app.
You could give them names like...
#IBAction func rockGenreButtonTapped {
// do something
}
#IBAction func bluesGenreButtonTapped {
// do something
}
That's much better. I now know just by looking at the function when it will run.
Maybe a better approach is to name the function as to what it's actually going to do...
#IBAction func showRockGenreDetails {
// present Rock Genre screen
}
#IBAction func showBluesGenreDetails {
// present Blues Genre screen
}
This time I not only know that it's an action (IBAction) I also know just by reading the function name what it is going to be doing.
As was mentioned in the comments. You could also do things like using a UITableViewController to present a list of genres. This is actually a much better approach than what I have shown here but is much more difficult to explain easily.

Selection of multiple values in ListBox

What I have is two indexes. I want to select both the indexes from a ListBox but when I use SelectedIndex = count (that's where the index is stored), only the last index is selected. I have also tried using selectedItems.add(count) but the result doesn't show up. Here's my code:
if (checkedvalues_forclass[j] == subjectid_forreverse[count])
{
listBox1.SelectedItems.Add(count);
//or
listBox1.SelectedIndex=count;
}
Can anyone help me with this?
The solution to your problem is the following:
listBox1.SelectionMode = SelectionMode.Extended;
listBox1.SelectedItems.Add(listBox1.Items[firstindex]);
listBox1.SelectedItems.Add(listBox1.Items[secondindex]);
These two links might help you:
http://marlongrech.wordpress.com/2009/06/02/sync-multi-select-listbox-with-viewmodel/
http://www.gbogea.com/2010/01/02/mvvm-multiselect-listbox

How can i get the number of the place in an array (AS3)?

if I have an array like this:
var myArray:Array = ["apple","orange","strawberry"];
then I want to get the order number of the orange, which is 1, out. Is there any way to do that?
If you want the index of "orange" you could do this:
trace(myArray.indexOf("orange")); // returns 1
If you want to get a hold of data in a few different ways it might be worth using a dictionary object.
http://gskinner.com/blog/archives/2006/07/as3_dictionary_.html
Refer the following link, then will know about all feature of arrays.
http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Array.html

Resources