【问题标题】:NSString defining variable to go into formatNSString 定义变量进入格式
【发布时间】:2015-11-29 20:00:48
【问题描述】:

我正在做作业,要求我返回一个完整的句子,表明我最喜欢的奶酪。我已经写出了下面的代码,但我不确定在哪里定义cheeseName。另外,我将如何测试代码以查看它是否返回正确的cheeseNameNSLog()

这是我的 .m 文件

#import "StringCheese.h"

@implementation StringCheese

- (NSString *) favoriteCheeseStringWithCheese:(NSString *)cheeseName {
/* WORK HERE */
return [NSString stringWithFormat:@"My favorite cheese is %@.", cheeseName];
/ My answer is the code above/
}

这是我的另一个 .m 文件:

#import <XCTest/XCTest.h>
#import "StringCheese.h"

@interface StringCheeseTests : XCTestCase

@property (nonatomic, strong) StringCheese *stringCheese;

@end

@implementation StringCheeseTests

- (void)setUp
{
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
self.stringCheese = [[StringCheese alloc] init];
}

- (void)tearDown
{
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}

- (void)testThatCheeseFavoritingWorks {
NSString *ricottaString = @"ricotta";
NSString *favoriteCheese = [self.stringCheese favoriteCheeseStringWithCheese:ricottaString];

XCTAssertEqualObjects(favoriteCheese, @"My favorite cheese is ricotta.", @"Incorrect favorite cheese string returned.");

NSString *goatString = @"goat";
favoriteCheese = [self.stringCheese favoriteCheeseStringWithCheese:goatString];
XCTAssertEqualObjects(favoriteCheese, @"My favorite cheese is goat.", @"Incorrect favorite cheese string returned.");

}

编辑:我运行它时的结果

12:21:17.116 xctest[13545:2066149] _XCT_testBundleReadyWithProtocolVersion:minimumVersion: reply received
12:21:17.129 xctest[13545:2066149]  _IDE_startExecutingTestPlanWithProtocolVersion:16
Test Suite 'Selected tests' started at 2015-11-29 12:21:17.150
Test Suite 'StringCheeseTests' started at 2015-11-29 12:21:17.153
Test Case '-[StringCheeseTests testThatCheeseFavoritingWorks]' started.
Test Case '-[StringCheeseTests testThatCheeseFavoritingWorks]' passed  (0.005 seconds).
Test Suite 'StringCheeseTests' passed at 2015-11-29 12:21:17.162.
Executed 1 test, with 0 failures (0 unexpected) in 0.005 (0.009) seconds
Test Suite 'Selected tests' passed at 2015-11-29 12:21:17.165.
 Executed 1 test, with 0 failures (0 unexpected) in 0.005 (0.015) seconds

Test session log:
/var/folders/c0/1phmslcx35ngtg42z44yk75h0000gn/T/com.apple.dt.XCTest-status/Session-2015-11-29_12:21:08-HEws7M.log

Program ended with exit code: 0

【问题讨论】:

  • 这段代码有什么问题?
  • 我不确定我是否正确地编写代码来表明我最喜欢的奶酪。作业说明如下:Creates a sentence indicating a favorite cheese. Example usage: @code NSString *fullSentence = [cheese favoriteCheeseStringWithCheese:@"cheddar"]; // fullSentence is "My favorite cheese is cheddar." @endcode @param cheeseName The name of the favorite cheese @return Returns a full sentence indicating a favorite cheese. */
  • 那么,当你运行它时会发生什么?
  • 在帖子中添加了输出

标签: objective-c cocoa nsstring


【解决方案1】:

要打印出附加值:

NSLog(@"%@", favouriteCheese);

在 - (void)testThatCheeseFavoritingWorks 方法的末尾,它看起来像:

- (void)testThatCheeseFavoritingWorks {
NSString *ricottaString = @"ricotta";
NSString *favoriteCheese = [self.stringCheese favoriteCheeseStringWithCheese:ricottaString];

XCTAssertEqualObjects(favoriteCheese, @"My favorite cheese is ricotta.", @"Incorrect favorite cheese string returned.");

NSString *goatString = @"goat";
favoriteCheese = [self.stringCheese favoriteCheeseStringWithCheese:goatString];
XCTAssertEqualObjects(favoriteCheese, @"My favorite cheese is goat.", @"Incorrect favorite cheese string returned.");

NSLog(@"%@", favoriteCheese);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 2012-10-18
    • 2011-09-16
    • 1970-01-01
    相关资源
    最近更新 更多