IOS objective-C 循环遍历 2 个数组
IOS objective-C loop through 2 arrays
我以前从未做过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
,并且您的输入位于名为inputField
的UITextField
中。
最后,请注意与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];
}
这里有一种方法可以做到这一点,值得注意的一点:
- 它使用带有选项
NSStringEnumerationByComposedCharacterSequences
的enumerateSubstringsInRange
一次处理一个字符的文本。使用此方法可确保正确处理 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);
呵呵
- 遍历并行数组以确定C++中的最大数字
- 遍历二维数组的所有子数组
- 遍历向量与数组哪个更快?
- 是否可以遍历传递给函数的数组?
- 如果你有一个固定大小的数组,你需要遍历它!n次,使用二进制搜索如何改变时间复杂性
- 我该如何循环遍历我的数组(缓冲区——包含一个文本文件),并将其打印成30字节的块
- 数组数据以错误的方式遍历 Python/Matlab
- 如何使用 PHPCPP 传递对象数组,遍历每个对象并返回关联数组
- 如何使用指针遍历结构数组中的数组
- 程序在遍历所有值之前离开循环. 数组的
- 按顺序遍历 AVL 树,将值保存在数组中
- 数组 - 循环遍历辅助阵列
- 在C++中循环遍历二维数组时改进 O(n)
- 如何修复我的代码并使其遍历 2D 数组中的所有行?(C++)
- 如何遍历充满结构的数组
- 数组 通过指针遍历
- 在常量数组上使用指针遍历
- 如何遍历可变参数模板以填充 POD 数组
- 有条件的大平面数组遍历和令人惊讶的短循环执行时间
- 数组遍历中的BST