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