IOS objective-C 循环遍历 2 个数组

IOS objective-C loop through 2 arrays

本文关键字:数组 遍历 objective-C 循环 IOS      更新时间:2023-10-16

我以前从未做过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] != ''; 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 != ''; 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。

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

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

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

最后,请注意与printf()类似的stringWithFormat:。但是,我使用%C(大写字母 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];
}

这里有一种方法可以做到这一点,值得注意的一点:

  • 它使用带有选项NSStringEnumerationByComposedCharacterSequencesenumerateSubstringsInRange一次处理一个字符的文本。使用此方法可确保正确处理 Unicode,例如,表情符号和其他组合序列被正确视为单个"字符"。这种方法还使用了一个块,一个(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);

呵呵