简体   繁体   English

根据字符串范围返回布尔值

[英]Returning Boolean based on range of string

I'm trying to find the string "wolf" in a NSArray and the return back the Boolean value of YES or NO if the word exists or not. 我正在尝试在NSArray中查找字符串“ wolf”,如果该单词存在或不存在,则返回布尔值YES或NO。 I'm having trouble returning the YES or NO. 我在返回“是”或“否”时遇到问题。 My initial thought was to assign an NSInteger or NSNumber to a 1 or a 0 but I can't seem to initialize either properly outside of my if/else statement. 我最初的想法是将NSInteger或NSNumber分配为1或0,但是我似乎无法在if / else语句之外正确地进行初始化。 Any help? 有什么帮助吗? Thanks guys. 多谢你们。

Here's my code: 这是我的代码:

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

NSNumber *characterExistance;

for (NSString* character in characterArray) {

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

    } else {

    }
}

return RETURN BOOLEAN HERE;

} }

Return YES if you find it, otherwise return NO after the for-loop if you don't. 如果找到,则返回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;   
}

For completeness, this is an alternative method that does what you were questioning, creating a variable outside the loop and setting it inside the loop: 为了完整起见,这是一种替代方法,可以执行您要查询的事情,在循环外创建变量并将其设置在循环内:

- (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;
}

This seems to be a fantastic opportunity to demonstrate how a simple unit test can help you feel much more confident about your new code: 这似乎是一个绝佳的机会来展示简单的单元测试如何帮助您对新代码更加自信:

- (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.");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM