Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of BenWe

BenWe's solution

to Leap in the Objective-C Track

Published at Apr 28 2019 · 0 comments
Test suite

Given a year, report if it is a leap year.

The tricky thing here is that a leap year in the Gregorian calendar occurs:

on every year that is evenly divisible by 4
  except every year that is evenly divisible by 100
    unless the year is also evenly divisible by 400

For example, 1997 is not a leap year, but 1996 is. 1900 is not a leap year, but 2000 is.

If your language provides a method in the standard library that does this look-up, pretend it doesn't exist and implement it yourself.


Though our exercise adopts some very simple rules, there is more to learn!

For a delightful, four minute explanation of the whole leap year phenomenon, go watch this youtube video.


There are two different methods of getting set up to run the tests with Objective-C:

  • Create an Xcode project with a test target which will run the tests.
  • Use the ruby gem objc as a test runner utility.

Both are described in more detail here: http://exercism.io/languages/objective-c

Submitting Exercises

When submitting an exercise, make sure your solution file is in the same directory as the test code.

The submit command will look something like:

exercism submit <path-to-exercism-workspace>/objective-c/leap/Leap.m

You can find the Exercism workspace by running exercism debug and looking for the line beginning with Workspace.


JavaRanch Cattle Drive, exercise 3 http://www.javaranch.com/leap.jsp

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


#import <XCTest/XCTest.h>

#if __has_include("LeapExample.h")
# import "LeapExample.h"
# else
# import "Leap.h"


@interface LeapTest : XCTestCase


@implementation LeapTest

- (void)testVanillaLeapYear {
  Leap *year = [[Leap alloc] initWithCalendarYear:@1996];

- (void)testAnyOldYear {
  Leap *year = [[Leap alloc] initWithCalendarYear:@1997];

- (void)testCentury {
  Leap *year = [[Leap alloc] initWithCalendarYear:@1900];

- (void)testExceptionalCentury {
  Leap *year = [[Leap alloc] initWithCalendarYear:@2400];



#import <Foundation/Foundation.h>


@interface Leap : NSObject

@property BOOL isLeapYear;

- (instancetype) initWithCalendarYear:(NSNumber *)year;




#import "Leap.h"

@implementation Leap

#pragma mark - Init

- (instancetype) initWithCalendarYear:(NSNumber *)year {
    if (self) {
        self = [super init];

        BOOL isDivisibleByFour = (year.integerValue % 4) == 0;
        BOOL isDivisibleByOneHundred = (year.integerValue % 100) == 0;
        BOOL isDivisibleByFourHundred = (year.integerValue % 400) == 0;

        self.isLeapYear = (isDivisibleByFour && !isDivisibleByOneHundred) || isDivisibleByFourHundred;

    return self;


Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other peopleโ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?