int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSDictionary * ProbablyInaccurateFinnishtoEnglshtranslation= [NSDictionary dictionaryWithObjectsAndKeys: @"postilaatikko", @"mailbox", @"oppikirja", @"textbook", @"näppäimistö", @"keyboard", @"piano", @"piano", @"laskukone", @"calculator", @"manteli", @"almond", @"lumi", @"snow", @"vuori", @"mountain", @"aika", @"time", @"kynttilä", @"candle", nil];
NSString * inputSentence;
char cstring[451];
NSArray * sentenceIntoWords;
NSMutableString * translatedSentence;
int i = 0;
NSLog(@"Enter a sentence: \n");
//scanf("%s", &cstring);
gets (cstring);
inputSentence = [NSString stringWithCString: cstring encoding: NSASCIIStringEncoding];
sentenceIntoWords = [inputSentence componentsSeparatedByString: @" "];
for(i=0; i<[sentenceIntoWords count]; i++)
{
if ([ProbablyInaccurateFinnishtoEnglshtranslation objectForKey:[sentenceIntoWords objectAtIndex:i] == nil])
{
translatedSentence = [NSMutableString stringWithString: [sentenceIntoWords objectAtIndex: i]];
}
else {
translatedSentence= [ProbablyInaccurateFinnishtoEnglshtranslation objectForKey: [sentenceIntoWords objectAtIndex: i]];
}
}
NSLog(@"%@", translatedSentence);
[pool drain];
return 0;
}
我想要做的是將用戶輸入的句子的每個單詞與NSArray進行比較,匹配時用翻譯的單詞替換關鍵字。好的,我已將它更改爲: if([ProbablyInaccurateFinnishtoEnglshtranslation objectForKey:[sentenceIntoWords objectAtIndex:i]]) { translatedSentence = [ProbablyInaccurateFinnishtoEnglshtranslation objectForKey:[sentenceIntoWords objectAtIndex:i]];傳遞參數1使得整型指針沒有轉換
}
else {
translatedSentence = [NSMutableString stringWithString: [sentenceIntoWords objectAtIndex: i]];
}
現在只有輸入句子的最後一個字出現了,我的邏輯中是否有錯誤?
如果objectForKey預期的指針,如何想,如果([ProbablyInaccurateFinnishtoEnglshtranslation objectForKey:boolean_value(是/否)),有意義嗎? – user1216983 2012-02-17 19:55:43
您不應該在objectForKey函數中使用原始值。 – 2012-02-17 20:08:46
啊現在有道理,我只是放錯了支架。我做了你所說的,現在輸入句子的最後一個字是輸出。 – user1216983 2012-02-17 20:15:13