Mike Owens
Mike Owens

Reputation: 936

Trying to understand why my if else statement does not work?

I am quite sure there are better ways of implementing what I am trying to do, and I am just trying different avenues to see what works best for me... So I have a UISegmentedController with 4 segments. I gave each a title in interface Builder and what I am trying to do is when they press a certain segment it will play the corresponding video. I am using a webview because... well that is what i know... so far... here is my code:

-(IBAction) getVideo: (id) sender
{
 NSURL *videoURl;
 NSString *videoURLString;
 NSString *video;

 video = [videoChoice titleForSegmentAtIndex:
   videoChoice.selectedSegmentIndex];

 if (video = @"Shimmy") {
  videoURLString = [[NSString alloc] initWithFormat:
        @"http://www.andalee.com/iPhoneVideos/mov.MOV"];
 } else if (video = @"Camel") {
  videoURLString = [[NSString alloc] initWithFormat:
        @"http://www.andalee.com"];
 } else {
  videoURLString = [[NSString alloc] initWithFormat:
        @"http://www.yahoo.com"];
 }

 videoURl = [[NSURL alloc] initWithString: videoURLString];
 [videoView loadRequest: [NSURLRequest requestWithURL: videoURl]];

 [videoURl release];
 [videoURLString release];
}

So when I use this (video = @"Shimmy") it goes straight to the video but the same for every segment... now if I change it to (video == @"Shimmy") it goes straight to the else and nothing... else. By the way the links right now are irrelevant just trying to understand the mechanics.

Upvotes: 0

Views: 282

Answers (4)

Nava Carmon
Nava Carmon

Reputation: 4533

In Objective-C the test video == @"Shimmy" compares 2 string objects, while video = @"Shimmy" is an assignment. What you want is:

if ([video isEqualToString:@"Shimmy"])

good luck

P.S. and if we're talking about better practices, you can allocate an NSString, using one of convenience methods and it will be autoreleased later. Like instead of

videoURLString = [[NSString alloc] initWithFormat: @"http://www.yahoo.com"]; 

you can use

videoURLString = [NSString stringWithFormat: @"http://www.yahoo.com"]; 

of cause don't call release in the end.

Upvotes: 3

Clyde Lobo
Clyde Lobo

Reputation: 9174

Well I dont know anything about Objective C , but i guess as per other programming languages , = is just plain assignment and would return true and hence the if loop always evluates, and == checks for the contents.

Try reading up here the difference between equals and == in Objective C

Upvotes: 0

Mike Bretz
Mike Bretz

Reputation: 1986

With video = @"Shimmy" you make an assignment to the variable video. The result of this assignment is @"Shimmy" which is evaluated as not false, so ghe if condition is true and the statement executed.

With == you make a pointer value comparision. This will only be trie in rare cases -exactly when the value has a pointer to exactly the same string. This will never be true if you compare with a ststic @"string".

you need to use a function to compare strings, e.g.

if ([value isEqualToString:@"Shimmy"]) {...}

Upvotes: 1

Dave DeLong
Dave DeLong

Reputation: 243156

When you use one equal sign you're doing an assignment. So:

if (video = @"Shimmy")

This is testing to see if you were able to successfully change video to point to the string @"Shimmy".

Using two equal signs tests for explicit equality. So:

if (video == @"Shimmy")

This tests to see if video is equal and identical to @"Shimmy". Note that the following will be false:

if ([NSMutableString stringWithString:@"Shimmy"] == @"Shimmy")

Because the first string is not the same string as the second string. They may have the same content, but they're not the same object.

What you're looking for is:

if ([video isEqual:@"Shimmy"])

This tests content equality.

Upvotes: 6

Related Questions