Dynamically declaring number of buttons in a for loop in Swift - swift

Is there a way to dynamically declare a certain number of UIButtons based on the number of iterations in a for loop?
the actual number would be passed in from the user or based on an array length
so pseudo-code would be
for num in total{
//declare a UIbutton with a unique name
}

If you are using UITableView the best way to accomplish this is using the table own behavior to populate its cells automatically with your source date (array, dictionaries, etc) even with data gathered from external sources like a REST service.
The way to do this is creating a custom cell with each outlet and point them to your sources.

After some googling, I think this is called metaprogramming and that swift doesn't have it yet :(
edit: looks like i was way overthinking things. this is actually fairly straightforward if i think about it without looping

Related

auto create stackView from array count

Please tell me if I chose the right approach or are there other options?
I receive an array of objects from the server.
According to the task I have to place each object of the array in the table cell
How many objects will come I don’t know, I just mean that they all have the same structure and that they should be placed automatically.
I chose the path - stack view
Is there any easy way to automatically generate their number?
And whether I think correctly...
I need to create 1n total stack in the cell and transfer new stack to inside
there is response from server
https://jsoneditoronline.org/?id=9ebed4100d4d4db49aab728a16ac693d
each array must be a cell
inside each array - the array is a data set that should be automatically insert ....
maybe in the stack?
Use UITableView. It will be better choise
upd:
Use complex UITableView with sections.
Also some like this can help you: https://medium.com/#stasost/ios-how-to-build-a-table-view-with-multiple-cell-types-2df91a206429

Xcode dynamically create arrays depending on user?

I'm trying to build an iphone fitness app to allow the user to first create 1 routine. then within the routine to create exercises, and within each of those exercises to create sets. I'd like to allow the user to create as many sets/routines.
My plan is to create an array within an array within an array.
The first array will hold the exercises (represented by an array). And within the exercise array will be the sets(represented by an array). And finally the set array will actually store the information.
I guess my question is, is this possible? To dynamically create arrays based on the user? I can't seem to find any information on this subject,.
Yes, this is definitely possible: NSArray and its subclass NSMutableArray let you create and manage arrays dynamically, growing and shrinking them as needed.
Rather than using arrays of arrays of arrays, I would use special-purpose classes that hold arrays, but hide them, and present some functionality that is related to your specific application.
For example, you may want to consider creating a class for Routine and for Exercise. Routine would have methods like
-(void)addExercise:(MyExercise*)exercise;
-(MyExercise)getExerciseForIndex:(int)index;
-(void)removeExerciseAtIndex:(int)index;
and so on, with NSMutableArray serving as a storage for exercises.

best way of handling self-changing array of information

This question is about handling arrays of information, there's are many ways I could do this, but I would like some input from programmers with more experience, I know what I want to do just not how to organize the information the best way, and objective-C is really making me ponder this, I don't want to get 100 hours into work a decide, oops this wasted the beast way to do this. So here goes:
I have a grid where I'm simulating a playing field, each piece of the grid I call a cell. The cells have around 20 different values each, all integers, nothing fancy. A change to a cell will be either by player input, or occur or by surrounding cells through different algorithms.
The changes to cells will occur once a turn is complete, so it's not real time. Now, I'm not even sure about doing this with a MutableArrays, a plain Array, or just a plain matrix. Arrays are good at keeping such info for one dimension, but I would imagine would become quite cumbersome if you have to address a batch of 10,000 of these cells. On the other hand a simple matrix might not be so elegant, but probably easier to work with.
Any insight would be greatly appreciated.
You have two options here that I see:
1) Use standard containers
Assuming that the playing field is of constant size, then you can create a mutable array of x*y size, and populate it with mutable dictionaries. By giving everything in the second mutable dictionary keys, you can query and set their properties (all objects of course, so wrap ints in NSNumbers etc). For indexing use a macro INDEX_FROM_ROW_COL(row, col) and apply the appropriate code to multiply/add.
2) Create a helper object subclassed from NSObject. It would manage mutable objects as above, but you could load it with functionality specific to your application. You could provide methods that have parameters of "row:" and "col:". Methods that change or set properties of each cell based on some criteria. Personally, I think this is a better idea as you can incapsulate logic here and make the interface to it more high level. It will make it easier to log whats going on too.

data structure best practices for UITableView datasource

I am trying to figure out whats the best data structure to hold the data source for a UITableView. I am looking for something as robust as possible - a structure that will hold data for the different sections, and which will enable convenient add and remove of objects. I saw a few implementations using NSMutableArrays and dictionaries, but I am still not convinced as to which approach is the most robust.
Appreciate your help and thoughts.
NSMutableArray is the most efficient since the index path for row and section readily convert to integers which can be used to access the array elements.
Depends on really how complex your data is. If its just couple of strings then an Array of NSStrings will suffice. If its like an image path, a text label and its description then maybe an array of custom class holding that data. I almost always go for the Arrays since you can get your desired object from it with the objectAtIndex:indexPath.row bit.

Regarding implementation of multi component dependent uipickerview

I am having trouble grasping the concept of multi component uipickerviews. I really would like to just OWN this subject. I would like to make a 4 component pickerview with components that are dependent on one another.
The first component is being populated from an array from my db, and that is showing up fine. I have all of the other info available in arrays, but I am just getting hung up on the dependent aspect of my pickerview. I figure the best way to make component 2's data depend on comp 1 is to link them somehow within the didSelectRow section. But I don't know the syntax.
I have been working at this for hours now and feel like I am really close, but I just need some help with a few issues. What is the syntax for connecting components so they depend on each other? Something like this? (Which is awful I know, but I am thinking that is the direction I need to get) :
if(picker = pickerComponent1)
//set number of rows for comp2 and also the content etc...
Another issue is determining the numberOfRowsInComponent bit because they aren't in the same method...
If anyone knows about (or wants to give :) a tutorial on this subject, it would be so helpful! Or if you are knowledgeable on the topic and would like to share some of what you know, that would be perfect too. I would really like to see this seemingly simple task completed.
It's quite simple. In the pickerView:didSelectRow:inComponent: method, just call [myPicker reloadComponent:] for all components coming after the one where the selection changed. The picker will then automatically ask its datasource (your view controller, presumably) for the number of rows and the rows' values.
Then, in pickerView:numberOfRowsInComponent: and pickerView:titleForRow:forComponent:, return the appropriate values (the count and contents of the corresponding array) depending on the values of the parent components' selected rows.