如何在NSString中使用printf

时间:2022-08-03 20:56:02

I need to use something like NSLog but without the timestamp and newline character, so I'm using printf. How can I use this with NSString?

我需要使用NSLog但是没有时间戳和换行字符,所以我使用printf。如何使用NSString?

2 个解决方案

#1


91  

You can convert an NSString into a UTF8 string by calling the UTF8String method:

可以通过调用UTF8String方法将NSString转换为UTF8字符串:

printf("%s", [string UTF8String]);

#2


0  

//public method that accepts a string argument

- (void) sayThis : ( NSString* )  this 
{

    printf("%s",[this cString]);    
}

According to the NSString.h ( html version ) the UTF8String method is only available on Mac OSX.

根据NSString。UTF8String方法只能在Mac OSX上使用。

(see below ) All the other methods I looked at are marked as 'availability:Openstep'

(见下文)我所研究的所有其他方法都标记为“可用性:Openstep”

There are further methods that will return regular char* strings but they might throw character conversion exceptions.

还有一些方法将返回常规的char* string,但是它们可能抛出字符转换异常。

NOTE The string pointers point to memory that might go away so you have to copy the strings if you want to keep a copy of the string contents, but immediate printing should be fine ?

注意,字符串指针指向可能会消失的内存,因此如果您想保留字符串内容的副本,就必须复制字符串,但立即打印应该可以吗?

There are also methods that will return an encoded string, and a method to test if the encoding you want will work ( I think ) so you can check if your required encoding will work and then request a string that has been encoded as required.

还有一些方法可以返回编码的字符串,还有一个方法可以测试您想要的编码是否有效(我认为),这样您就可以检查所需的编码是否有效,然后请求按照需要编码的字符串。

From reading through the .h file itself there are many encodings and translations between encodings. These are managed using enumerations so you can pass the type of encoding you want as an argument.

从读取.h文件本身开始,在编码之间有许多编码和翻译。这些都是使用枚举进行管理的,因此可以将所需的编码类型作为参数传递。

On linux etc. do :

在linux等上做:

locate NSString.h ** Note this found the html doc file also

定位NSString。h ** *注意,这也找到了html文档文件

otherwise do a :

否则做一个:

find /usr -name NSString.h

找到/ usr - name NSString.h

NOTE Your mileage may vary :)

注意你的里程可能有所不同:

Thanks.

谢谢。

From the NSString.h html doc file :

cString - (const char*) cString; Availability: OpenStep

cString - (const char*) cString;可用性:OpenStep

Returns a pointer to a null terminated string of 8-bit characters in the default encoding. The memory pointed to is not owned by the caller, so the caller must copy its contents to keep it. Raises an NSCharacterConversionException if loss of information would occur during conversion. (See -canBeConvertedToEncoding: .)

返回一个指向默认编码中带8位字符的空终止字符串的指针。指向的内存不属于调用者,因此调用者必须复制它的内容来保存它。如果在转换过程中丢失了信息,就会引发NSCharacterConversionException。(见-canBeConvertedToEncoding:)。

cStringLength - (NSUInteger) cStringLength; Availability: OpenStep

(cStringLength)-了NSUInteger cStringLength;可用性:OpenStep

Returns length of a version of this unicode string converted to bytes using the default C string encoding. If the conversion would result in information loss, the results are unpredictable. Check -canBeConvertedToEncoding: first.

返回使用默认C字符串编码将此unicode字符串转换为字节的版本的长度。如果转换会导致信息丢失,结果是不可预测的。检查-canBeConvertedToEncoding:第一。

cStringUsingEncoding: - (const char*) cStringUsingEncoding: (NSStringEncoding)encoding; Availability: MacOS-X 10.4.0, Base 1.2.0

cStringUsingEncoding: - (const char*) cStringUsingEncoding:(NSStringEncoding)编码;可用性:macos - x10.4.0,基础1.2.0

Returns a pointer to a null terminated string of characters in the specified encoding. NB. under GNUstep you can used this to obtain a nul terminated utf-16 string (sixteen bit characters) as well as eight bit strings. The memory pointed to is not owned by the caller, so the caller must copy its contents to keep it. Raises an NSCharacterConversionException if loss of information would occur during conversion.

返回指向指定编码中的空终止字符字符串的指针。NB。在GNUstep下,您可以使用它来获取终止nul的utf-16字符串(16位字符)以及8位字符串。指向的内存不属于调用者,因此调用者必须复制它的内容来保存它。如果在转换过程中发生信息丢失,则引发NSCharacterConversionException。

canBeConvertedToEncoding: - (BOOL) canBeConvertedToEncoding: (NSStringEncoding)encoding; Availability: OpenStep

canBeConvertedToEncoding: - (BOOL) canBeConvertedToEncoding: (NSStringEncoding)编码;可用性:OpenStep

Returns whether this string can be converted to the given string encoding without information loss.

返回该字符串是否可以转换为给定的字符串编码,而不需要信息丢失。

#1


91  

You can convert an NSString into a UTF8 string by calling the UTF8String method:

可以通过调用UTF8String方法将NSString转换为UTF8字符串:

printf("%s", [string UTF8String]);

#2


0  

//public method that accepts a string argument

- (void) sayThis : ( NSString* )  this 
{

    printf("%s",[this cString]);    
}

According to the NSString.h ( html version ) the UTF8String method is only available on Mac OSX.

根据NSString。UTF8String方法只能在Mac OSX上使用。

(see below ) All the other methods I looked at are marked as 'availability:Openstep'

(见下文)我所研究的所有其他方法都标记为“可用性:Openstep”

There are further methods that will return regular char* strings but they might throw character conversion exceptions.

还有一些方法将返回常规的char* string,但是它们可能抛出字符转换异常。

NOTE The string pointers point to memory that might go away so you have to copy the strings if you want to keep a copy of the string contents, but immediate printing should be fine ?

注意,字符串指针指向可能会消失的内存,因此如果您想保留字符串内容的副本,就必须复制字符串,但立即打印应该可以吗?

There are also methods that will return an encoded string, and a method to test if the encoding you want will work ( I think ) so you can check if your required encoding will work and then request a string that has been encoded as required.

还有一些方法可以返回编码的字符串,还有一个方法可以测试您想要的编码是否有效(我认为),这样您就可以检查所需的编码是否有效,然后请求按照需要编码的字符串。

From reading through the .h file itself there are many encodings and translations between encodings. These are managed using enumerations so you can pass the type of encoding you want as an argument.

从读取.h文件本身开始,在编码之间有许多编码和翻译。这些都是使用枚举进行管理的,因此可以将所需的编码类型作为参数传递。

On linux etc. do :

在linux等上做:

locate NSString.h ** Note this found the html doc file also

定位NSString。h ** *注意,这也找到了html文档文件

otherwise do a :

否则做一个:

find /usr -name NSString.h

找到/ usr - name NSString.h

NOTE Your mileage may vary :)

注意你的里程可能有所不同:

Thanks.

谢谢。

From the NSString.h html doc file :

cString - (const char*) cString; Availability: OpenStep

cString - (const char*) cString;可用性:OpenStep

Returns a pointer to a null terminated string of 8-bit characters in the default encoding. The memory pointed to is not owned by the caller, so the caller must copy its contents to keep it. Raises an NSCharacterConversionException if loss of information would occur during conversion. (See -canBeConvertedToEncoding: .)

返回一个指向默认编码中带8位字符的空终止字符串的指针。指向的内存不属于调用者,因此调用者必须复制它的内容来保存它。如果在转换过程中丢失了信息,就会引发NSCharacterConversionException。(见-canBeConvertedToEncoding:)。

cStringLength - (NSUInteger) cStringLength; Availability: OpenStep

(cStringLength)-了NSUInteger cStringLength;可用性:OpenStep

Returns length of a version of this unicode string converted to bytes using the default C string encoding. If the conversion would result in information loss, the results are unpredictable. Check -canBeConvertedToEncoding: first.

返回使用默认C字符串编码将此unicode字符串转换为字节的版本的长度。如果转换会导致信息丢失,结果是不可预测的。检查-canBeConvertedToEncoding:第一。

cStringUsingEncoding: - (const char*) cStringUsingEncoding: (NSStringEncoding)encoding; Availability: MacOS-X 10.4.0, Base 1.2.0

cStringUsingEncoding: - (const char*) cStringUsingEncoding:(NSStringEncoding)编码;可用性:macos - x10.4.0,基础1.2.0

Returns a pointer to a null terminated string of characters in the specified encoding. NB. under GNUstep you can used this to obtain a nul terminated utf-16 string (sixteen bit characters) as well as eight bit strings. The memory pointed to is not owned by the caller, so the caller must copy its contents to keep it. Raises an NSCharacterConversionException if loss of information would occur during conversion.

返回指向指定编码中的空终止字符字符串的指针。NB。在GNUstep下,您可以使用它来获取终止nul的utf-16字符串(16位字符)以及8位字符串。指向的内存不属于调用者,因此调用者必须复制它的内容来保存它。如果在转换过程中发生信息丢失,则引发NSCharacterConversionException。

canBeConvertedToEncoding: - (BOOL) canBeConvertedToEncoding: (NSStringEncoding)encoding; Availability: OpenStep

canBeConvertedToEncoding: - (BOOL) canBeConvertedToEncoding: (NSStringEncoding)编码;可用性:OpenStep

Returns whether this string can be converted to the given string encoding without information loss.

返回该字符串是否可以转换为给定的字符串编码,而不需要信息丢失。