Xcode dynamically create arrays depending on user? - iphone

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.

Related

SCNNode names, suggestions for a unique value?

My app reads a text file line-by-line, copies the data into an array of Card objects, and then creates SceneKit nodes for each one. Some of the Cards modify or make iterative copies of the data on earlier lines, so there is no 1:1 correspondence between line numbers and the number of SCNNodes.
I'm trying to come up with a way that I can uniquely name the nodes when they are created so that I can use childNodeWithName(_:recursively:) to find them if. Using the Cards index in the array is not useful because that might change - the user might add or remove objects or reorder them, for instance.
Is there some sort of unique ID or hash on the Card (or any) objects themselves that I can access that I might use for this task?
the answer to your question depends on what a Card represents and is completely independent of SceneKit. I guess it's up to you to find a hash function that works well for what's contained in a Card, or you can simply store a NSUUID in the Card's initializer.

Dynamically declaring number of buttons in a for loop in 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

Efficiently accessing array within array

I have a data type called Filter which has an NSMutableArray property which holds a bunch of FilterKey objects (different amount of keys for each filter). I have a bunch of these Filter objects stored in an NSMutableArray called Filters.
I have a UITableView for which each row is populated with data from one of the FilterKey objects. My question is, for a given row in the UITableView, how can I use the Filters array to find the right FilterKey (considering I've already put the Filters and Keys in order manually)?
Basically, I know I could just traverse through the Filters array and for each Filter object, traverse through all it's FilterKeys, but I'm wondering is there a better way to do this (ie better data structure, or something that would give me the right object faster?
Sorry if this is confusing if you have any questions please let me know in the comments.
Typically you would use sections and rows for this, where each section is a Filter and each row is a FilterKey.
It sounds like you just want to show the filter keys, and not have section headers for their filters (if I'm reading your post correctly). If you don't actually want headers, that's fine, just return 0 for tableView:heightForHeaderInSection: and nil for tableView:viewForHeaderInSection:.
All of this is really more for convenience than performance. It is unlikely that it will be much faster than running through the filters and adding up the counts of their keys. That kind of operation is extremely fast. But sections/rows maps your data better, so I'd probably use it anyway to keep the code simpler.
You can use NSMutableDictionary which is hash-mapped resulting in faster, easier, readable operations.
If you prefer arrays then there is no need to traverse to search for a specific value, you can use NSPredicate to filter your array.

Storing the order of embedded documents in a separated array

I have a set of objects that the user can sort arbitrarily. I would like to make my client remember the sorting of the set of objects so that when the user visits the page again the ordering he/she chose will be preserved. However, the client-side framework should also be able to quickly lookup the objects from whatever array/hashmap they are stored in based upon the ordering. What is the most efficient way of doing this?
The best way I have found for doing this is using an array that stores the IDs of the array in the particular order I wanted. From there, I can access the array of objects I wanted by converting the array to a hashmap using Underscore.js.

Populate separate arrays in iPhone from data in NSMutableArray

In one of my applications I have a requirement to fetch data from an NSMutableArray and store the data which we got in separate arrays in iPhone. Please provide a solution for me. How do I accomplish this?
Requirements are listed below:
Actually total projects are available in one array (this I got from server).
Total number of filters available in another array (this also I got from server).
Based on each filter (each filter is linked with few projects) I have to fetch projects from projects array.
I am able to fetch projects related to separate filter, but the problem is that after fetching the projects I have to store those projects in separate new arrays related to each filter (means here I have to create new array in runtime).
It is here that I am having trouble. How do I create arrays dynamically at runtime instead of declaring them in advance? Because the filters count will vary at any point of time at server side, I cannot predict in advance how many arrays I need.
I’m following this approach, but this is the issue I am facing.
Create one Mutable Dictionary in which add the project with respective filter and add that object in new NSMutable array.This you will each different project with respective filter.You can fetch the projects form respective Key for project.