• 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: Learning Cocoa/ObjectiveC and wondering why I had to do this.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Learning Cocoa/ObjectiveC and wondering why I had to do this.


  • Subject: Re: Learning Cocoa/ObjectiveC and wondering why I had to do this.
  • From: "M. Uli Kusterer" <email@hidden>
  • Date: Mon, 7 Mar 2005 00:46:48 +0100

Just realized there may be another way to read your question... So, another explanation here, even though I think Joar probably nailed your question:

At 17:20 Uhr -0500 06.03.2005, Tom Boucher wrote:
Content-Type: multipart/signed; protocol="application/pgp-signature";
	micalg=pgp-sha1; boundary="Apple-Mail-3-843901364"
Content-Transfer-Encoding: 7bit

Can you translate this for me?

NSString *result = [NSString stringWithFormat:@"%@ has %u letters.",string,letterCount];

It did what I wanted, which was create a string that is the results of a string, an unsigned int and some other words and put them in there.

But it didn't do it the way I thought I should. I can't figure out why I had to put the NSString inside the object call [NSString stringWithFormat...]

Because a string specified by @"" is just an ordinary string. Moreover, it is constant, meaning you can't change the contents of such a string.


So, in this case the @"%@ has %u letters." string is just a template. The NSString class's stringWithFormat: method is a method provided by Cocoa and takes such a template string, and creates a new string, replacing the %something sequences in it with whatever arguments you pass it in addition to that string.

It works similarly to printf() etc. in the standard C library, and NSLog(), in case that is any help.

 Another approach to do the same would be:

NSString *firstPart = [string stringByAppendingString: @" has "];
NSString *numStr = [[NSNumber numberWithUnsignedInt: letterCount] stringValue];
NSString *secondPart = [firstPart stringByAppendingString: numStr];
NSString *result = [secondPart stringByAppendingString: @" letters."];

There are dozens more, and there are ways to make this more efficient, but I don't want to confuse you too much. There is also NSMutableString, which is a version of NSString that *can* be changed, but you'd have to start by creating a new mutable string from your constant, so it isn't much of an advantage. Just to be exhaustive, here's how you'd do the same with NSMutableString:

NSMutableString* result = [NSMutableString stringWithString: string];
[result appendString: @" has "];
NSString *numStr = [[NSNumber numberWithUnsignedInt: letterCount] stringValue];
[result appendString: numStr];
[result appendString: @" letters."];

However, for various reasons, the stringWithFormat: version is better. Not the least of which being a bug in NSNumber objects' stringValue method that makes it treat all numbers as signed, IIRC. So see these samples as illustrations, not as best practice.
--
Cheers,
M. Uli Kusterer
------------------------------------------------------------
"The Witnesses of TeachText are everywhere..."
http://www.zathras.de
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden
  • Follow-Ups:
    • Question to Apple: When do GetThemeImage() or GetThemePattern get called
      • From: schneider <email@hidden>
References: 
 >Learning Cocoa/ObjectiveC and wondering why I had to do this. (From: Tom Boucher <email@hidden>)

  • Prev by Date: Re: iTunes Music Library parsing help
  • Next by Date: Re: Learning Cocoa/ObjectiveC and wondering why I had to do this.
  • Previous by thread: Re: Learning Cocoa/ObjectiveC and wondering why I had to do this.
  • Next by thread: Question to Apple: When do GetThemeImage() or GetThemePattern get called
  • Index(es):
    • Date
    • Thread