String formatting output in objective-C

Source: Internet
Author: User
Tags format definition
When using functions such as nslog and [nsstring stringwithformat:], they all work based on C/C ++-style string formatting.

Originally, C/C ++ has never been used. In iPhone development and development, the NS series objects are added, and the formatted output is even more messy.
After reading the documentation of programming guide for cocoa, I still have a detailed description.

Format definition
The format specifiers supported by
Nsstring formatting methods and cfstring formatting functions follow
The IEEE printf Specification; The specifiers are summarized in table
1. Note that you can also use the "N $" positional specifiers such
% 1 $ @ % 2 $ S. For more details, see the IEEE printf specification. You can
Also use these format specifiers with the nslog function.

Table 1 format specifiers supported by the nsstring formatting methods and cfstring formatting Functions
Definition Description
% @ Objective-C object, printed as the string returned
Descriptionwithlocale: If available, or description otherwise. Also
Works with cftyperef objects, returning the result of
Cfcopydescription function.
% '%' Character
% D, % d, % I Signed 32-bit integer (INT)
% U, % u Unsigned 32-bit integer (unsigned INT)
% Hi Signed 16-bit integer (short)
% Hu Unsigned 16-bit integer (unsigned short)
% Qi Signed 64-bit integer (long)
% Qu Unsigned 64-bit integer (unsigned long)
% X Unsigned 32-bit integer (unsigned INT), printed in hexadecimal using the digits 0-9 and lowercase A-F
% X Unsigned 32-bit integer (unsigned INT), printed in hexadecimal using the digits 0-9 and uppercase A-F
% QX Unsigned 64-bit integer (unsigned long), printed in hexadecimal using the digits 0-9 and lowercase A-F
% QX Unsigned 64-bit integer (unsigned long), printed in hexadecimal using the digits 0-9 and uppercase A-F
% O, % O Unsigned 32-bit integer (unsigned INT), printed in octal
% F 64-bit floating-point number (double)
% E 64-bit floating-point number (double), printed in scientific notation using a lowercase e to introduce the exponent
% E 64-bit floating-point number (double), printed in scientific notation using an uppercase e to introduce the exponent
% G 64-bit floating-point number (double), printed in
Style of % E if the exponent is less than-4 or greater than or equal
The precision, in the style of % F otherwise
% G 64-bit floating-point number (double), printed in
Style of % E if the exponent is less than-4 or greater than or equal
The precision, in the style of % F otherwise
% C 8-bit unsigned character (unsigned char), printed
Nslog () as an ASCII character, or, if not an ASCII character, in
Octal format \ DDD or the Unicode hexadecimal format \ udddd, where D
Is a digit
% C 16-bit Unicode character (unichar), printed by nslog ()
As an ASCII character, or, if not an ASCII character, in the octal
Format \ DDD or the Unicode hexadecimal format \ udddd, where D is
Digit
% S Null-terminated array of 8-bit unsigned characters. % s
Interprets its input in the system encoding rather than, for example,
UTF-8.
% S Null-terminated array of 16-bit Unicode characters
% P Void pointer (void *), printed in hexadecimal with the digits 0-9 and lowercase A-F, with a leading 0x
% L Length modifier specifying that a following a, A, E, E, F, F, G, or G conversion specifier applies to a long double argument
% 64-bit floating-point number (double), printed in
Scientific notation with a leading 0x and one hexadecimal digit before
The decimal point using a lowercase P to introduce the exponent
% 64-bit floating-point number (double), printed in
Scientific notation with a leading 0x and one hexadecimal digit before
The decimal point using a uppercase P to introduce the exponent
% F 64-bit floating-point number (double), printed in decimal notation
% Z Length modifier specifying that a following D, I, O, U,
X, or x conversion specifier applies to a size_t or the corresponding
Signed integer type argument
% T Length modifier specifying that a following D, I, O, U,
X, or x conversion specifier applies to a ptrdiff_t or
Corresponding unsigned integer type argument
% J Length modifier specifying that a following D, I, O, U,
X, or x conversion specifier applies to a intmax_t or uintmax_t
Argument

Platform dependency
Mac OS X uses several data types-nsinteger,
Nsuinteger, cgfloat, and cfindex-to provide a consistent means
Representing values in 32-and 64-bit environments. In a 32-bit
Environment, nsinteger and nsuinteger are defined as int and unsigned
Int, respectively. In 64-bit environments, nsinteger and nsuinteger are
Defined as long and unsigned long, respectively. To avoid the need
Use different printf-style type specifiers depending on the platform,
You can use the specifiers shown in Table 2. Note that in some cases
You may have to cast the value.

Table 2 format specifiers for Data Types
Type Definition Suggestions
Nsinteger % LD or % LX Cast the value to long
Nsuinteger % Lu or % LX Cast the value to unsigned long
Cgfloat % F or % G % F works for floats and doubles when formatting; but see below warning when scanning
Cfindex % LD or % LX The same as nsinteger
Pointer % P % P adds 0x to the beginning of the output. If you don't want that, use % lx and cast to long.
Long long % LLD or % LLX Long long is 64-bit on both 32-and 64-bit platforms
Unsigned long % LlU or % LLX Unsigned long is 64-bit on both 32-and 64-bit platforms

The following example extends strates the use of % LD to format an nsinteger and the use of a cast.

1
2
Nsinteger I= 42;
Printf("% LD\ N",(Long)I);

In addition to the considerations mentioned in Table 2, there is one
Extra case with scanning: You must distinguish the types for float and
Double. You shoshould use % F for float, % lf for double. If you need to use
Scanf (or a variant thereof) with cgfloat, switch to double instead,
And copy the double to cgfloat.

1
2
3
4
Cgfloat imagewidth;
DoubleTMP;
Sscanf (STR,"% Lf",&Amp; TMP);
Imagewidth=TMP;

It is important to remember that % lf does not represent cgfloat
Correctly on either 32-or 64-bit platforms. This is unlike % lD, which
Works for long in all cases.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.