Objective-c每隔23个字符换行而不断词

时间:2018-08-16 21:38:11

标签: ios objective-c nsstring nsscanner

我从这篇Objective-c Line breaks every 10 characters (Keeping words in tact)帖子中发现了该算法,问题是它从不返回最后一个单词,它总是以某种方式丢弃它。

NSString *sourceStr = @"The Chocholate Experience in the Anna Ice Cream Museum - Spanish";

NSMutableString *resultString = [[NSMutableString alloc] init];
NSMutableString *currentLine = [[NSMutableString alloc] init];
NSMutableArray *stringsArray = [[NSMutableArray alloc] init];
NSScanner *scanner = [NSScanner scannerWithString:sourceStr];
NSString *scannedString = nil;

while ([scanner scanUpToCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString: &scannedString]) {
    if ([currentLine length] + [scannedString length] <= 23) {
        [currentLine appendFormat:@"%@ ", scannedString];
    }
    else if ([currentLine length] == 0) { // Newline but next word > 23
        [resultString appendFormat:@"%@\n", scannedString];
    }
    else { // Need to break line and start new one
        [resultString appendFormat:@"%@\n", currentLine];
        [currentLine setString:[NSString stringWithFormat:@"%@ ", scannedString]];
    }
    [scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:NULL];
}
NSLog(@"Result String: \n%@", resultString);

I get this as the output: 
The Chocholate 
Experience in the Anna 
Ice Cream Museum -

摆脱了西班牙语的最后一个单词。任何人都可以看到错误吗?我已经在这里呆了几个小时了。任何帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

如果您单步执行操作,则会发现在while循环之外之后,缺少的单词存储在currentLine var中。基本上,最后一个条件将“冰淇淋博物馆-”附加到resultString,但是由于没有其他要扫描的内容(“ Spanish”已经保存在scandString中,因此已经被扫描了),因此只需放置当前行中的单词“ Spanish”,并且不会经过while循环的另一次迭代(因此,永远不要将其放置在resultString中以进行打印)。

以下是修改后的代码,应打印出最后一个单词:

NSString *sourceStr = @"The Chocholate Experience in the Anna Ice Cream Museum - Spanish";

NSMutableString *resultString = [[NSMutableString alloc] init];
NSMutableString *currentLine = [[NSMutableString alloc] init];
NSMutableArray *stringsArray = [[NSMutableArray alloc] init];
NSScanner *scanner = [NSScanner scannerWithString:sourceStr];
NSString *scannedString = nil;

while ([scanner scanUpToCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString: &scannedString]) {

    // keep appending as long as we have something to append to
    if ([currentLine length] + [scannedString length] <= 23) {
        [currentLine appendFormat:@"%@ ", scannedString];
    }
    else if ([currentLine length] == 0) { // Newline but next word > 23
        [resultString appendFormat:@"%@\n", scannedString];
    }
    else { // Need to break line and start new one
        [resultString appendFormat:@"%@\n", currentLine];
        [currentLine setString:[NSString stringWithFormat:@"%@ ", scannedString]];
    }
    [scanner scanCharactersFromSet:[NSCharacterSet whitespaceCharacterSet] intoString:NULL];
}
// we may have something left over that couldn't fit into the last line
// that needs to be appended to the result string
if (currentLine.length > 0) {
    [resultString appendFormat:@"%@\n", currentLine];
}
NSLog(@"Result String: \n%@", resultString);