2

I cannot save the selected item from the array with a string value. Any idea what's wrong?

Its my code of compare:

if(self.detailItem){
    [WebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[self.detailItem description] ofType:@"html"]isDirectory:NO]]];
    NSString *value = (NSString *)self.detailItem;
    NSString *value1 = (NSString *)@"DecideVectorVPiramidi";
    value=[NSString stringWithFormat:@"%@", value];
    value1=[NSString stringWithFormat:@"%@", value1];
    if (value == value1) {
        WebView.scalesPageToFit=YES;
    }

This is the array code:

case 1:
            switch (self.nomberInSection) {
                case 0:
                    self.detailViewController.detailItem =[NSString stringWithFormat:@"%@", [DecideOfZakusPath objectAtIndex:indexPath.row]];
                    break;
            }
break;

Type of detailItem is id.

For errors, I checked

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
IlyaKharlamov
  • 479
  • 1
  • 4
  • 10

1 Answers1

8

You need to use isEqualToString. The ==operator will compare by reference only.

This means you have to do

if ( [value isEqualToString:value1] )
{
    ...

Your two strings will be pointers to two different memory locations, and the == will compare those two memory locations, and consequently evaluate to NO. The isEqualToString, or isEqual, is the method to use for doing a string comparison using the string equality rules (ie. if the two strings contains the same characters, they are considered equal).

This can be a bit counter-intuitive. In Objective-C, operator overloading is not possible. In many other languages, the operators can be overloaded so that a value comparison for built in and custom classes can be done using the equality operator.

Community
  • 1
  • 1
driis
  • 161,458
  • 45
  • 265
  • 341