• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Where can I get some examples of if() conditionals being used with string variables in Objective-C
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Where can I get some examples of if() conditionals being used with string variables in Objective-C


  • Subject: Re: Where can I get some examples of if() conditionals being used with string variables in Objective-C
  • From: Mike Abdullah <email@hidden>
  • Date: Wed, 25 Jun 2008 13:54:58 +0100

if ([theName isEqualToString:@"John Lennon"])
{
	theName = @"Ringo Starr";
}


--------------

By using == you were checking to see if the strings were the same object. Sometimes this would, sometimes it would not. Instead, you want to check if the two objects have equivalent contents. Thus, you need to ask one of them if they are equal to the other. This can be done using -isEqualToString: or -isEqual:

Secondly, you are using NSString not NSMutableString, so it is immutable; you cannot call some method to replace its contents. You could do this with NSMutableString, but it would generally be bad practice. Instead, just do the simple code I have above.


Mike.

On 25 Jun 2008, at 13:24, Papa-Raboon wrote:

Hi All,

Anybody know where can I get some examples of if() conditionals being used
with string variables in Objective-C?


To be specific, I am trying to use an if conditional to check the value of a
string being a certain value and then replacing it with a different value if
the condition returns TRUE.
Currently I have:


NSString *theName = [NSString stringWithFormat:@"%@ %@", [firstName
stringValue], [lastName stringValue]];

I thought that this would work but it doesn't:

if ([NSString theName] == @"John Lenon") {

// then something here using setStringValue but haven't got a clue how to do
this.


}

I want to check if "theName is currently "John Lenon" and change it to be
"Ringo Starr".


Any help would be greatly appreciated thanks as I am a complete newbie and
need to get my head around many things in Objective-C.


Thanks

Paul Randall
_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Where can I get some examples of if() conditionals being used with string variables in Objective-C (From: Papa-Raboon <email@hidden>)

  • Prev by Date: IB 3.0's Managed Object Context class
  • Next by Date: Re: IB 3.0's Managed Object Context class
  • Previous by thread: Re: Where can I get some examples of if() conditionals being used with string variables in Objective-C
  • Next by thread: Re: Where can I get some examples of if() conditionals being used with string variables in Objective-C
  • Index(es):
    • Date
    • Thread