【问题标题】:IOS objective-C loop through 2 arraysIOS Objective-C循环通过2个数组
【发布时间】:2018-02-04 07:16:36
【问题描述】:

我以前从未做过Objective-C,只会一点点c和c++。为了好玩,我做了一个小的 c++ 程序,不是最有效的代码,但没关系,它将一个单词/句子转换为一个单词/句子,所有辅音都在末尾添加 op。

char vowels[]{'b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','q','w','x','y','z'};
string word;
int temp;


cout << "Enter a word or exit to stop program: " << endl;

getline(cin,word);

cout << "OP translation is: ";


for(int i = 0; word[i] != '\0'; i++){

    if(word[i] == vowels[0] || word[i] == vowels[1] || word[i] == vowels[2] || word[i] == vowels[3] || word[i] == vowels[4] || word[i] == vowels[5] || word[i] == vowels[6]
       || word[i] == vowels[7] || word[i] == vowels[8] || word[i] == vowels[9] || word[i] == vowels[10] || word[i] == vowels[11] || word[i] == vowels[12] || word[i] == vowels[13]
       || word[i] == vowels[14] || word[i] == vowels[15] || word[i] == vowels[16] || word[i] == vowels[17] || word[i] == vowels[18] || word[i] == vowels[19] || word[i] == vowels[20]
       || word[i] == vowels[21]){
        if(word[0])
            cout<<word[i] << "op" << " ";
        else
            cout << " " << word[i] << "op";
    }
    else
        cout<<word[i] << " ";
}

现在我想尝试将它变成一个应用程序,但我不确定如何在objective-c 中循环遍历字符串和数组。很多可能是错误的,但从一个小教程中我发现了这一点。

NSString *word = self.translateTextField.text;

NSArray *vowels;
vowels = [NSArray arrayWithObjects: @"b",@"c",@"d",@"f",@"g",@"h",@"j",@"k",@"l",@"m",@"n",@"p",@"q",@"r",@"s",@"t",@"v",@"q",@"w",@"x",@"y",@"z", nil];

NSArray *compArray = [word componentsSeparatedByString:@"-"];
int i;

for (i = 0; self.translateTextField.text != '\0'; i++){
    //NSLog (@"Element %i = %@", i, [vowels objectAtIndex: i]);
    if([compArray objectAtIndex:i] == [vowels objectAtIndex:i]){
        self.translationTextField.text = ;
    }
    else
        self.translationTextField.text = ;

}

基本上,如果您将 home 写入 translateTextField 并点击 translatePressed 按钮,我希望它在 translationTextField 中输出 hop o mop e。

【问题讨论】:

    标签: c++ ios objective-c arrays xcode


    【解决方案1】:

    您已接近解决方案。我对您的代码进行了一些修改,并将其变成了一个函数。输入参数是您的单词,但它适用于任何字符串。我还把你的字母数组变成了一个字符串(辅音,不是元音,对吧?)。当然也可以使用单独的字符串,然后稍微修改 if 语句。

    该方法的核心使用NSString方法substringWithRange:一次提取一个字母,然后测试consonants字符串是否包含该字母。它可能看起来是倒退的,实际上,您可以将其反转,以便循环遍历consonants 字符串并测试它们中的每一个是否存在于word 字符串中。采用哪种方式并不重要。

    我还添加了一个函数goButtonPressed:,向您展示如何使用translate: 函数。这可以从您的故事板按钮连接,假设您有一个名为 resultLabel 的输出的 UILabel 并且您的输入位于一个名为 inputFieldUITextField 中。

    最后,请注意stringWithFormat: 的作用类似于printf()。但是,我使用 %C (capital C) 来输出 Unicode 字符。虽然小写 %c 将适用于美国 ASCII,但它会在其他字符上失败。

    - (NSString *)translate:(NSString *)word {
        NSString *consonats = @"bcdfghjklmnpqrstvwqxyz";
    
        NSString *result = @"";
        for (NSInteger i = 0; i < word.length; i++) {
            if ([consonats containsString:[word substringWithRange:NSMakeRange(i, 1)]])
                result = [NSString stringWithFormat:@"%@%Cop ", result, [word characterAtIndex:i]];
            else
                result = [NSString stringWithFormat:@"%@%C ", result, [word characterAtIndex:i]];
        }
        return result;
    }
    
    - (IBAction)goButtonPressed:(UIButton *)sender {
        _resultLabel.text = [self translate:_inputField.text];
    }
    

    【讨论】:

    • 非常感谢,是的,辅音不是元音。它被命名为元音,因为一开始我要发出一系列元音,但最终变成了辅音,只是忘记改变它并留下了它。
    【解决方案2】:

    这是一种方法,值得注意的点:

    • 它使用enumerateSubstringsInRangeNSStringEnumerationByComposedCharacterSequences 选项来一次处理一个字符的文本。使用此方法可确保正确处理 Unicode,例如表情符号和其他组合序列被正确地视为单个“字符”。此方法还使用了一个 block,这是一个 (Objective-)C 内联函数来处理每个单独的“字符”。
    • 最终文本建立在一个可变字符串translated 中,使其能够有效地累积。
    • 您的原始字符串数组被保留,而不是循环遍历它的成员资格使用conainsObject: 进行测试

    片段可以用于改进,例如它增加了一个太多的空格,但给出了一般的想法:

     NSString *word = @"home";
    
     NSArray *constants = @[@"b", @"c", @"d", @"f", @"g", @"h", @"j",
                            @"k", @"l", @"m", @"n", @"p", @"q", @"r",
                            @"s", @"t", @"v", @"w", @"x", @"y", @"z"];
    
     NSMutableString *translated = [NSMutableString new]; // mutable string to build up result in
    
     [word enumerateSubstringsInRange:NSMakeRange(0, word.length)
                              options:NSStringEnumerationByComposedCharacterSequences // enumerate by "character"
                           usingBlock:^(NSString *nextChar, NSRange ignoreOne, NSRange ignoreTwo, BOOL *ignoreThree)
                           {
                               [translated appendString:nextChar]; // add the character
                               if ([constants containsObject:nextChar]) [translated appendString:@"op"]; // append "op" if a consonant
                               [translated appendString:@" "];  // append space (note that overall one too many is appended by this sample)
                           }];
    
     NSLog(@"%@", translated);
    

    HTH

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-04
      相关资源
      最近更新 更多