基于字符串的范围返回布尔值

时间:2014-09-29 02:10:12

标签: objective-c

我正在尝试在NSArray中找到字符串“wolf”,如果该单词存在与否,则返回布尔值YES或NO。我无法返回YES或NO。我最初的想法是将NSInteger或NSNumber分配给1或0,但我似乎无法在if / else语句之外正确初始化。有帮助吗?多谢你们。

这是我的代码:

- (BOOL) characterArrayContainsWorf:(NSArray *)characterArray {

NSNumber *characterExistance;

for (NSString* character in characterArray) {

    if ([character rangeOfString:@"wolf" options:NSCaseInsensitiveSearch].location != NSNotFound) {

    } else {

    }
}

return RETURN BOOLEAN HERE;

}

1 个答案:

答案 0 :(得分:2)

如果找到则返回YES,否则请在for循环后返回NO

- (BOOL)characterArrayContainsWolf:(NSArray *)characterArray {
    // loop through the array of characters
    for (NSString *character in characterArray) {
        // if you find the wolf, return that you found the wolf
        if ([character rangeOfString:@"wolf" options:NSCaseInsensitiveSearch].location != NSNotFound) {
            return YES;
        }
    }

    // if you get here that means you iterated through each string in
    // the array and none of them contained 'wolf'
    return NO;   
}

为了完整性,这是一种替代方法,可以执行您所提出的问题,在循环外创建变量并将其设置在循环内:

- (BOOL)characterArrayContainsWolf:(NSArray *)characterArray {
    // initialize the BOOL to no to start
    BOOL didFindWolf = NO;

    // loop through the array of characters
    for (NSString *character in characterArray) {
        // if you find the wolf, set that you found the world and break,
        // stopping the for-loop
        if ([character rangeOfString:@"wolf" options:NSCaseInsensitiveSearch].location != NSNotFound) {
            didFindWolf = YES;
            break;
        }
    }

    // this will be YES if we found it, or NO if we didn't
    return didFindWolf;
}

这似乎是展示简单单元测试如何帮助您对新代码更加自信的绝佳机会:

- (void)testWolfSearch {
    NSArray *emptyArray = @[];
    NSArray *nilArray = nil;
    NSArray *noWolfHere = @[@"apple", @"maserati", @"The Spriggan", @"Oedipus Rex"];
    NSArray *almostWolf = @[@"Wol", @"olf", @"w"];
    NSArray *onlyWolf = @[@"wolf"];
    NSArray *onlyWolfCapital = @[@"WOLF"];
    NSArray *containsWolf = @[@"omfg", @"Why am I doing this?", @"wolf", @"The cake is a lie..."];
    NSArray *containsVersionOfWolf = @[@"Gregory", @"WOLFENSTEIN", @"Rubbish"];
    NSArray *containsManyWolves = @[@"wolf", @"wolf", @"DAT WOLF DOE", @"WOLF"];

    XCTAssertFalse([self characterArrayContainsWolf:emptyArray], @"An empty array should return NO.");
    XCTAssertFalse([self characterArrayContainsWolf:nilArray], @"A nil array should return NO.");
    XCTAssertFalse([self characterArrayContainsWolf:noWolfHere], @"An array with multiple values that doesn't contain wolf should return NO.");
    XCTAssertFalse([self characterArrayContainsWolf:almostWolf], @"An array with values close to Wolf, but not, should return NO.");
    XCTAssertTrue([self characterArrayContainsWolf:onlyWolf], @"An array containing a single string that matches should return YES.");
    XCTAssertTrue([self characterArrayContainsWolf:onlyWolfCapital], @"An array containing a single string that matches and it all caps, should return YES.");
    XCTAssertTrue([self characterArrayContainsWolf:containsWolf], @"An array containing many values, one of which is wolf, should return YES.");
    XCTAssertTrue([self characterArrayContainsWolf:containsVersionOfWolf], @"An array that contains a string with wolf as a substring should return YES.");
    XCTAssertTrue([self characterArrayContainsWolf:containsManyWolves], @"An array that contains many versions of the wolf string should return YES.");
}