PDA

View Full Version : question about NSArray comparison




~skepticism
Feb 15, 2012, 02:08 PM
What I'm trying to do is compare each word of a sentence entered by the user with the NSArray, replacing the key with the translated word when matching. Is there a way to run through an NSDictionary, or would I be better off with using the NSArray for the FinnishtoEnglshtranslation (that way I can compare the elements using integers)?
I'm getting a bunch of garbage with the output.

NSArray * ProbablyInaccurateFinnishtoEnglshtranslation= [NSArray arrayWithObject: @"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 check = 0;
int i, j;

NSLog(@"Enter a sentence: \n");
gets (cstring);

inputSentence = [NSString stringWithCString: cstring encoding: NSASCIIStringEncoding];

sentenceIntoWords = [inputSentence componentsSeparatedByString: @" "];

for(i=0; i<[sentenceIntoWords count]; i++)
{
for(j=0; j<[ProbablyInaccurateFinnishtoEnglshtranslation count]; (j+2)){
if([[sentenceIntoWords objectAtIndex:i] containsObject: [ProbablyInaccurateFinnishtoEnglshtranslation arrayWithObject: j]){
translatedSentence= [NSMutableString stringWithString: [ProbablyInaccurateFinnishtoEnglshtranslation arrayWithObject: (j+1)];
check = 1;
break;
}
else
check = 0;
}
if(check == 0)
translatedSentence = [NSMutableString stringWithString: [sentenceIntoWords objectAtIndex: i]];
}


NSLog(@"%@", translatedSentence);



gnasher729
Feb 15, 2012, 05:54 PM
If the code that you posted is the actual code, then I suggest that you learn how to turn all warnings on in XCode, and fix any problems that the compiler finds.

~skepticism
Feb 15, 2012, 06:37 PM
If the code that you posted is the actual code, then I suggest that you learn how to turn all warnings on in XCode, and fix any problems that the compiler finds.

Well the program runs with two warnings, I don't remember exactly what it said (I'm stuck using notepad until tomorrow).

chown33
Feb 15, 2012, 07:38 PM
There's no way the code you posted "runs with two warnings". It can't possibly compile, because what you posted is missing some important things. Like balanced use of [ ]'s.

For example, here:
if([[sentenceIntoWords objectAtIndex:i] containsObject: [ProbablyInaccurateFinnishtoEnglshtranslation arrayWithObject: j]){

The red-hilited [ is missing a corresponding ]. So there's no way it compiles as posted. (That's not the only unbalanced square-bracket, either). If it doesn't even compile, then it can't possibly run.

You've also neglected to show what function or method contains your posted code, so it can't possibly compile or run, even if the []'s balanced.

Finally, you have a major logic problem with this for loop:
for(j=0; j<[ProbablyInaccurateFinnishtoEnglshtranslation count]; (j+2))
{ ..stuff that doesn't compile.. }

This will loop forever, because nothing changes the j variable. That (j+2) won't change j. It would if it were j+=2, but that's not what you posted.

So please post complete code, inside a function, and make sure that what you post is actually compilable as posted. If you can't compile it, then post the code you have, along with the exact error messages.


(FWIW, the compilation errors are not the only errors in the code. You have other serious logic errors, too. We can address those errors after you post something that compiles.)


EDIT
Here's one of the serious logic errors, the very first line:
NSArray * ProbablyInaccurateFinnishtoEnglshtranslation= [NSArray arrayWithObject: ...stuff omitted...
This is the wrong method for a list of objects. You need to use arrayWithObjects: (note the spelling).

~skepticism
Feb 15, 2012, 09:59 PM
Appreciate the help, I made a few changes after on notepad (switching from NSDictionary to NSArray as it seems more appropriate). I'll post the code once I have access to xcode, This was in main btw.

chown33
Feb 15, 2012, 11:12 PM
Appreciate the help, I made a few changes after on notepad (switching from NSDictionary to NSArray as it seems more appropriate). I'll post the code once I have access to xcode, This was in main btw.

You weren't using NSDictionary, so how could you switch from it?

Accuracy is important. I'll just wait for the code.

~skepticism
Feb 16, 2012, 11:37 PM
I believe I have a better understanding of NSDictionary now, the only problem I have is:
if ([ProbablyInaccurateFinnishtoEnglshtranslation objectForKey:[sentenceIntoWords objectAtIndex:i] == nil])
I forgot what the compiler error was (has to do with nill), but could I use a replacement for nil such as if(condition==0)? Hopefully I can find out if I get xcode to work.




#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

@autoreleasepool {
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 check = 0;
int i, j;

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);
}
return 0;
}

Sydde
Feb 17, 2012, 01:37 PM
I forgot what the compiler error was (has to do with nill), but could I use a replacement for nil such as if(condition==0)? Hopefully I can find out if I get xcode to work.

No, the error has nothing at all to do with nil, it has to do with the fact that you put the second "]" in the wrong place. It is the same kind of error that chown33 and others have been pointing out in earlier posts. Learn how Objective-C syntax works and what its markups signify so that you can write well-formed lines of code.

~skepticism
Feb 17, 2012, 02:43 PM
Got it to work thanks, I made it harder than it really was.