• 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: printf
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: printf


  • Subject: Re: printf
  • From: James Bucanek <email@hidden>
  • Date: Thu, 13 Dec 2007 08:51:20 -0700

One additional tip:

If you're writing log-file-like output and want it as readable ASCII text, consider using -[NSString cStringUsingEncoding:NSNonLossyASCIIStringEncoding] instead of -[NSString UTF8String].

While UTF8 is great for machine-to-machine transport, NSNonLossyASCIIStringEncoding encodes non-ASCII characters using a simple ASCII escape sequence (\0134) that makes reading or parsing the file using plain ol' text editors/viewers easy. Even better, NSNonLossyASCIIStringEncoding is bidirectional like UTF8, so if you ever have to read the text back in as data use the same encoding; NSNonLossyASCIIStringEncoding will parse the escape sequences and convert them back to their original Unicode values.

Clark Cox <mailto:email@hidden> wrote (Thursday, December 13, 2007 8:03 AM -0800):

On Dec 13, 2007 6:40 AM, Roland Silver <email@hidden> wrote:
Is there a function in one of the frameworks that's like printf, but
allows the output conversion '%@' in its template? Eg

likePrintf("%@ is allowable", @"this string");
or better:
likePrintf(@"%@ is allowable", @"this string");

No, but it's relatively easy to write your own:

int likePrintf(NSString *format, ...) {
va_list args;
va_start(args, format);

NSString    *output = [[NSString alloc] initWithFormat: format
arguments: args];
va_end(args);

int result  = printf("%s", [output UTF8String]);
[output release];

return result;
}

--
James Bucanek

_______________________________________________

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


  • Follow-Ups:
    • Re: printf
      • From: "Clark Cox" <email@hidden>
References: 
 >Re: printf (From: "Clark Cox" <email@hidden>)

  • Prev by Date: Re: Need the Why and How of mouseDragged:
  • Next by Date: Re: Need the Why and How of mouseDragged:
  • Previous by thread: Re: printf
  • Next by thread: Re: printf
  • Index(es):
    • Date
    • Thread