传递参数1使得指针来自整数而没有强制转换

时间:2012-02-17 19:22:16

标签: objective-c

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]];

    }
    else {
        translatedSentence = [NSMutableString stringWithString: [sentenceIntoWords objectAtIndex: i]];
            }

现在只显示输入句子的最后一个单词,我的逻辑中是否有错误?

3 个答案:

答案 0 :(得分:2)

支架位于错误的位置。你想要这个:

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

你现在所拥有的大致相当于:

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

显然这是错误的,因为objectForKey(很可能)期望一个对象指针,而不是一个布尔值。

答案 1 :(得分: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)

答案 2 :(得分:-1)

它在我的程序中显示的类似错误,即通过'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;
}