Objective-C中的字符串格式化输出

本文详细介绍了Objective-C中使用NSLog和NSString进行字符串格式化输出的方法,包括常用的格式化符号及其用途,以及如何处理不同数据类型的值。特别强调了在不同平台下数据类型的一致性和格式化符号的选择。


                                                    Objective-C中的字符串格式化输出

uint64的格式化请用%llu
int64的格式化请用%lld



以下内容转载自:http://www.cocoachina.com/macdev/objc/2010/0430/1289.html


在使用诸如NSLog, [NSString stringWithFormat:]之类的函数时,都是基于c/c++风格的字符串格式化工作的.

本来c/c++就没怎么用过,到iphone开发开发时掺合上NS系的对象,格式化输出更是一头的乱.
看了一下Programming Guide for Cocoa的文档,还是有比较详尽的说明的,整理出来备查.

格式定义
The format specifiers supported by the 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 as %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
定义说明
%@Objective-C object, printed as the string returned by descriptionWithLocale: if available, or description otherwise. Also works with CFTypeRef objects, returning the result of the CFCopyDescription function.
%%‘%’ character
%d, %D, %iSigned 32-bit integer (int)
%u, %UUnsigned 32-bit integer (unsigned int)
%hiSigned 16-bit integer (short)
%huUnsigned 16-bit integer (unsigned short)
%qiSigned 64-bit integer (long long)
%quUnsigned 64-bit integer (unsigned long long)
%xUnsigned 32-bit integer (unsigned int), printed in hexadecimal using the digits 0–9 and lowercase a–f
%XUnsigned 32-bit integer (unsigned int), printed in hexadecimal using the digits 0–9 and uppercase A–F
%qxUnsigned 64-bit integer (unsigned long long), printed in hexadecimal using the digits 0–9 and lowercase a–f
%qXUnsigned 64-bit integer (unsigned long long), printed in hexadecimal using the digits 0–9 and uppercase A–F
%o, %OUnsigned 32-bit integer (unsigned int), printed in octal
%f64-bit floating-point number (double)
%e64-bit floating-point number (double), printed in scientific notation using a lowercase e to introduce the exponent
%E64-bit floating-point number (double), printed in scientific notation using an uppercase E to introduce the exponent
%g64-bit floating-point number (double), printed in the style of %e if the exponent is less than –4 or greater than or equal to the precision, in the style of %f otherwise
%G64-bit floating-point number (double), printed in the style of %E if the exponent is less than –4 or greater than or equal to the precision, in the style of %f otherwise
%c8-bit unsigned character (unsigned char), 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 a digit
%C16-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 a digit
%sNull-terminated array of 8-bit unsigned characters. %s interprets its input in the system encoding rather than, for example, UTF-8.
%SNull-terminated array of 16-bit Unicode characters
%pVoid pointer (void *), printed in hexadecimal with the digits 0–9 and lowercase a–f, with a leading 0x
%LLength modifier specifying that a following a, A, e, E, f, F, g, or G conversion specifier applies to a long double argument
%a64-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
%A64-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
%F64-bit floating-point number (double), printed in decimal notation
%zLength 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
%tLength modifier specifying that a following d, i, o, u, x, or X conversion specifier applies to a ptrdiff_t or the corresponding unsigned integer type argument
%jLength modifier specifying that a following d, i, o, u, x, or X conversion specifier applies to a intmax_t or uintmax_t argument

平台依赖
Mac OS X uses several data types—NSInteger, NSUInteger,CGFloat, and CFIndex—to provide a consistent means of 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 to 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
类型定义建议
NSInteger%ld or %lxCast the value to long
NSUInteger%lu or %lxCast 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 %lxThe 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 %llxlong long is 64-bit on both 32- and 64-bit platforms
unsigned long long%llu or %llxunsigned long long is 64-bit on both 32- and 64-bit platforms

The following example illustrates 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 should 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;
double tmp;
sscanf (str, "%lf"&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.

分析以下文本,提取关键内容 Ixia Performance Endpoint for Windows 64-bit (Windows Server 2008 R2 / Windows 7 / Windows 8.x / Windows 10) File README.TXT Version 9.6 SP1 Copyright (C) Keysight Technologies 1995-2018 All rights reserved. Ixia - A Keysight Business 26601 W. Agoura Road Calabasas, CA 91302 U.S.A. Web: www.ixiacom.com Phone: +1 818 871-1800 or + 1 877 FOR IXIA Fax: +1 818 871 1805 General information: Email: info@ixiacom.com Technical support: Email: support-ixchariot@ixiacom.com Thanks! We appreciate your interest in our software. We keep seeking ways to improve it; we welcome your feedback and suggestions. We continue to enhance and expand our Web site -- come and visit us. The latest versions of the endpoints are always available for free at https://support.ixiacom.com/support-links/ixchariot/endpoint-library/platform-endpoints -------------------------------------------------------------------------------- Hardware and Software Requirements -------------------------------------------------------------------------------- - 32 MBytes of random access memory (RAM). The RAM requirement depends on RAM usage of protocol stack and number of concurrent connections or pairs. For very large tests, additional memory may be required. - Hard disk with at least 8 MBytes of space available. - Operating system requirements Windows Server 2008 R2 Windows 7 64-bit Windows 8.x 64-bit Windows 10 64-bit Note: for IP TOS and DiffServ QoS support, the endpoint requires the Windows service QWAVE (Quality Windows Audio Video Experience). This service is not installed by default in Windows Server. It can be installed from Control Panel / Programs and Features / Turn Windows features on or off / Add Features -------------------------------------------------------------------------------- After Installation... -------------------------------------------------------------------------------- On Windows, the endpoint program should be running if you have installed it and requested the Ixia Performance Endpoint to start (it runs as a service). There is nothing for you to do at the endpoint program; it just waits for incoming test requests. Only an administrator has the authority to install the endpoint. Check the configuration of your Personal Firewall software, such as the Windows Firewall. When runnings tests, ensure that the UDP and TCP ports used during the test are not blocked. In addition, the Performance Endpoint always needs to be able to receive incoming management connections on TCP port 10115. This port can be changed from the ENDPOINT.INI file by removing the ";" character in front of the MANAGEMENT_PORT keyword and specifying the desired management port. The same port must be selected in the IxChariot console. For more information on this, consult the IxChariot user manual.
最新发布
08-22
<think>首先,用户要求使用中文回答问题。文本是英文的,但我需要提取关键内容并用中文呈现。 文本内容是关于 Ixia Performance Endpoint for Windows 64-bit 的 README 文件。关键内容包括: - 产品名称:Ixia Performance Endpoint for Windows 64-bit - 支持的 Windows 版本:Windows Server 2008 R2, Windows 7 64-bit, Windows 8.x 64-bit, Windows 10 64-bit - 文件:README.TXT - 版本:9.6 SP1 - 版权信息:Copyright (C) Keysight Technologies 1995-2018 - 公司信息:Ixia - A Keysight Business,地址:26601 W. Agoura Road, Calabasas, CA 91302 U.S.A. - 联系方式:网站:www.ixiacom.com,电话:+1 818 871-1800 or +1 877 FOR IXIA,传真:+1 818 871 1805 - 一般信息邮箱:info@ixiacom.com - 技术支持邮箱:support-ixchariot@ixiacom.com - 软件下载:最新版本免费提供于 https://support.ixiacom.com/support-links/ixchariot/endpoint-library/platform-endpoints - 硬件和软件要求: - 内存:32 MB RAM,但实际需求取决于协议栈和并发连接数,大测试可能需要更多内存 - 硬盘:至少 8 MB 可用空间 - 操作系统要求:如上所述 - 注意:对于 IP TOS 和 DiffServ QoS 支持,需要 Windows 服务 QWAVE,该服务在 Windows Server 中默认未安装,需手动安装 - 安装后注意事项: - 端点程序作为服务运行,安装后自动启动(如果选择启动) - 只有管理员可以安装 - 检查个人防火墙配置,如 Windows 防火墙,确保测试中使用的 UDP 和 TCP 端口未被阻塞 - 管理端口:默认 TCP 端口 10115,可修改 ENDPOINT.INI 文件中的 MANAGEMENT_PORT 设置,端口需与 IxChariot 控制台一致 - 更多信息参考 IxChariot 用户手册 现在,我需要提取关键内容并用中文总结。关键点包括: - 产品描述和版本 - 系统要求 - 安装后配置 - 联系和支持信息 在回答中,我需要结构化地呈现这些信息。 回答后,必须提出 3-5 个相关问题,以 "§§相关问题§§" 开头,然后列出问题,每个问题用数字或点号开头。示例中是 "1. ··· 2. ··· 3. ···",所以我应该用类似格式。 问题必须基于用户的问题和我的回答。用户的问题是“分析以下文本,提取关键内容”,所以相关问题应围绕这个主题,比如关于端点软件的使用、配置、兼容性等。 可能的§§相关问题§§: 1. 如何修改 Ixia Performance Endpoint 的管理端口? 2. 在 Windows Server 上安装 QWAVE 服务的具体步骤是什么? 3. IxChariot 控制台如何与端点通信? 4. 为什么需要确保防火墙端口不被阻塞? 5. 最新版本的端点软件在哪里下载? 确保有 3-5 个。 回答格式:先回答问题,然后以 "§§相关问题§§" 开头一行,再列出问题。 禁止换行分散,所以问题列表应该紧凑。 最终回答结构: 1. 中文总结关键内容。 2. "§§相关问题§§" 3. 问题列表,如 "1. ... 2. ... 3. ..." 现在,写回答。</think>### 分析文本并提取关键内容 以下是根据提供的文本提取的关键内容,使用中文总结: 1. **产品信息**: - 名称:Ixia Performance Endpoint for Windows 64-bit - 文件:README.TXT - 版本:9.6 SP1 - 版权:Keysight Technologies 1995-2018 - 公司:Ixia(Keysight 旗下),地址:26601 W. Agoura Road, Calabasas, CA 91302 U.S.A. - 联系方式:网站 www.ixiacom.com;电话 +1 818 871-1800 或 +1 877 FOR IXIA;传真 +1 818 871 1805;一般邮箱 info@ixiacom.com;技术支持邮箱 support-ixchariot@ixiacom.com - 软件下载:最新版本免费提供于 https://support.ixiacom.com/support-links/ixchariot/endpoint-library/platform-endpoints 2. **硬件和软件要求**: - 内存:至少 32 MB RAM(实际需求取决于协议栈和并发连接数,大测试需更多内存)。 - 硬盘:至少 8 MB 可用空间。 - 操作系统:支持 Windows Server 2008 R2、Windows 7 64-bit、Windows 8.x 64-bit、Windows 10 64-bit。 - 特殊要求:支持 IP TOS 和 DiffServ QoS 需安装 Windows 服务 QWAVE(Quality Windows Audio Video Experience),该服务在 Windows Server 中默认未安装,需通过“控制面板” > “程序和功能” > “启用或关闭 Windows 功能”手动添加。 3. **安装后注意事项**: - 端点程序作为服务运行,安装后自动启动(如果选择启动选项)。 - 仅管理员有权限安装。 - 防火墙配置:需确保测试中使用的 UDP/TCP 端口未被阻塞(如 Windows 防火墙),管理端口默认 TCP 10115,可修改 ENDPOINT.INI 文件(调整 MANAGEMENT_PORT 设置),端口需与 IxChariot 控制台一致。 - 更多细节参考 IxChariot 用户手册。 4. **其他信息**: - 软件免费提供,鼓励用户反馈和建议。 - 端点程序在后台运行,等待测试请求,无需用户手动操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值