簡體   English   中英

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] != '\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 = ;

}

基本上,如果你寫的家變成了translateTextField和打translatePressed按鈕,我希望它在輸出跳Ø拖把é translationTextField

您已接近解決方案。 我對您的代碼做了一些修改,並將其變成一個函數。 輸入參數是您的單詞,但可以在任何字符串上使用。 我也將您的字母數組更改為字符串(輔音,不是元音,對嗎?)。 您當然也可以使用單獨的字符串,然后只需稍微修改if語句即可。

該方法的核心使用NSString方法substringWithRange:一次提取一個字母,然后測試consonants字符串是否包含該字母。 它可能看起來是倒退的,實際上,您可以將其反轉,以便遍歷consonants字符串並測試每個consonants是否出現在word字符串中。 哪種方式都沒關系。

我還添加了一個功能goButtonPressed:它向您展示如何使用translate:函數。 可以通過情節resultLabel按鈕進行連接,假設您有一個用於輸出的UILabel稱為resultLabel而您的輸入位於一個名為inputFieldUITextField

最后,請注意stringWithFormat:類似於printf() 但是,我使用%C大寫 C)輸出Unicode字符。 小寫的%c適用於US 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];
}

這是一種方法,值得注意的是:

  • 它使用enumerateSubstringsInRange和選項NSStringEnumerationByComposedCharacterSequences來一次處理文本。 使用此方法可確保正確處理Unicode,例如將表情符號和其他組成的序列正確地視為單個“字符”。 該方法還使用了一個 (一個(Objective-)C內聯函數)來處理每個單獨的“字符”。
  • 最終的文本以可變的字符串(已translated )構建,從而可以有效地進行積累。
  • 您的原始字符串數組將保留,而不是使用conainsObject:測試成員資格循環遍歷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);

高溫超導

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM