🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of xhruso00

xhruso00's solution

to Allergies in the Objective-C Track

Published at Dec 10 2018 · 0 comments
Test suite

Given a person's allergy score, determine whether or not they're allergic to a given item, and their full list of allergies.

An allergy test produces a single numeric score which contains the information about all the allergies the person has (that they were tested for).

The list of items (and their value) that were tested are:

  • eggs (1)
  • peanuts (2)
  • shellfish (4)
  • strawberries (8)
  • tomatoes (16)
  • chocolate (32)
  • pollen (64)
  • cats (128)

So if Tom is allergic to peanuts and chocolate, he gets a score of 34.

Now, given just that score of 34, your program should be able to say:

  • Whether Tom is allergic to any one of those allergens listed above.
  • All the allergens Tom is allergic to.

Note: a given score may include allergens not listed above (i.e. allergens that score 256, 512, 1024, etc.). Your program should ignore those components of the score. For example, if the allergy score is 257, your program should only report the eggs (1) allergy.


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/allergies/Allergies.m

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


Jumpstart Lab Warm-up http://jumpstartlab.com

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("AllergiesExample.h")
# import "AllergiesExample.h"
# else
# import "Allergies.h"

@interface AllergiesTest : XCTestCase


@implementation AllergiesTest

- (NSArray<NSNumber *> *)allAllergens {
    return @[@(AllergenEggs), @(AllergenPeanuts), @(AllergenShellfish), @(AllergenStrawberries), @(AllergenTomatoes), @(AllergenChocolate), @(AllergenPollen), @(AllergenCats)];

- (void)testAllergicToEggs { //!OCLint
    Allergies *allergies = [[Allergies alloc] initWithScore:1];
    XCTAssertTrue([allergies hasAllergy:AllergenEggs]);
    XCTAssertFalse([allergies hasAllergy:AllergenPeanuts]);
    XCTAssertFalse([allergies hasAllergy:AllergenPollen]);

- (void)testAllergicToChocolate { //!OCLint
    Allergies *allergies = [[Allergies alloc] initWithScore:32];
    XCTAssertTrue([allergies hasAllergy:AllergenChocolate]);
    XCTAssertFalse([allergies hasAllergy:AllergenEggs]);
    XCTAssertFalse([allergies hasAllergy:AllergenTomatoes]);

- (void)testBob { //!OCLint
    Allergies *allergies = [[Allergies alloc] initWithScore:34];
    XCTAssertTrue([allergies hasAllergy:AllergenPeanuts], "Bob is allergic to peanuts");
    XCTAssertTrue([allergies hasAllergy:AllergenChocolate], "Bob is allergic to chocolate");
    XCTAssertFalse([allergies hasAllergy:AllergenCats], "Bob is not allergic to cats");

- (void)testEggsAndCats { //!OCLint
    Allergies *allergies = [[Allergies alloc] initWithScore:129];
    XCTAssertTrue([allergies hasAllergy:AllergenEggs]);
    XCTAssertTrue([allergies hasAllergy:AllergenCats]);
    XCTAssertFalse([allergies hasAllergy:AllergenChocolate]);

- (void)testNone {
    Allergies *allergies = [[Allergies alloc] initWithScore:0];
    for (NSNumber *allergen in [self allAllergens]) {
        XCTAssertFalse([allergies hasAllergy:[allergen intValue]]);

- (void)testAll {
    Allergies *allergies = [[Allergies alloc] initWithScore:255];
    for (NSNumber *allergen in [self allAllergens]) {
        XCTAssertTrue([allergies hasAllergy:[allergen intValue]]);



//Created __DATE__

#import "Allergies.h"

@interface Allergies() {
    NSUInteger _score;

@implementation Allergies

- (instancetype)initWithScore:(NSUInteger)aScore
    self = [super init];
    if (self) {
        _score = aScore;
    return self;

- (BOOL)hasAllergy:(AllergyName)allergy
    return _score & allergy;



#import <Foundation/Foundation.h>

typedef NS_OPTIONS(NSUInteger, AlergyName) {
    AllergenEggs = 1ULL << 0,
    AllergenPeanuts = 1ULL << 1,
    AllergenShellfish = 1ULL << 2,
    AllergenStrawberries = 1ULL << 3,
    AllergenTomatoes = 1ULL << 4,
    AllergenChocolate = 1ULL << 5,
    AllergenPollen = 1ULL << 6,
    AllergenCats = 1ULL << 7

@interface Allergies : NSObject

- (instancetype)initWithScore:(NSUInteger)aScore;
- (BOOL)hasAllergy:(AlergyName)allergy;


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?