A Swift introduction into fitness

Many people think getting fit is hard, the fitness industry is full of myths and unnecessary complications.

Doesn’t matter if your goal is to lose weight or gain it, what matter is Physics and law of energy:

If you burn more calories than you eat you will lose weight

Two questions arise:

  1. how to estimate our caloric needs?
  2. how fast should we lose or gain weight?

We'll use Swift Playgrounds to explain it.

Defining user

Before we start calculating our caloric needs we will need to have access to our user stats.

We could define a struct for our user or we can simplify it and use tuple:

enum Gender {
  case Male
  case Female
}

typealias User = (gender: Gender, age: Int, weightInKg: Double, heightInCm: Int, bodyfat: Int?)

Don’t worry if you use Imperial system for measurements, let’s introduce conversion functions:

func toKg(#lbs: Double) -> Double {
  return lbs / 2.2
}

func toCm(#feet: Double, #inches: Double) -> Double {
  return feet * 30.48 + inches * 2.54
}

Now we can setup our user by simply assigning it to variable:

var user = User(gender: .Male, age: 26, weightInKg: 73, heightInCm: 184, bodyfat: 10)

How to estimate calories we should eat?

The only real way to know how much calories you should eat would be to track your food intake and weight changes over couple of weeks, then we can calculate your personal calories accurately.

But there are few math approximations we can use as a starting point, we’ll take a look at 2 most accurate ones but first some theory.

There are few components we need to consider when calculating our caloric needs:

BMR - Basal Metabolic Rate

BMR defines bulk of our caloric needs, it can be thought of as calories you would need if you stayed in bed all day and didn’t do anything eg. Comatose. Resting Metabolic Rate can be considered same as BMR .

It will decrease with age or with loss of LBM - lean body mass, if you have more muscles then you’ll have higher BMR, but the effect of having more muscle mass doesn’t have (as previously thought) high enough effect on BMR to be used for fat loss purpose (dreamers bulking to have more muscle and thinking that will make your fat loss phase easier is just stupid).

Since we will be using one of two functions to calculate BMR, let’s create a typealias for our calculator:

typealias BMRCalculator = () -> Double

NEAT and Activity Factor- Non-Exercise Associated Thermogenesis

NEAT is calories we burn doing stuff that’s not exercise, eg. washing/talking/working.

NEAT is calculated by selecting an activity factor multiplier dependent on your lifestyle, let’s define some common values:

enum ActivityFactor: Double {
  case Sedentary = 1.2 // Little or no exercise and desk job
  case LightlyActive = 1.375 // Light exercise or sports 1-3 days a week
  case ModeratelyActive = 1.55 // Moderate exercise or sports 3-5 days a week
  case VeryActive = 1.725 // Hard exercise or sports 6-7 days a week4
  case ExtremelyActive = 1.9 // Hard daily exercise or sports and physical job
}

TEF - Thermic Effect of Feeding

TEF is used to calculate amount of calories you burn while eating, it will mostly depend on the amount of Protein and Fiber content in your diet as they influence how much energy is needed for converting food into energy.

It does NOT depend on the amount of meals you have, doesn’t matter if you eat 2k calories in 1 or 6 meals, TEF is percentage based on total intake.

Values would vary depending on protein intake mostly:
0.05 to 0.15 from low to high protein intake (1-3g of protein / kg), but for most people starting out this difference will fall into calculation error margin so let’s not worry about it for now.

MET - Metabolic Effect of Training

MET is amount of calories you burn during exercise, as such it only applies for training days.

TDEE - Total Daily Energy Expenditure

TDEE is total amount of calories you need to maintain your current weight, it’s affected by all of the above, but we’ll be calculating TDEE without taking TEF (because for beginners it will be negligible) and MET (only applies to training days).

We can define TDEE as follows:

func TDEE(bmrCalculator: BMRCalculator, activityFactor: ActivityFactor) -> Int {
  return Int(bmrCalculator() * activityFactor.rawValue)
}

BMR Equations

Two most accurate equations for calculating BMR are:

  1. Cunningham Equation - if we know user lean body mass (simply weight * (100-bodyfat%))
  2. Mifflin St Jeor - if body fat is unknown

Let’s write a function generator that will return a proper calculator for a specific user:

func lbm(user: User) -> Double {
  return user.weightInKg * (100.0-Double(user.bodyfat!))/100.0
}

func BMRCalculatorForUser(user: User) -> BMRCalculator {
  func cunninghamCalculator(user: User) -> BMRCalculator {
    return { 500 + 22 * lbm(user) }
  }
  
  func mifflinCalculator(user: User) -> BMRCalculator {
    let genderAdjustment = user.gender == .Male ? 161.0 : -5.0
    return { 10.0 * user.weightInKg + 6.25 * Double(user.heightInCm) + genderAdjustment }
  }
  
  if let bodyfat = user.bodyfat {
    return cunninghamCalculator(user)
  }
  return mifflinCalculator(user)
}

Calculating maintenance

Now we have enough code to calculate our TDEE needs:

user = User(gender: .Male, age: 26, weightInKg: 73, heightInCm: 184, bodyfat: 10)
let tdee = TDEE(BMRCalculatorForUser(user), ActivityFactor.Sedentary)
println("You can maintain your weight by eating \(tdee) kcal daily")

Personal Goals

Now that we have calories to maintain our weight, let’s look at possible goals for a user:

  1. Fat Loss - we want to cut some fat
  2. Muscle gain - when we want to gain muscle

I didn’t use word weight here for a reason, when you bulk you want to gain muscle, you don’t want to get fat.
Same goes for fat loss, we don’t want to lose hard earned muscle tissue, only fat and as such aiming at highest possible weight loss is usually mistake.

How fast can we change our weight?

1 KG of weight is considered to amount to ~7700 calories:

func caloriesForWeight(kgs: Double) -> Double {
  return kgs * 7700
}

Fat loss

It could be fast if you were willing to really restrict calories, but one have to be careful to avoid muscle loss. Starvation mode is a myth but you might be miserable when you do very strict diet. I’d suggest going slower but steady, having around 0.5 kg fat loss per week wouldn’t be bad, in 10 weeks you’d have 5 kg of FAT less.

If you feel like it’s a bit too fast or to slow, adjust the value and see how much less you’d need to eat daily to hit your desired speed:

println("daily deficit: \(caloriesForWeight(0.5) / 7.0)")

Muscle gain

Muscle gain is hard, we can’t just overeat and expect to gain more muscle, doing so will end up with getting fat and having to go into Fat Loss phase, it will make you miserable in long run.

Avoid dreamers bulk, better to go slow and steady, what should be your weekly aim? It will heavily depend on your current conditioning:

On average, a guy doing everything right (Perfect diet etc.) will be doing very well to gain 0.2kg of muscle per week. A female might gain half that or about 0.2kg of muscle every 2 weeks.

The more advanced you are, the slower you should be gaining weight to avoid getting too much fat.

So we have 2 weight goals, bulking and cutting:

typealias Goal = () -> Double
func bulking(#kgPerWeek: Double) -> Goal {
  return {+kgPerWeek/7}
}
func cutting(#kgPerWeek: Double) -> Goal {
  return {-kgPerWeek/7}
}

Final calculation

Now we can finally calculate daily calories needed to reach our goals:

func dailyCalories(tdee: Int, #goal: Goal) -> Int {
  return Int(Double(tdee) + (caloriesForWeight(goal())))
}

let target = dailyCalories(tdee, goal: bulking(kgPerWeek: 0.2))
println("You should be eating \(target) kcal everyday")

Conclusion

This should make starting your fitness journey easier, it also shows how cool playgrounds are.

Related:

You've successfully subscribed to Krzysztof Zabłocki
Great! Next, complete checkout to get full access to all premium content.
Error! Could not sign up. invalid link.
Welcome back! You've successfully signed in.
Error! Could not sign in. Please try again.
Success! Your account is fully activated, you now have access to all content.
Error! Stripe checkout failed.
Success! Your billing info is updated.
Error! Billing info update failed.