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

josepvcliment's solution

to Two Fer in the Objective-C Track

Published at Mar 30 2021 · 0 comments
Instructions
Test suite
Solution

Two-fer or 2-fer is short for two for one. One for you and one for me.

"One for X, one for me."

When X is a name or "you".

If the given name is "Alice", the result should be "One for Alice, one for me." If no name is given, the result should be "One for you, one for me."

Setup

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/two-fer/TwoFer.m

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

Source

https://en.wikipedia.org/wiki/Two-fer

Submitting Incomplete Solutions

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

TwoFerTest.m

#import <XCTest/XCTest.h>

#if __has_include("TwoFerExample.h")
# import "TwoFerExample.h"
# else
# import "TwoFer.h"
#endif

@interface TwoFerTest : XCTestCase

@end

@implementation TwoFerTest

NS_ASSUME_NONNULL_BEGIN
    
- (void)testNoNameGiven {
    XCTAssertEqualObjects([TwoFer twoFerWithName:nil], @"One for you, one for me.");
}

- (void)testANameGiven {
    XCTAssertEqualObjects([TwoFer twoFerWithName:@"Alice"], @"One for Alice, one for me.");
}

- (void)testAnotherNameGiven {
    XCTAssertEqualObjects([TwoFer twoFerWithName:@"Bob"], @"One for Bob, one for me.");
}

NS_ASSUME_NONNULL_END
    
@end
//
//  HelloWorld.m
//  HelloWorldTest
//
//  Created by Josep V. Climent Barreira on 24/3/21.
//

#import "TwoFer.h"

@implementation TwoFer
+(NSString *) twoFerWithName:(NSString *) you;
{
	NSString * name = you ? you : @"you";
	return [NSString stringWithFormat:@"One for %@, one for me.", name];
}
@end

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?