LodgeApps LodgeApps - 1 year ago 72
Objective-C Question

Linking Text to an Integer Objective C

The goal of this post is to find a more efficient way to create this method. Right now, as I start adding more and more values, I'm going to have a very messy and confusing app. Any help is appreciated!

I am making a workout app and assign an integer value to each workout. For example:

Where the number is


01 is High Knees

02 is Jumping Jacks

03 is Jog in Place


I am making it so there is a feature to randomize the workout. To do this I am using this code:

-(IBAction) setWorkoutIntervals {
exerciseInt01 = 1 + (rand() %3);
exerciseInt02 = 1 + (rand() %3);
exerciseInt03 = 1 + (rand() %3);

So basically the workout intervals will first be a random workout (between high knees, jumping jacks, and jog in place). What I want to do is make a universal that defines the following so I don't have to continuously hard code everything.

Right now I have:

-(void) setLabelText {
if (exerciseInt01 == 1) {
exercise01Label.text = [NSString stringWithFormat:@"High Knees"];
if (exerciseInt01 == 2) {
exercise01Label.text = [NSString stringWithFormat:@"Jumping Jacks"];
if (exerciseInt01 == 3) {
exercise01Label.text = [NSString stringWithFormat:@"Jog in Place"];

I can already tell this about to get really messy once I start specifying images for each workout and start adding workouts. Additionally, my plan was to put the same code for
, etc. which would become extremely redundant and probably unnecessary.

What I'm thinking would be perfect if there would be someway to say

exercise01Label.text = exercise01Int;
(I want to to say that the Label's text equals Jumping Jacks based on the current integer value)

How can I make it so I only have to state everything once and make the code less messy and less lengthy?

Answer Source

Three things for you to explore to make your code easier:

1. Count from zero

A number of things can be easier if you count from zero. A simple example is if your first exercise was numbered 0 then your random calculation would just be rand() % 3 (BTW look up uniform random number, there are much better ways to get a random number).

2. Learn about enumerations

An enumeration is a type with a set of named literal values. In (Objective-)C you can also think of them as just a collection of named integer values. For example you might declare:

typedef enum
} ExerciseCount;

Which declares ExerciseCount as a new type with 4 values. Each of these is equivalent to an integer, here HighKnees is equivalent to 0 and ExerciseKindCount to 3 - this should make you think of the first thing, count from zero...

3. Discover arrays

An array is an ordered collection of items where each item has an index - which is usually an integer or enumeration value. In (Objective-)C there are two basic kinds of arrays: C-style and object-style represented by NSArray and NSMutableArray. For example here is a simple C-style array:

NSString *gExerciseLabels[ExerciseKindCount] =
   {  @"High Knees",
      @"Jumping Jacks",
      @"Jog in Place"

You've probably guessed by now, the first item of the above array has index 0, back to counting from zero...

Exploring these three things should quickly show you ways to simplify your code. Later you may wish to explore structures and objects.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download