2012-02-17 56 views
2
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]]; 
      } 

現在只有輸入句子的最後一個字出現了,我的邏輯中是否有錯誤?

回答

2

支架位於錯誤的地方。你想這樣的:

if ([ProbablyInaccurateFinnishtoEnglshtranslation objectForKey:[sentenceIntoWords objectAtIndex:i]] == nil) 

你現在有什麼是大致相同的:

BOOL isNil = [sentenceIntoWords objectAtIndex:i]] == nil; 
if ([ProbablyInaccurateFinnishtoEnglshtranslation objectForKey:isNil) 

顯然這是錯誤的,因爲objectForKey是(最有可能)預期的對象的指針,而不是一個布爾值。

0

您不能使用objectForKey:的原始類型。

if ([ProbablyInaccurateFinnishtoEnglshtranslation objectForKey:[sentenceIntoWords objectAtIndex:i] == nil]) 

[sentenceIntoWords objectAtIndex:i] == nil行會給你布爾輸出

所以這是錯誤的if ([ProbablyInaccurateFinnishtoEnglshtranslation objectForKey:boolean_value(yes/NO))

你應該做的就像是在我的計劃顯示這個

if ([ProbablyInaccurateFinnishtoEnglshtranslation objectForKey:[sentenceIntoWords objectAtIndex:i]] == nil) 
+0

如果objectForKey預期的指針,如何想,如果([ProbablyInaccurateFinnishtoEnglshtranslation objectForKey:boolean_value(是/否)),有意義嗎? – user1216983 2012-02-17 19:55:43

+0

您不應該在objectForKey函數中使用原始值。 – 2012-02-17 20:08:46

+0

啊現在有道理,我只是放錯了支架。我做了你所說的,現在輸入句子的最後一個字是輸出。 – user1216983 2012-02-17 20:15:13

-1

類似的錯誤,iepassing 'strlen'的參數1使得整型指針沒有轉換

//編程讀取fasta序列並隨機化它!

#include<stdio.h> 
#include<stdlib.h> 
#include<ctype.h> 
#include<string.h> 

int main(int ac,char *av[]) 
{ 
    int i; 
    int l1; 
    FILE *file1; 

    if (ac < 2) return 0; 
    file1= fopen(av[1] , "r"); //"r" is a string and 'r' is a character 
    char x; 
    while(((x=fgetc(file1))!= EOF) && (x!='\n')); //skip the header part 
    while(((x=fgetc(file1))!= EOF) && (x!='>')) 
    { 
     if(isalpha(x))        //reading all the alphabets after this(>) sign 

     { 
     printf("%c",x); 

     } 
    } 

//To calculate the length of sequence 

    l1=strlen(x); 
    printf ("The sequence is %d characters long.\n",l1); 

    //to Randomize all the characters present in a string(FASTA sequence) 
    srand(time(NULL)); 

    for (i=l1-1;i>0;i--) 
    { 
    char j = rand() % (i+1); 
    char temp = j; 
    j = x; 
    x = temp; 

    } 
    fclose(file1); 
    return 0; 
} 
+0

錯誤在strlen函數中 – vShivang 2014-05-31 11:43:44

相關問題