• 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: [Q] Garbage strings appened to a string
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Q] Garbage strings appened to a string


  • Subject: Re: [Q] Garbage strings appened to a string
  • From: Bill Bumgarner <email@hidden>
  • Date: Tue, 27 Sep 2005 08:43:29 -0700

On Sep 26, 2005, at 9:20 PM, JongAm Park wrote:
NSString *outputString = [ NSString stringWithFormat:@"%S is being typed.", [aString cStringUsingEncoding: NSUnicodeStringEncoding] ];

Nick is likely correct in that you are seeing the Unicode endian marker common to UTF-16.


I have an additional question: Why are you converting the string to a non-object form only to subsequently shove it into another string?

This would work better:

NSString *outputString = [ NSString stringWithFormat:@"%@ is being typed.", aString];

In general and certainly during initial development, you will be better off if you stay at the object level while programming Cocoa unless you really, really need to go to one of the native C types. Once you get everything working, profile the code to determine if you need to optimize by moving to lower level constructs.

Strings are one of the better examples of why this is so important. In particular, NSString does a mostly brilliant job of hiding all of the complexities of encodings and unicode. So, if you concatenate two strings via the NSString API where the two strings are of completely different encodings -- say, UTF-16 and ASCII -- NSString will take care of doing the appropriate conversions for you.

There is a tremendous amount of complexity and power behind the Cocoa APIs. Leverage it well and you will find that many difficult problems disappear while your productivity goes way up.

b.bum



_______________________________________________
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


References: 
 >[Q] Garbage strings appened to a string (From: JongAm Park <email@hidden>)

  • Prev by Date: Distributed Objects and notficiation
  • Next by Date: Softening NSAttributedString
  • Previous by thread: Re: [Q] Garbage strings appened to a string
  • Next by thread: Fullscreen enter/exit notification [After Panther]
  • Index(es):
    • Date
    • Thread