0

I'm translating a small java library to objective c. I'm having some trouble translating the following:

return other.tokens[0].equals(tokens[0]) &&
       other.tokens[1].equals(tokens[1]) &&
       other.tokens[2].equals(tokens[2]) &&
       other.tokens[3].equals(tokens[3]);

where other is an instance of a class, and tokens is a string array that is an instance variable of the same class. So far, what I have is this:

return ([other [[tokens[0] equals:tokens[0]]]] && 
        [other [[tokens[1] equals:tokens[1]]]] && 
        [other [[tokens[2] equals:tokens[2]]]] && 
        [other [[tokens[3] equals:tokens[3]]]]);

But I keep getting an "expected identifier" error, which I believe means I have something wrong with my syntax.

Mike Mertsock
  • 11,825
  • 7
  • 42
  • 75
Malfunction
  • 1,324
  • 4
  • 15
  • 25

3 Answers3

1

since you have getToken method implementation in Quad class, you can use it with following implementation.

return ([[other getTokens:0] isEqualToString: [self getTokens[0]] &&
       [[other getTokens:1] isEqualToString: [self getTokens[1]] &&
       [[other getTokens:2] isEqualToString: [self getTokens[2]] &&
       [[other getTokens:3] isEqualToString: [self getTokens[3]]);
ldindu
  • 4,270
  • 2
  • 20
  • 24
0

You can make your life easy be breaking four conditions into 4 lines as:

BOOL b1=[other.tokens[0] isEqualTo:tokens[0]];
BOOL b2=[other.tokens[1] isEqualTo:tokens[1]];
BOOL b3=[other.tokens[2] isEqualTo:tokens[2]];
BOOL b4=[other.tokens[3] isEqualTo:tokens[3]];

return (b1 && b2 && b3 && b4);

Also, alternate way is iterate through all the values, if any one is False return NO:

for(NSInteger i=0; i<4; i++){
    if(![other.tokens[i] isEqualTo:tokens[i]]){
         return NO;
    }
}
return YES;

And read this, as I am not sure what kind of value are you comparing (Object / NSString / Number etc).. Implementing -hash / -isEqual: / -isEqualTo...: for Objective-C collections

Community
  • 1
  • 1
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • I'm getting an error when using the above syntax, presumably due to the dot syntax, and I'm not sure how to fix it. – Malfunction Jun 30 '13 at 10:52
  • Also, "tokens" is an NSArray. – Malfunction Jun 30 '13 at 10:55
  • "property 'tokens' not found on object of type 'quad *'". It's dealing with tokens as a property rather than an instant variable, which is due to the dot syntax used. – Malfunction Jun 30 '13 at 11:12
  • Now what is `quad`, your problem is increasing... cant solve without seeing full -relevant-code – Anoop Vaidya Jun 30 '13 at 11:14
  • I'm really sorry, but since I'm not sure of what exactly is the problem, I'm not able to exactly pin-point out all the relevant. Will this help? http://pastebin.com/BRrULVRL – Malfunction Jun 30 '13 at 11:30
  • There is no such method as isEqualTo in objective C. Please refers to documentation properly before posting any answer. – ldindu Jun 30 '13 at 18:32
0

There is no such method "equals" in Objective C. If you are working on string comparison, it should be

[[token objectAtIndex: 0] isEqualToString:[token objectAtIndex: 0]] 

but why you are comparing same strings.

ldindu
  • 4,270
  • 2
  • 20
  • 24
  • I'm not comparing the same string. The first string is "other"'s instant variable. How do I add "other" to the first part of the code? – Malfunction Jun 30 '13 at 10:52